0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Fix: static-build with .md pages (#2363)

* Fix: static-build with .md pages

This fixes the `--experimental-static-build` flag to work with markdown
pages.

* Adds a changeset

* Account for difference in specifier on windows
This commit is contained in:
Matthew Phillips 2022-01-11 16:52:46 -05:00 committed by GitHub
parent eaa626de88
commit 7e0b32c569
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes use of --experimental-static-build with markdown pages

View file

@ -1,4 +1,4 @@
import type { OutputChunk, PreRenderedChunk, RollupOutput } from 'rollup'; import type { OutputChunk, OutputAsset, PreRenderedChunk, RollupOutput } from 'rollup';
import type { Plugin as VitePlugin, UserConfig } from '../vite'; import type { Plugin as VitePlugin, UserConfig } from '../vite';
import type { AstroConfig, RouteCache, SSRElement } from '../../@types/astro'; import type { AstroConfig, RouteCache, SSRElement } from '../../@types/astro';
import type { AllPagesData } from './types'; import type { AllPagesData } from './types';
@ -35,6 +35,17 @@ function addPageName(pathname: string, opts: StaticBuildOptions): void {
opts.pageNames.push(pathname.replace(/\/?$/, pathrepl).replace(/^\//, '')); opts.pageNames.push(pathname.replace(/\/?$/, pathrepl).replace(/^\//, ''));
} }
// Determines of a Rollup chunk is an entrypoint page.
function chunkIsPage(output: OutputAsset | OutputChunk, internals: BuildInternals) {
if(output.type !== 'chunk') {
return false;
}
const chunk = output as OutputChunk;
return chunk.facadeModuleId &&
(internals.entrySpecifierToBundleMap.has(chunk.facadeModuleId) ||
internals.entrySpecifierToBundleMap.has('/' + chunk.facadeModuleId));
}
export async function staticBuild(opts: StaticBuildOptions) { export async function staticBuild(opts: StaticBuildOptions) {
const { allPages, astroConfig } = opts; const { allPages, astroConfig } = opts;
@ -158,8 +169,8 @@ async function generatePages(result: RollupOutput, opts: StaticBuildOptions, int
debug(opts.logging, 'generate', 'End build step, now generating'); debug(opts.logging, 'generate', 'End build step, now generating');
const generationPromises = []; const generationPromises = [];
for (let output of result.output) { for (let output of result.output) {
if (output.type === 'chunk' && output.facadeModuleId && output.facadeModuleId.endsWith('.astro')) { if (chunkIsPage(output, internals)) {
generationPromises.push(generatePage(output, opts, internals, facadeIdToPageDataMap)); generationPromises.push(generatePage(output as OutputChunk, opts, internals, facadeIdToPageDataMap));
} }
} }
await Promise.all(generationPromises); await Promise.all(generationPromises);

View file

@ -0,0 +1,6 @@
<html>
<head>
<title>Testing</title>
</head>
<body><slot></slot></body>
</html>

View file

@ -0,0 +1,6 @@
<html>
<head>
<title>Testing</title>
</head>
<body><h1>Testing</h1></body>
</html>

View file

@ -0,0 +1,7 @@
---
layout: ../../layouts/Main.astro
---
# Post
Testing here

View file

@ -0,0 +1,28 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
describe('Static build', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/static-build/',
renderers: [],
buildOptions: {
experimentalStaticBuild: true,
},
});
await fixture.build();
});
it('Builds out .astro pags', async () => {
const html = await fixture.readFile('/index.html');
expect(html).to.be.a('string');
});
it('Builds out .md pages', async () => {
const html = await fixture.readFile('/posts/thoughts/index.html');
expect(html).to.be.a('string');
});
});