From 7e0b32c5696ec9db3cdee3de732de056b380568a Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Tue, 11 Jan 2022 16:52:46 -0500 Subject: [PATCH] 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 --- .changeset/tricky-eagles-enjoy.md | 5 ++++ packages/astro/src/core/build/static-build.ts | 17 +++++++++-- .../static-build/src/layouts/Main.astro | 6 ++++ .../static-build/src/pages/index.astro | 6 ++++ .../static-build/src/pages/posts/thoughts.md | 7 +++++ packages/astro/test/static-build.test.js | 28 +++++++++++++++++++ 6 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 .changeset/tricky-eagles-enjoy.md create mode 100644 packages/astro/test/fixtures/static-build/src/layouts/Main.astro create mode 100644 packages/astro/test/fixtures/static-build/src/pages/index.astro create mode 100644 packages/astro/test/fixtures/static-build/src/pages/posts/thoughts.md create mode 100644 packages/astro/test/static-build.test.js diff --git a/.changeset/tricky-eagles-enjoy.md b/.changeset/tricky-eagles-enjoy.md new file mode 100644 index 000000000..4285d2c19 --- /dev/null +++ b/.changeset/tricky-eagles-enjoy.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes use of --experimental-static-build with markdown pages diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index c424ec92d..1caeebdb1 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -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 { AstroConfig, RouteCache, SSRElement } from '../../@types/astro'; import type { AllPagesData } from './types'; @@ -35,6 +35,17 @@ function addPageName(pathname: string, opts: StaticBuildOptions): void { 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) { 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'); const generationPromises = []; for (let output of result.output) { - if (output.type === 'chunk' && output.facadeModuleId && output.facadeModuleId.endsWith('.astro')) { - generationPromises.push(generatePage(output, opts, internals, facadeIdToPageDataMap)); + if (chunkIsPage(output, internals)) { + generationPromises.push(generatePage(output as OutputChunk, opts, internals, facadeIdToPageDataMap)); } } await Promise.all(generationPromises); diff --git a/packages/astro/test/fixtures/static-build/src/layouts/Main.astro b/packages/astro/test/fixtures/static-build/src/layouts/Main.astro new file mode 100644 index 000000000..b6af3a4fe --- /dev/null +++ b/packages/astro/test/fixtures/static-build/src/layouts/Main.astro @@ -0,0 +1,6 @@ + + +Testing + + + diff --git a/packages/astro/test/fixtures/static-build/src/pages/index.astro b/packages/astro/test/fixtures/static-build/src/pages/index.astro new file mode 100644 index 000000000..e22975e58 --- /dev/null +++ b/packages/astro/test/fixtures/static-build/src/pages/index.astro @@ -0,0 +1,6 @@ + + +Testing + +

Testing

+ diff --git a/packages/astro/test/fixtures/static-build/src/pages/posts/thoughts.md b/packages/astro/test/fixtures/static-build/src/pages/posts/thoughts.md new file mode 100644 index 000000000..76e108103 --- /dev/null +++ b/packages/astro/test/fixtures/static-build/src/pages/posts/thoughts.md @@ -0,0 +1,7 @@ +--- +layout: ../../layouts/Main.astro +--- + +# Post + +Testing here diff --git a/packages/astro/test/static-build.test.js b/packages/astro/test/static-build.test.js new file mode 100644 index 000000000..2e0896cce --- /dev/null +++ b/packages/astro/test/static-build.test.js @@ -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'); + }); +});