@@ -20,7 +20,11 @@ import { rolldown } from 'rolldown'
2020
2121import { dts } from 'rolldown-plugin-dts'
2222
23+ import { resolveChunkAddon } from '../features/banner'
24+ import { copyFiles } from '../features/copy'
25+ import { addHashToFilename , hasHash } from '../features/hash'
2326import { distSize , fmtPath , sideEffectSize } from '../utils'
27+ import { nodeProtocolPlugin } from './plugins/node-protocol'
2428import { makeExecutable , shebangPlugin } from './plugins/shebang'
2529
2630/**
@@ -44,7 +48,16 @@ function formatToRolldownFormat(format: OutputFormat): ModuleFormat {
4448/**
4549 * Get file extension for format
4650 */
47- function getFormatExtension ( format : OutputFormat , platform : Platform ) : string {
51+ function getFormatExtension (
52+ format : OutputFormat ,
53+ platform : Platform ,
54+ fixedExtension = false ,
55+ ) : string {
56+ if ( fixedExtension ) {
57+ // Force .cjs/.mjs extensions
58+ return format === 'cjs' ? '.cjs' : '.mjs'
59+ }
60+
4861 switch ( format ) {
4962 case 'esm' :
5063 return '.mjs' // Always use .mjs for ESM to be explicit about module type
@@ -182,7 +195,10 @@ export async function rolldownBuild(
182195 const baseRolldownConfig = defu ( entry . rolldown , {
183196 cwd : ctx . pkgDir ,
184197 input : inputs ,
185- plugins : [ shebangPlugin ( ) ] as Plugin [ ] ,
198+ plugins : [
199+ shebangPlugin ( ) ,
200+ nodeProtocolPlugin ( entry . nodeProtocol || false ) ,
201+ ] as Plugin [ ] ,
186202 platform : platform === 'node' ? 'node' : 'neutral' ,
187203 external : typeof entry . external === 'function'
188204 ? entry . external
@@ -213,7 +229,7 @@ export async function rolldownBuild(
213229
214230 for ( const format of formats ) {
215231 const rolldownFormat = formatToRolldownFormat ( format )
216- const extension = getFormatExtension ( format , platform )
232+ const extension = getFormatExtension ( format , platform , entry . fixedExtension )
217233
218234 // Create config for this format
219235 const formatConfig = { ...baseRolldownConfig }
@@ -256,6 +272,8 @@ export async function rolldownBuild(
256272 chunkFileNames : `_chunks/[name]-[hash]${ extension } ` ,
257273 minify : entry . minify ,
258274 name : entry . globalName , // For IIFE/UMD formats
275+ banner : resolveChunkAddon ( entry . banner , format ) ,
276+ footer : resolveChunkAddon ( entry . footer , format ) ,
259277 }
260278
261279 await hooks . rolldownOutput ?.( outConfig , res , ctx )
@@ -295,20 +313,42 @@ export async function rolldownBuild(
295313 if ( chunk . fileName . endsWith ( 'ts' ) )
296314 continue
297315
316+ let finalFileName = chunk . fileName
317+ let finalFilePath = join ( formatOutDir , chunk . fileName )
318+
319+ // Add hash to filename if requested
320+ if ( entry . hash && ! hasHash ( chunk . fileName ) ) {
321+ const content = chunk . code
322+ const hashedFileName = addHashToFilename ( chunk . fileName , content )
323+ const hashedFilePath = join ( formatOutDir , hashedFileName )
324+
325+ // Rename the file to include hash
326+ const { rename } = await import ( 'node:fs/promises' )
327+ await rename ( finalFilePath , hashedFilePath )
328+
329+ finalFileName = hashedFileName
330+ finalFilePath = hashedFilePath
331+ }
332+
298333 // Store full path for logging
299- filePathMap . set ( chunk . fileName , join ( formatOutDir , chunk . fileName ) )
334+ filePathMap . set ( finalFileName , finalFilePath )
300335
301336 allOutputEntries . push ( {
302337 format,
303- name : chunk . fileName ,
338+ name : finalFileName ,
304339 exports : chunk . exports ,
305340 deps : resolveDeps ( chunk ) ,
306- ...( await distSize ( formatOutDir , chunk . fileName ) ) ,
307- sideEffectSize : await sideEffectSize ( formatOutDir , chunk . fileName ) ,
341+ ...( await distSize ( formatOutDir , finalFileName ) ) ,
342+ sideEffectSize : await sideEffectSize ( formatOutDir , finalFileName ) ,
308343 } )
309344 }
310345 }
311346
347+ // Copy files if specified
348+ if ( entry . copy ) {
349+ await copyFiles ( ctx . pkgDir , fullOutDir , entry . copy )
350+ }
351+
312352 // Display build results
313353 consola . log (
314354 `\n${ allOutputEntries
0 commit comments