diff --git a/.changeset/afraid-crabs-heal.md b/.changeset/afraid-crabs-heal.md new file mode 100644 index 000000000..90aa3cc3d --- /dev/null +++ b/.changeset/afraid-crabs-heal.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix for the static build when project contains a space diff --git a/package.json b/package.json index fde15dec5..a7997a7f9 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "packages/astro/test/fixtures/builtins/packages/*", "packages/astro/test/fixtures/builtins-polyfillnode", "packages/astro/test/fixtures/custom-elements/my-component-lib", - "packages/astro/test/fixtures/static-build/pkg" + "packages/astro/test/fixtures/static build/pkg" ], "volta": { "node": "14.17.0", diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 78deded54..e70429d5c 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -14,6 +14,7 @@ import { fileURLToPath } from 'url'; import glob from 'fast-glob'; import vite from '../vite.js'; import { debug, error } from '../../core/logger.js'; +import { prependForwardSlash } from '../../core/path.js'; import { createBuildInternals } from '../../core/build/internal.js'; import { rollupPluginAstroBuildCSS } from '../../vite-plugin-build-css/index.js'; import { getParamsAndProps } from '../ssr/index.js'; @@ -40,12 +41,16 @@ function addPageName(pathname: string, opts: StaticBuildOptions): void { } // Determines of a Rollup chunk is an entrypoint page. -function chunkIsPage(output: OutputAsset | OutputChunk, internals: BuildInternals) { +function chunkIsPage(astroConfig: AstroConfig, 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)); + if(chunk.facadeModuleId) { + const facadeToEntryId = prependForwardSlash(chunk.facadeModuleId.slice(fileURLToPath(astroConfig.projectRoot).length)); + return internals.entrySpecifierToBundleMap.has(facadeToEntryId); + } + return false; } // Throttle the rendering a paths to prevents creating too many Promises on the microtask queue. @@ -74,8 +79,8 @@ function* throttle(max: number, inPaths: string[]) { function getByFacadeId(facadeId: string, map: Map): T | undefined { return ( map.get(facadeId) || - // Check with a leading `/` because on Windows it doesn't have one. - map.get('/' + facadeId) + // Windows the facadeId has forward slashes, no idea why + map.get(facadeId.replace(/\//g, '\\')) ); } @@ -105,7 +110,7 @@ export async function staticBuild(opts: StaticBuildOptions) { for (const [component, pageData] of Object.entries(allPages)) { const astroModuleURL = new URL('./' + component, astroConfig.projectRoot); - const astroModuleId = astroModuleURL.pathname; + const astroModuleId = prependForwardSlash(component); const [renderers, mod] = pageData.preload; const metadata = mod.$$metadata; @@ -121,7 +126,7 @@ export async function staticBuild(opts: StaticBuildOptions) { // Add hoisted scripts const hoistedScripts = new Set(metadata.hoistedScriptPaths()); if (hoistedScripts.size) { - const moduleId = new URL('./hoisted.js', astroModuleURL + '/').pathname; + const moduleId = npath.posix.join(astroModuleId, 'hoisted.js'); internals.hoistedScriptIdToHoistedMap.set(moduleId, hoistedScripts); topLevelImports.add(moduleId); } @@ -131,7 +136,7 @@ export async function staticBuild(opts: StaticBuildOptions) { } pageInput.add(astroModuleId); - facadeIdToPageDataMap.set(astroModuleId, pageData); + facadeIdToPageDataMap.set(fileURLToPath(astroModuleURL), pageData); } // Empty out the dist folder, if needed. Vite has a config for doing this @@ -177,7 +182,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp root: viteConfig.root, envPrefix: 'PUBLIC_', server: viteConfig.server, - base: astroConfig.buildOptions.site ? new URL(astroConfig.buildOptions.site).pathname : '/', + base: astroConfig.buildOptions.site ? fileURLToPath(new URL(astroConfig.buildOptions.site)) : '/', ssr: viteConfig.ssr, } as ViteConfigWithSSR); } @@ -208,7 +213,7 @@ async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals, }, plugins: [ vitePluginNewBuild(input, internals, 'js'), - vitePluginHoistedScripts(internals), + vitePluginHoistedScripts(astroConfig, internals), rollupPluginAstroBuildCSS({ internals, }), @@ -218,7 +223,7 @@ async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals, root: viteConfig.root, envPrefix: 'PUBLIC_', server: viteConfig.server, - base: astroConfig.buildOptions.site ? new URL(astroConfig.buildOptions.site).pathname : '/', + base: astroConfig.buildOptions.site ? fileURLToPath(new URL(astroConfig.buildOptions.site)) : '/', }); } @@ -257,7 +262,7 @@ async function generatePages(result: RollupOutput, opts: StaticBuildOptions, int const generationPromises = []; for (let output of result.output) { - if (chunkIsPage(output, internals)) { + if (chunkIsPage(opts.astroConfig, output, internals)) { generationPromises.push(generatePage(output as OutputChunk, opts, internals, facadeIdToPageDataMap, renderers)); } } @@ -272,7 +277,7 @@ async function generatePage(output: OutputChunk, opts: StaticBuildOptions, inter let pageData = getByFacadeId(facadeId, facadeIdToPageDataMap); if (!pageData) { - throw new Error(`Unable to find a PageBuildData for the Astro page: ${facadeId}. There are the PageBuilDatas we have ${Array.from(facadeIdToPageDataMap.keys()).join(', ')}`); + throw new Error(`Unable to find a PageBuildData for the Astro page: ${facadeId}. There are the PageBuildDatas we have ${Array.from(facadeIdToPageDataMap.keys()).join(', ')}`); } const linkIds = getByFacadeId(facadeId, internals.facadeIdToAssetsMap) || []; @@ -383,7 +388,7 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G async function cleanSsrOutput(opts: StaticBuildOptions) { // The SSR output is all .mjs files, the client output is not. const files = await glob('**/*.mjs', { - cwd: opts.astroConfig.dist.pathname, + cwd: fileURLToPath(opts.astroConfig.dist), }); await Promise.all( files.map(async (filename) => { diff --git a/packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts b/packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts index 24071a241..84fd43b8a 100644 --- a/packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts +++ b/packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts @@ -1,11 +1,13 @@ +import type { AstroConfig } from '../../@types/astro'; import type { Plugin as VitePlugin } from '../vite'; import type { BuildInternals } from '../../core/build/internal.js'; +import { fileURLToPath } from 'url'; function virtualHoistedEntry(id: string) { return id.endsWith('.astro/hoisted.js') || id.endsWith('.md/hoisted.js'); } -export function vitePluginHoistedScripts(internals: BuildInternals): VitePlugin { +export function vitePluginHoistedScripts(astroConfig: AstroConfig, internals: BuildInternals): VitePlugin { return { name: '@astro/rollup-plugin-astro-hoisted-scripts', @@ -34,7 +36,8 @@ export function vitePluginHoistedScripts(internals: BuildInternals): VitePlugin for (const [id, output] of Object.entries(bundle)) { if (output.type === 'chunk' && output.facadeModuleId && virtualHoistedEntry(output.facadeModuleId)) { const facadeId = output.facadeModuleId!; - const filename = facadeId.slice(0, facadeId.length - '/hoisted.js'.length); + const pathname = facadeId.slice(0, facadeId.length - '/hoisted.js'.length); + const filename = fileURLToPath(new URL('.' + pathname, astroConfig.projectRoot)); internals.facadeIdToHoistedEntryMap.set(filename, id); } } diff --git a/packages/astro/test/fixtures/static-build/package.json b/packages/astro/test/fixtures/static build/package.json similarity index 100% rename from packages/astro/test/fixtures/static-build/package.json rename to packages/astro/test/fixtures/static build/package.json diff --git a/packages/astro/test/fixtures/static-build/pkg/package.json b/packages/astro/test/fixtures/static build/pkg/package.json similarity index 88% rename from packages/astro/test/fixtures/static-build/pkg/package.json rename to packages/astro/test/fixtures/static build/pkg/package.json index 42e6943a5..8d84a91a1 100644 --- a/packages/astro/test/fixtures/static-build/pkg/package.json +++ b/packages/astro/test/fixtures/static build/pkg/package.json @@ -1,7 +1,7 @@ { "name": "@astrojs/test-static-build-pkg", "main": "./oops.js", - "version": "0.0.1", + "version": "0.0.2", "exports": { ".": { "import": "./pkg.js", diff --git a/packages/astro/test/fixtures/static-build/pkg/pkg.js b/packages/astro/test/fixtures/static build/pkg/pkg.js similarity index 100% rename from packages/astro/test/fixtures/static-build/pkg/pkg.js rename to packages/astro/test/fixtures/static build/pkg/pkg.js diff --git a/packages/astro/test/fixtures/static-build/src/components/ExternalHoisted.astro b/packages/astro/test/fixtures/static build/src/components/ExternalHoisted.astro similarity index 100% rename from packages/astro/test/fixtures/static-build/src/components/ExternalHoisted.astro rename to packages/astro/test/fixtures/static build/src/components/ExternalHoisted.astro diff --git a/packages/astro/test/fixtures/static-build/src/components/InlineHoisted.astro b/packages/astro/test/fixtures/static build/src/components/InlineHoisted.astro similarity index 100% rename from packages/astro/test/fixtures/static-build/src/components/InlineHoisted.astro rename to packages/astro/test/fixtures/static build/src/components/InlineHoisted.astro diff --git a/packages/astro/test/fixtures/static-build/src/components/MainHead.astro b/packages/astro/test/fixtures/static build/src/components/MainHead.astro similarity index 100% rename from packages/astro/test/fixtures/static-build/src/components/MainHead.astro rename to packages/astro/test/fixtures/static build/src/components/MainHead.astro diff --git a/packages/astro/test/fixtures/static-build/src/components/Nav/index.jsx b/packages/astro/test/fixtures/static build/src/components/Nav/index.jsx similarity index 100% rename from packages/astro/test/fixtures/static-build/src/components/Nav/index.jsx rename to packages/astro/test/fixtures/static build/src/components/Nav/index.jsx diff --git a/packages/astro/test/fixtures/static-build/src/components/Nav/styles.module.scss b/packages/astro/test/fixtures/static build/src/components/Nav/styles.module.scss similarity index 100% rename from packages/astro/test/fixtures/static-build/src/components/Nav/styles.module.scss rename to packages/astro/test/fixtures/static build/src/components/Nav/styles.module.scss diff --git a/packages/astro/test/fixtures/static-build/src/layouts/Main.astro b/packages/astro/test/fixtures/static build/src/layouts/Main.astro similarity index 100% rename from packages/astro/test/fixtures/static-build/src/layouts/Main.astro rename to packages/astro/test/fixtures/static build/src/layouts/Main.astro diff --git a/packages/astro/test/fixtures/static-build/src/pages/hoisted.astro b/packages/astro/test/fixtures/static build/src/pages/hoisted.astro similarity index 100% rename from packages/astro/test/fixtures/static-build/src/pages/hoisted.astro rename to packages/astro/test/fixtures/static build/src/pages/hoisted.astro diff --git a/packages/astro/test/fixtures/static-build/src/pages/index.astro b/packages/astro/test/fixtures/static build/src/pages/index.astro similarity index 100% rename from packages/astro/test/fixtures/static-build/src/pages/index.astro rename to packages/astro/test/fixtures/static build/src/pages/index.astro diff --git a/packages/astro/test/fixtures/static-build/src/pages/posts/nested/more.md b/packages/astro/test/fixtures/static build/src/pages/posts/nested/more.md similarity index 100% rename from packages/astro/test/fixtures/static-build/src/pages/posts/nested/more.md rename to packages/astro/test/fixtures/static build/src/pages/posts/nested/more.md 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 similarity index 100% rename from packages/astro/test/fixtures/static-build/src/pages/posts/thoughts.md rename to packages/astro/test/fixtures/static build/src/pages/posts/thoughts.md diff --git a/packages/astro/test/fixtures/static-build/src/scripts/external-hoist.ts b/packages/astro/test/fixtures/static build/src/scripts/external-hoist.ts similarity index 100% rename from packages/astro/test/fixtures/static-build/src/scripts/external-hoist.ts rename to packages/astro/test/fixtures/static build/src/scripts/external-hoist.ts diff --git a/packages/astro/test/fixtures/static-build/src/styles/main.scss b/packages/astro/test/fixtures/static build/src/styles/main.scss similarity index 100% rename from packages/astro/test/fixtures/static-build/src/styles/main.scss rename to packages/astro/test/fixtures/static build/src/styles/main.scss diff --git a/packages/astro/test/static-build.test.js b/packages/astro/test/static-build.test.js index 57598bfd2..6645bb2b6 100644 --- a/packages/astro/test/static-build.test.js +++ b/packages/astro/test/static-build.test.js @@ -11,7 +11,7 @@ describe('Static build', () => { before(async () => { fixture = await loadFixture({ - projectRoot: './fixtures/static-build/', + projectRoot: './fixtures/static build/', renderers: ['@astrojs/renderer-preact'], buildOptions: { experimentalStaticBuild: true,