Skip to content

Commit a184ecf

Browse files
committed
fix(astro): handle builtin modules without node prefix
1 parent 66ea1e6 commit a184ecf

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

packages/astro/src/integration/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as path from 'path';
44
import { sentryVitePlugin } from '@sentry/vite-plugin';
55
import type { AstroConfig, AstroIntegration } from 'astro';
66

7+
import { namespaceBuiltinPlugin } from './namespace';
78
import { buildClientSnippet, buildSdkInitFileImportSnippet, buildServerSnippet } from './snippets';
89
import type { SentryOptions } from './types';
910

@@ -51,6 +52,17 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
5152
});
5253
}
5354

55+
// Update Node.js builtin imports for Cloudflare.
56+
// Node.js APIs are available under `node:` prefix.
57+
// Ref: https://developers.cloudflare.com/workers/runtime-apis/nodejs/
58+
if (config?.adapter?.name.startsWith('@astrojs/cloudflare')) {
59+
updateConfig({
60+
vite: {
61+
plugins: [namespaceBuiltinPlugin()],
62+
},
63+
});
64+
}
65+
5466
const pathToClientInit = options.clientInitPath
5567
? path.resolve(options.clientInitPath)
5668
: findDefaultSdkInitFile('client');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import module from 'module';
2+
import type { Plugin } from 'vite';
3+
4+
/**
5+
* Cloudflare like environments doesn't support requiring builtin modules
6+
* to be loaded without `node:` prefix. This vite plugin ensures that,
7+
* all built-in modules are loaded with `node:` prefix.
8+
*
9+
* TODO: Remove this when we support Node 18 and above.
10+
*/
11+
export function namespaceBuiltinPlugin(): Plugin {
12+
return {
13+
name: 'sentry-builtin-namespace',
14+
enforce: 'pre',
15+
resolveId(id) {
16+
if (module.builtinModules.includes(id)) {
17+
return {
18+
id: `node:${id}`,
19+
external: true,
20+
};
21+
}
22+
23+
return undefined;
24+
},
25+
};
26+
}

0 commit comments

Comments
 (0)