Skip to content

Commit

Permalink
fix(astro): handle builtin modules without node prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Dec 29, 2023
1 parent 66ea1e6 commit a184ecf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/astro/src/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'path';
import { sentryVitePlugin } from '@sentry/vite-plugin';
import type { AstroConfig, AstroIntegration } from 'astro';

import { namespaceBuiltinPlugin } from './namespace';
import { buildClientSnippet, buildSdkInitFileImportSnippet, buildServerSnippet } from './snippets';
import type { SentryOptions } from './types';

Expand Down Expand Up @@ -51,6 +52,17 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
});
}

// Update Node.js builtin imports for Cloudflare.
// Node.js APIs are available under `node:` prefix.
// Ref: https://developers.cloudflare.com/workers/runtime-apis/nodejs/
if (config?.adapter?.name.startsWith('@astrojs/cloudflare')) {
updateConfig({
vite: {
plugins: [namespaceBuiltinPlugin()],
},
});
}

const pathToClientInit = options.clientInitPath
? path.resolve(options.clientInitPath)
: findDefaultSdkInitFile('client');
Expand Down
26 changes: 26 additions & 0 deletions packages/astro/src/integration/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import module from 'module';
import type { Plugin } from 'vite';

/**
* Cloudflare like environments doesn't support requiring builtin modules
* to be loaded without `node:` prefix. This vite plugin ensures that,
* all built-in modules are loaded with `node:` prefix.
*
* TODO: Remove this when we support Node 18 and above.
*/
export function namespaceBuiltinPlugin(): Plugin {
return {
name: 'sentry-builtin-namespace',
enforce: 'pre',
resolveId(id) {
if (module.builtinModules.includes(id)) {
return {
id: `node:${id}`,
external: true,
};
}

return undefined;
},
};
}

0 comments on commit a184ecf

Please sign in to comment.