Skip to content

Commit dd338b4

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

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

packages/astro/src/integration/index.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ 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

10-
const PKG_NAME = '@sentry/astro';
11-
1211
export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
1312
return {
14-
name: PKG_NAME,
13+
name: '@sentry/astro',
1514
hooks: {
1615
// eslint-disable-next-line complexity
1716
'astro:config:setup': async ({ updateConfig, injectScript, addMiddleware, config, command }) => {
@@ -51,6 +50,17 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
5150
});
5251
}
5352

53+
// Update Node.js builtin imports for Cloudflare.
54+
// Node.js APIs are available under `node:` prefix.
55+
// Ref: https://developers.cloudflare.com/workers/runtime-apis/nodejs/
56+
if (config?.adapter?.name.startsWith('@astrojs/cloudflare')) {
57+
updateConfig({
58+
vite: {
59+
plugins: [namespaceBuiltinPlugin()],
60+
},
61+
});
62+
}
63+
5464
const pathToClientInit = options.clientInitPath
5565
? path.resolve(options.clientInitPath)
5666
: 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/astro',
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+
}

packages/astro/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"include": ["src/**/*"],
55

66
"compilerOptions": {
7-
// package-specific options
7+
// Required for importing 'module' for access to builtin modules.
8+
"allowSyntheticDefaultImports": true
89
}
910
}

0 commit comments

Comments
 (0)