summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@matthewphillips.info> 2022-01-28 20:29:28 -0500
committerGravatar GitHub <noreply@github.com> 2022-01-28 20:29:28 -0500
commit6bd165f84cd3a1550b29fec539af814360c87f54 (patch)
tree1a9bdd9cc379fa3ee95a3b3b45d3be7c258ae0f7
parentb3e0b80ab6b8f957919555eb63af35c3d43403ad (diff)
downloadastro-6bd165f84cd3a1550b29fec539af814360c87f54.tar.gz
astro-6bd165f84cd3a1550b29fec539af814360c87f54.tar.zst
astro-6bd165f84cd3a1550b29fec539af814360c87f54.zip
Handle project folders containing a space in the static build (#2486)
* Handle project folders containing a space * Adds a changeset * Bump the test package version * Use server relative paths as entries * Fix windows * A hoisted fix * Correctly handle facadeIds on windows
-rw-r--r--.changeset/afraid-crabs-heal.md5
-rw-r--r--package.json2
-rw-r--r--packages/astro/src/core/build/static-build.ts31
-rw-r--r--packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts7
-rw-r--r--packages/astro/test/fixtures/static build/package.json (renamed from packages/astro/test/fixtures/static-build/package.json)0
-rw-r--r--packages/astro/test/fixtures/static build/pkg/package.json (renamed from packages/astro/test/fixtures/static-build/pkg/package.json)2
-rw-r--r--packages/astro/test/fixtures/static build/pkg/pkg.js (renamed from packages/astro/test/fixtures/static-build/pkg/pkg.js)0
-rw-r--r--packages/astro/test/fixtures/static build/src/components/ExternalHoisted.astro (renamed from packages/astro/test/fixtures/static-build/src/components/ExternalHoisted.astro)0
-rw-r--r--packages/astro/test/fixtures/static build/src/components/InlineHoisted.astro (renamed from packages/astro/test/fixtures/static-build/src/components/InlineHoisted.astro)0
-rw-r--r--packages/astro/test/fixtures/static build/src/components/MainHead.astro (renamed from packages/astro/test/fixtures/static-build/src/components/MainHead.astro)0
-rw-r--r--packages/astro/test/fixtures/static build/src/components/Nav/index.jsx (renamed from packages/astro/test/fixtures/static-build/src/components/Nav/index.jsx)0
-rw-r--r--packages/astro/test/fixtures/static build/src/components/Nav/styles.module.scss (renamed from packages/astro/test/fixtures/static-build/src/components/Nav/styles.module.scss)0
-rw-r--r--packages/astro/test/fixtures/static build/src/layouts/Main.astro (renamed from packages/astro/test/fixtures/static-build/src/layouts/Main.astro)0
-rw-r--r--packages/astro/test/fixtures/static build/src/pages/hoisted.astro (renamed from packages/astro/test/fixtures/static-build/src/pages/hoisted.astro)0
-rw-r--r--packages/astro/test/fixtures/static build/src/pages/index.astro (renamed from packages/astro/test/fixtures/static-build/src/pages/index.astro)0
-rw-r--r--packages/astro/test/fixtures/static build/src/pages/posts/nested/more.md (renamed from packages/astro/test/fixtures/static-build/src/pages/posts/nested/more.md)0
-rw-r--r--packages/astro/test/fixtures/static build/src/pages/posts/thoughts.md (renamed from packages/astro/test/fixtures/static-build/src/pages/posts/thoughts.md)0
-rw-r--r--packages/astro/test/fixtures/static build/src/scripts/external-hoist.ts (renamed from packages/astro/test/fixtures/static-build/src/scripts/external-hoist.ts)0
-rw-r--r--packages/astro/test/fixtures/static build/src/styles/main.scss (renamed from packages/astro/test/fixtures/static-build/src/styles/main.scss)0
-rw-r--r--packages/astro/test/static-build.test.js2
20 files changed, 31 insertions, 18 deletions
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<T>(facadeId: string, map: Map<string, T>): 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<PageBuildData>(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<string[]>(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
index 7cf3a1792..7cf3a1792 100644
--- a/packages/astro/test/fixtures/static-build/package.json
+++ b/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
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
index b013a3b42..b013a3b42 100644
--- a/packages/astro/test/fixtures/static-build/pkg/pkg.js
+++ b/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
index a9e7d2ae2..a9e7d2ae2 100644
--- a/packages/astro/test/fixtures/static-build/src/components/ExternalHoisted.astro
+++ b/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
index ba6c0ab4d..ba6c0ab4d 100644
--- a/packages/astro/test/fixtures/static-build/src/components/InlineHoisted.astro
+++ b/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
index 532d99299..532d99299 100644
--- a/packages/astro/test/fixtures/static-build/src/components/MainHead.astro
+++ b/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
index d74961d6d..d74961d6d 100644
--- a/packages/astro/test/fixtures/static-build/src/components/Nav/index.jsx
+++ b/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
index ecbe5585e..ecbe5585e 100644
--- 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
diff --git a/packages/astro/test/fixtures/static-build/src/layouts/Main.astro b/packages/astro/test/fixtures/static build/src/layouts/Main.astro
index 26f817711..26f817711 100644
--- a/packages/astro/test/fixtures/static-build/src/layouts/Main.astro
+++ b/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
index 9677a6c52..9677a6c52 100644
--- a/packages/astro/test/fixtures/static-build/src/pages/hoisted.astro
+++ b/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
index 2251cc19f..2251cc19f 100644
--- a/packages/astro/test/fixtures/static-build/src/pages/index.astro
+++ b/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
index b2304b748..b2304b748 100644
--- 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
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
index 76e108103..76e108103 100644
--- a/packages/astro/test/fixtures/static-build/src/pages/posts/thoughts.md
+++ b/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
index ca6112cd3..ca6112cd3 100644
--- a/packages/astro/test/fixtures/static-build/src/scripts/external-hoist.ts
+++ b/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
index cb4e85345..cb4e85345 100644
--- a/packages/astro/test/fixtures/static-build/src/styles/main.scss
+++ b/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,