@@ -14,6 +14,7 @@ import { builtinModules } from "node:module";
1414const { default : babelReact } = await import ( "@babel/preset-react" ) ;
1515
1616const BUILTINS = new Set ( builtinModules ) ;
17+ const NODE_BUILTIN_PREFIX = "\0fresh-node-builtin::" ;
1718
1819type LoaderModule = typeof import ( "@deno/loader" ) ;
1920
@@ -65,11 +66,13 @@ export function deno(): Plugin {
6566 return true ;
6667 } ,
6768 async resolveId ( id , importer , options ) {
68- if ( BUILTINS . has ( id ) ) {
69- // `node:` prefix is not included in builtins list.
70- if ( ! id . startsWith ( "node:" ) ) {
71- id = `node:${ id } ` ;
69+ const builtin = id . startsWith ( "node:" ) ? id . slice ( "node:" . length ) : id ;
70+ if ( BUILTINS . has ( builtin ) ) {
71+ id = id . startsWith ( "node:" ) ? id : `node:${ id } ` ;
72+ if ( this . environment . config . consumer === "server" ) {
73+ return NODE_BUILTIN_PREFIX + id ;
7274 }
75+ // `node:` prefix is not included in builtins list.
7376 return {
7477 id,
7578 external : true ,
@@ -179,6 +182,10 @@ export function deno(): Plugin {
179182 }
180183 } ,
181184 async load ( id ) {
185+ if ( id . startsWith ( NODE_BUILTIN_PREFIX ) ) {
186+ return nodeBuiltinModule ( id . slice ( NODE_BUILTIN_PREFIX . length ) ) ;
187+ }
188+
182189 const loader = this . environment . config . consumer === "server"
183190 ? ssrLoader
184191 : browserLoader ;
@@ -399,6 +406,25 @@ function getDenoType(id: string, type: string): DenoRequestedModuleType {
399406 }
400407}
401408
409+ async function nodeBuiltinModule ( id : string ) {
410+ const names = Object . keys ( await import ( id ) ) ;
411+ return [
412+ `const mod = await Function("id", "return import(id)")(${
413+ JSON . stringify ( id )
414+ } );`,
415+ "const requireValue = mod.default ?? mod;" ,
416+ "export { requireValue as __require };" ,
417+ "export default requireValue;" ,
418+ ...names
419+ . filter ( ( name ) =>
420+ / ^ [ $ _ \p{ ID_Start} ] [ $ _ \u200c \u200d \p{ ID_Continue} ] * $ / u
421+ . test ( name )
422+ )
423+ . filter ( ( name ) => name !== "default" )
424+ . map ( ( name ) => `export const ${ name } = mod[${ JSON . stringify ( name ) } ];` ) ,
425+ ] . join ( "\n" ) ;
426+ }
427+
402428// Builds a 1:1 (line-by-line, column 0) source map so that Vite/V8 can
403429// rewrite stack frames from `\0deno::{type}::{specifier}` virtual IDs back
404430// to the original specifier. Uses an absolute URL/path in `sources` combined
0 commit comments