Skip to content

Commit 10f6fe2

Browse files
HiDeoodelucis
andauthored
Prevent Cloudflare build issues due to Node.js builtins usage (#3341)
Co-authored-by: delucis <[email protected]> Co-authored-by: Chris Swithinbank <[email protected]>
1 parent 4b5a2fa commit 10f6fe2

File tree

23 files changed

+294
-167
lines changed

23 files changed

+294
-167
lines changed

.changeset/olive-weeks-rest.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/starlight': patch
3+
---
4+
5+
Prevents potential build issues with the Astro Cloudflare adapter due to the dependency on Node.js builtins.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @ts-check
2+
import starlight from '@astrojs/starlight';
3+
import { defineConfig } from 'astro/config';
4+
import { preventNodeBuiltinDependencyPlugin } from './src/noNodeModule';
5+
6+
export default defineConfig({
7+
integrations: [
8+
starlight({
9+
title: 'No Node Builtins',
10+
pagefind: false,
11+
}),
12+
],
13+
vite: {
14+
plugins: [preventNodeBuiltinDependencyPlugin()],
15+
},
16+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@e2e/no-node-builtins",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"@astrojs/starlight": "workspace:*",
7+
"astro": "^5.6.1"
8+
}
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineCollection } from 'astro:content';
2+
import { docsLoader } from '@astrojs/starlight/loaders';
3+
import { docsSchema } from '@astrojs/starlight/schema';
4+
5+
export const collections = {
6+
docs: defineCollection({ loader: docsLoader(), schema: docsSchema() }),
7+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Home Page
3+
---
4+
5+
Home page content
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { ViteUserConfig } from 'astro';
2+
import { builtinModules } from 'node:module';
3+
4+
const nodeModules = builtinModules.map((name) => [name, `node:${name}`]).flat();
5+
6+
/**
7+
* A Vite plugin used to verify that the final bundle does not have a hard dependency on Node.js
8+
* builtins due to Starlight.
9+
* This is to ensure that Starlight can run on platforms like Cloudflare.
10+
*
11+
* @see https://github.com/withastro/astro/blob/8491aa56e8685677af8458ff1c5a80d6461413f8/packages/astro/test/test-plugins.js
12+
*/
13+
export function preventNodeBuiltinDependencyPlugin(): NonNullable<
14+
ViteUserConfig['plugins']
15+
>[number] {
16+
return {
17+
name: 'verify-no-node-stuff',
18+
generateBundle() {
19+
nodeModules.forEach((name) => {
20+
const importers = this.getModuleInfo(name)?.importers || [];
21+
const starlightPath = new URL('../../../../', import.meta.url).pathname;
22+
const nodeStarlightImport = importers.find((importer) =>
23+
importer.startsWith(starlightPath)
24+
);
25+
26+
if (nodeStarlightImport) {
27+
throw new Error(
28+
'A node builtin dependency imported by Starlight was found in the production bundle:\n\n' +
29+
` - Node builtin: '${name}'\n` +
30+
` - Importer: ${nodeStarlightImport}\n`
31+
);
32+
}
33+
});
34+
},
35+
};
36+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect, testFactory } from './test-utils';
2+
3+
const test = testFactory('./fixtures/no-node-builtins/');
4+
5+
test('builds without any dependencies on Node.js builtins', async ({ page, getProdServer }) => {
6+
const starlight = await getProdServer();
7+
await starlight.goto('/');
8+
9+
await expect(page.getByText('Home page content')).toBeVisible();
10+
});

packages/starlight/__tests__/remark-rehype/anchor-links.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createTranslationSystemFromFs } from '../../utils/translations-fs';
44
import { StarlightConfigSchema, type StarlightUserConfig } from '../../utils/user-config';
55
import { absolutePathToLang as getAbsolutePathFromLang } from '../../integrations/shared/absolutePathToLang';
66
import { starlightAutolinkHeadings } from '../../integrations/heading-links';
7+
import { getCollectionPosixPath } from '../../utils/collection-fs';
78

89
const starlightConfig = StarlightConfigSchema.parse({
910
title: 'Anchor Links Tests',
@@ -23,7 +24,10 @@ const useTranslations = createTranslationSystemFromFs(
2324
);
2425

2526
function absolutePathToLang(path: string) {
26-
return getAbsolutePathFromLang(path, { astroConfig, starlightConfig });
27+
return getAbsolutePathFromLang(path, {
28+
docsPath: getCollectionPosixPath('docs', astroConfig.srcDir),
29+
starlightConfig,
30+
});
2731
}
2832

2933
const processor = await createMarkdownProcessor({

packages/starlight/__tests__/remark-rehype/asides.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createTranslationSystemFromFs } from '../../utils/translations-fs';
77
import { StarlightConfigSchema, type StarlightUserConfig } from '../../utils/user-config';
88
import { BuiltInDefaultLocale } from '../../utils/i18n';
99
import { absolutePathToLang as getAbsolutePathFromLang } from '../../integrations/shared/absolutePathToLang';
10+
import { getCollectionPosixPath } from '../../utils/collection-fs';
1011

1112
const starlightConfig = StarlightConfigSchema.parse({
1213
title: 'Asides Tests',
@@ -26,7 +27,10 @@ const useTranslations = createTranslationSystemFromFs(
2627
);
2728

2829
function absolutePathToLang(path: string) {
29-
return getAbsolutePathFromLang(path, { astroConfig, starlightConfig });
30+
return getAbsolutePathFromLang(path, {
31+
docsPath: getCollectionPosixPath('docs', astroConfig.srcDir),
32+
starlightConfig,
33+
});
3034
}
3135

3236
const processor = await createMarkdownProcessor({

packages/starlight/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ export default function StarlightIntegration(
9292
// config or by a plugin.
9393
const allIntegrations = [...config.integrations, ...integrations];
9494
if (!allIntegrations.find(({ name }) => name === 'astro-expressive-code')) {
95-
integrations.push(...starlightExpressiveCode({ starlightConfig, useTranslations }));
95+
integrations.push(
96+
...starlightExpressiveCode({ astroConfig: config, starlightConfig, useTranslations })
97+
);
9698
}
9799
if (!allIntegrations.find(({ name }) => name === '@astrojs/sitemap')) {
98100
integrations.push(starlightSitemap(starlightConfig));

0 commit comments

Comments
 (0)