@@ -37,6 +37,25 @@ function guardCreateRequire(): Plugin {
3737 } ;
3838}
3939
40+ // When code-splitting is enabled, bundlers hoist externalized `node:*` built-in
41+ // imports as bare side-effect imports (`import "node:buffer"`) into entry and
42+ // chunk files. These are no-ops (Node.js built-ins have no meaningful
43+ // module-level side effects) but they can cause issues on worker runtimes where
44+ // `node:*` modules may not be available or trigger unnecessary warnings.
45+ const BARE_NODE_IMPORT_RE = / ^ i m p o r t \s * [ ' " ] n o d e : [ ^ ' " ] + [ ' " ] ; ? \s * $ / gm;
46+ function stripBareNodeImports ( ) : Plugin {
47+ return {
48+ name : "nitro:cloudflare-strip-bare-node-imports" ,
49+ generateBundle ( _options , bundle ) {
50+ for ( const chunk of Object . values ( bundle ) ) {
51+ if ( chunk . type === "chunk" && chunk . code . includes ( "node:" ) ) {
52+ chunk . code = chunk . code . replace ( BARE_NODE_IMPORT_RE , "" ) ;
53+ }
54+ }
55+ } ,
56+ } ;
57+ }
58+
4059export type { CloudflareOptions as PresetOptions } from "./types.ts" ;
4160
4261const cloudflarePages = defineNitroPreset (
@@ -69,7 +88,7 @@ const cloudflarePages = defineNitroPreset(
6988 format : "esm" ,
7089 inlineDynamicImports : false ,
7190 } ,
72- plugins : [ guardCreateRequire ( ) ] ,
91+ plugins : [ guardCreateRequire ( ) , stripBareNodeImports ( ) ] ,
7392 } ,
7493 hooks : {
7594 "build:before" : async ( nitro ) => {
@@ -149,7 +168,7 @@ const cloudflareModule = defineNitroPreset(
149168 exports : "named" ,
150169 inlineDynamicImports : false ,
151170 } ,
152- plugins : [ guardCreateRequire ( ) ] ,
171+ plugins : [ guardCreateRequire ( ) , stripBareNodeImports ( ) ] ,
153172 } ,
154173 wasm : {
155174 lazy : false ,
0 commit comments