88
99import assert from 'node:assert' ;
1010import path from 'node:path' ;
11- import type { OutputChunk , Plugin , RolldownOutput , RolldownPluginOption } from 'rolldown' ;
11+ import type { Plugin , RolldownOutput , RolldownPluginOption } from 'rolldown' ;
1212import { loadRolldown } from '../../../utils/load-rolldown' ;
1313import { toPosixPath } from '../../../utils/path' ;
1414import type { BundleResult , NormalizedEntryPoint , NormalizedLibraryOptions } from '../types' ;
@@ -372,6 +372,7 @@ function createMemoryFileLoaderPlugin(
372372 includeMap : boolean ,
373373) : Plugin {
374374 const isDts = extensions === DTS_EXTENSIONS ;
375+ const resolutionCache = new Map < string , { id : string ; external : boolean } | null > ( ) ;
375376
376377 return {
377378 name : 'memory-file-loader' ,
@@ -390,25 +391,48 @@ function createMemoryFileLoaderPlugin(
390391 return { id, external : true } ;
391392 }
392393
393- const resolved = id [ 0 ] === '.' ? path . posix . join ( path . posix . dirname ( importer ) , id ) : id ;
394- const normalizedResolved = resolved . replace ( / \/ + $ / , '' ) ;
394+ const importerDir = path . posix . dirname ( importer ) ;
395+ const cacheKey = `${ importerDir } \0${ id } ` ;
396+ const cached = resolutionCache . get ( cacheKey ) ;
397+ if ( cached !== undefined ) {
398+ return cached ?? undefined ;
399+ }
400+
401+ const resolved =
402+ id [ 0 ] === '.' ? path . posix . join ( importerDir , id ) : path . posix . normalize ( id ) ;
403+ const normalizedResolved = resolved . endsWith ( '/' ) ? resolved . slice ( 0 , - 1 ) : resolved ;
395404
396405 if ( files . has ( normalizedResolved ) ) {
397- return { id : normalizedResolved , external : false } ;
406+ const result = { id : normalizedResolved , external : false } ;
407+ resolutionCache . set ( cacheKey , result ) ;
408+
409+ return result ;
398410 }
399411
400- const base = normalizedResolved . replace ( / \. m ? j s $ / , '' ) ;
412+ const base = normalizedResolved . endsWith ( '.js' )
413+ ? normalizedResolved . slice ( 0 , - 3 )
414+ : normalizedResolved . endsWith ( '.mjs' )
415+ ? normalizedResolved . slice ( 0 , - 4 )
416+ : normalizedResolved ;
401417 if ( isDts && normalizedResolved . endsWith ( '.mjs' ) && files . has ( `${ base } .d.mts` ) ) {
402- return { id : `${ base } .d.mts` , external : false } ;
418+ const result = { id : `${ base } .d.mts` , external : false } ;
419+ resolutionCache . set ( cacheKey , result ) ;
420+
421+ return result ;
403422 }
404423
405424 for ( const ext of extensions ) {
406425 const candidate = base + ext ;
407426 if ( files . has ( candidate ) ) {
408- return { id : candidate , external : false } ;
427+ const result = { id : candidate , external : false } ;
428+ resolutionCache . set ( cacheKey , result ) ;
429+
430+ return result ;
409431 }
410432 }
411433
434+ resolutionCache . set ( cacheKey , null ) ;
435+
412436 return undefined ;
413437 } ,
414438 } ,
@@ -426,39 +450,83 @@ function createMemoryFileLoaderPlugin(
426450 } ;
427451}
428452
453+ /**
454+ * Pre-extracted metadata for a Rolldown output chunk to avoid repeated NAPI getter calls.
455+ */
456+ interface ChunkMetadata {
457+ readonly fileName : string ;
458+ readonly outputFilePath : string ;
459+ readonly name : string ;
460+ readonly posixModuleIds : readonly string [ ] ;
461+ readonly allImports : readonly string [ ] ;
462+ }
463+
429464/**
430465 * Processes Rolldown output assets and chunks, extracting emitted files and computing
431466 * the transitive sets of module IDs, output file paths, and tslib imports belonging to
432467 * each entry point bundle.
433468 *
434469 * @param output The array of output chunks and assets from Rolldown.
435470 * @param dir The destination POSIX output directory prefix.
471+ * @param isDts Whether the output is for TypeScript declaration (.d.ts) bundles.
436472 * @returns The processed multi-bundle output.
437473 */
438474function processRolldownOutput (
439475 output : RolldownOutput [ 'output' ] ,
440476 dir : string ,
477+ isDts : boolean ,
441478) : Omit < MultiBundleOutput , 'warnings' > {
442479 const filesToEmit : MemoryOutputFile [ ] = [ ] ;
443480 const bundles = new Map < string , EntryBundleOutput > ( ) ;
444- const chunksByFileName = new Map < string , OutputChunk > ( ) ;
481+ const chunksByFileName = new Map < string , ChunkMetadata > ( ) ;
445482 const outputFileNames = new Set < string > ( ) ;
446- const entryChunks : OutputChunk [ ] = [ ] ;
483+ const entryChunks : ChunkMetadata [ ] = [ ] ;
447484
448485 for ( const item of output ) {
449- outputFileNames . add ( item . fileName ) ;
450- filesToEmit . push (
451- createMemoryOutputFile (
452- path . posix . join ( dir , item . fileName ) ,
453- item . type === 'chunk' ? item . code : item . source ,
454- ) ,
455- ) ;
456-
457486 if ( item . type === 'chunk' ) {
458- chunksByFileName . set ( item . fileName , item ) ;
459- if ( item . isEntry ) {
460- entryChunks . push ( item ) ;
487+ const moduleIds = item . moduleIds ;
488+ if ( isDts && moduleIds . length === 1 && moduleIds [ 0 ] === '\0rolldown/runtime.js' ) {
489+ continue ;
490+ }
491+
492+ const fileName = item . fileName ;
493+ const outputFilePath = path . posix . join ( dir , fileName ) ;
494+ outputFileNames . add ( fileName ) ;
495+ filesToEmit . push ( createMemoryOutputFile ( outputFilePath , item . code ) ) ;
496+
497+ const posixModuleIds : string [ ] = [ ] ;
498+ for ( const modId of moduleIds ) {
499+ if ( modId [ 0 ] !== '\0' ) {
500+ posixModuleIds . push ( toPosixPath ( modId ) ) ;
501+ }
461502 }
503+
504+ const imports = item . imports ;
505+ const dynamicImports = item . dynamicImports ;
506+ const allImports =
507+ dynamicImports . length === 0
508+ ? imports
509+ : imports . length === 0
510+ ? dynamicImports
511+ : [ ...imports , ...dynamicImports ] ;
512+
513+ const isEntry = item . isEntry ;
514+ const chunkMeta : ChunkMetadata = {
515+ fileName,
516+ outputFilePath,
517+ name : isEntry ? item . name : '' ,
518+ posixModuleIds,
519+ allImports,
520+ } ;
521+
522+ chunksByFileName . set ( fileName , chunkMeta ) ;
523+ if ( isEntry ) {
524+ entryChunks . push ( chunkMeta ) ;
525+ }
526+ } else {
527+ const fileName = item . fileName ;
528+ outputFileNames . add ( fileName ) ;
529+ filesToEmit . push ( createMemoryOutputFile ( path . posix . join ( dir , fileName ) , item . source ) ) ;
462530 }
463531 }
464532
@@ -467,21 +535,18 @@ function processRolldownOutput(
467535 const outputFiles = new Set < string > ( ) ;
468536 let hasTslibImport = false ;
469537
470- const visited = new Set < OutputChunk > ( [ entryChunk ] ) ;
538+ const visited = new Set < ChunkMetadata > ( [ entryChunk ] ) ;
471539 for ( const chunk of visited ) {
472- outputFiles . add ( path . posix . join ( dir , chunk . fileName ) ) ;
473- const mapFileName = `${ chunk . fileName } .map` ;
474- if ( outputFileNames . has ( mapFileName ) ) {
475- outputFiles . add ( path . posix . join ( dir , mapFileName ) ) ;
540+ outputFiles . add ( chunk . outputFilePath ) ;
541+ if ( outputFileNames . has ( `${ chunk . fileName } .map` ) ) {
542+ outputFiles . add ( `${ chunk . outputFilePath } .map` ) ;
476543 }
477544
478- for ( const modId of chunk . moduleIds ) {
479- if ( modId [ 0 ] !== '\0' ) {
480- moduleIds . add ( toPosixPath ( modId ) ) ;
481- }
545+ for ( const modId of chunk . posixModuleIds ) {
546+ moduleIds . add ( modId ) ;
482547 }
483548
484- for ( const depFile of [ ... chunk . imports , ... chunk . dynamicImports ] ) {
549+ for ( const depFile of chunk . allImports ) {
485550 if ( depFile === 'tslib' ) {
486551 hasTslibImport = true ;
487552 }
@@ -562,12 +627,14 @@ async function executeMultiBundle(
562627 entryFileNames : `[name].${ extension } ` ,
563628 chunkFileNames : `[name]-[hash].${ extension } ` ,
564629 sourcemap,
630+ sourcemapExcludeSources : isDts ,
631+ sourcemapIgnoreList : false ,
565632 hoistTransitiveImports : false ,
566633 comments : { jsdoc : isDts , legal : true , annotation : ! isDts } ,
567634 } ) ;
568635
569636 return {
570- ...processRolldownOutput ( output , dir ) ,
637+ ...processRolldownOutput ( output , dir , isDts ) ,
571638 warnings,
572639 } ;
573640 } finally {
@@ -637,6 +704,12 @@ async function bundleAllDts(
637704 cjsDefault : false ,
638705 sideEffects : false ,
639706 } ) ;
707+ // Disable `generateBundle`: Rolldown already omits sourcemaps when `sourcemap: false` and omits
708+ // `sourcesContent` in Rust via `sourcemapExcludeSources: true` when `sourcemap: true`, while
709+ // `processRolldownOutput` filters out any `\0rolldown/runtime.js` chunk directly. Disabling this
710+ // hook avoids materializing Rolldown's mutable `OutputBundle` proxy and copying every `.d.ts`
711+ // chunk across NAPI.
712+ fakeJsPlugin . generateBundle = undefined ;
640713
641714 return executeMultiBundle (
642715 resolveEntryInputMap ( entryPoints , true ) ,
0 commit comments