Skip to content

Commit

Permalink
Fix bug for projects with spaces in their pathname (#2156)
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis committed Jul 29, 2024
1 parent 6757d97 commit 904ad47
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-planes-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/starlight": patch
---

Fixes builds for projects with a space in their pathname
4 changes: 3 additions & 1 deletion packages/starlight/__e2e__/collection-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect, testFactory } from './test-utils';

const test = await testFactory('./fixtures/custom-src-dir/');
// This fixture contains a space in the directory so that we have a smoke test for building
// Starlight projects with pathnames like this, which are a common source of bugs.
const test = await testFactory('./fixtures/custom src-dir/');

test('builds a custom page using the `<StarlightPage>` component and a custom `srcDir`', async ({
page,
Expand Down
13 changes: 10 additions & 3 deletions packages/starlight/integrations/virtual-user-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ export function vitePluginStarlightUserConfig(
build: Pick<AstroConfig['build'], 'format'>;
}
): NonNullable<ViteUserConfig['plugins']>[number] {
const resolveId = (id: string) =>
JSON.stringify(id.startsWith('.') ? resolve(fileURLToPath(root), id) : id);
/**
* Resolves module IDs to a usable format:
* - Relative paths (e.g. `'./module.js'`) are resolved against `base` and formatted as an absolute path.
* - Package identifiers (e.g. `'module'`) are returned unchanged.
*
* By default, `base` is the project root directory.
*/
const resolveId = (id: string, base = root) =>
JSON.stringify(id.startsWith('.') ? resolve(fileURLToPath(base), id) : id);

const virtualComponentModules = Object.fromEntries(
Object.entries(opts.components).map(([name, path]) => [
Expand Down Expand Up @@ -50,7 +57,7 @@ export function vitePluginStarlightUserConfig(
: 'export const logos = {};',
'virtual:starlight/collection-config': `let userCollections;
try {
userCollections = (await import('${new URL('./content/config.ts', srcDir).pathname}')).collections;
userCollections = (await import(${resolveId('./content/config.ts', srcDir)})).collections;
} catch {}
export const collections = userCollections;`,
...virtualComponentModules,
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 904ad47

Please sign in to comment.