@@ -438,21 +438,26 @@ async function doBuild(
438
438
try {
439
439
const buildOutputOptions = ( output : OutputOptions = { } ) : OutputOptions => {
440
440
const cjsSsrBuild = ssr && config . ssr ?. format === 'cjs'
441
+ const format = output . format || ( cjsSsrBuild ? 'cjs' : 'es' )
442
+ const jsExt =
443
+ ( ssr && config . ssr ?. target !== 'webworker' ) || libOptions
444
+ ? resolveOutputJsExtension ( format , getPkgJson ( config . root ) ?. type )
445
+ : 'js'
441
446
return {
442
447
dir : outDir ,
443
448
// Default format is 'es' for regular and for SSR builds
444
- format : cjsSsrBuild ? 'cjs' : 'es' ,
449
+ format,
445
450
exports : cjsSsrBuild ? 'named' : 'auto' ,
446
451
sourcemap : options . sourcemap ,
447
452
name : libOptions ? libOptions . name : undefined ,
448
453
generatedCode : 'es2015' ,
449
454
entryFileNames : ssr
450
- ? `[name].js `
455
+ ? `[name].${ jsExt } `
451
456
: libOptions
452
- ? resolveLibFilename ( libOptions , output . format || 'es' , config . root )
457
+ ? resolveLibFilename ( libOptions , format , config . root , jsExt )
453
458
: path . posix . join ( options . assetsDir , `[name].[hash].js` ) ,
454
459
chunkFileNames : libOptions
455
- ? `[name].[hash].js `
460
+ ? `[name].[hash].${ jsExt } `
456
461
: path . posix . join ( options . assetsDir , `[name].[hash].js` ) ,
457
462
assetFileNames : libOptions
458
463
? `[name].[ext]`
@@ -591,10 +596,24 @@ function getPkgName(name: string) {
591
596
return name ?. startsWith ( '@' ) ? name . split ( '/' ) [ 1 ] : name
592
597
}
593
598
599
+ type JsExt = 'js' | 'cjs' | 'mjs'
600
+
601
+ function resolveOutputJsExtension (
602
+ format : ModuleFormat ,
603
+ type : string = 'commonjs'
604
+ ) : JsExt {
605
+ if ( type === 'module' ) {
606
+ return format === 'cjs' || format === 'umd' ? 'cjs' : 'js'
607
+ } else {
608
+ return format === 'es' ? 'mjs' : 'js'
609
+ }
610
+ }
611
+
594
612
export function resolveLibFilename (
595
613
libOptions : LibraryOptions ,
596
614
format : ModuleFormat ,
597
- root : string
615
+ root : string ,
616
+ extension ?: JsExt
598
617
) : string {
599
618
if ( typeof libOptions . fileName === 'function' ) {
600
619
return libOptions . fileName ( format )
@@ -608,13 +627,7 @@ export function resolveLibFilename(
608
627
'Name in package.json is required if option "build.lib.fileName" is not provided.'
609
628
)
610
629
611
- let extension : string
612
-
613
- if ( packageJson ?. type === 'module' ) {
614
- extension = format === 'cjs' || format === 'umd' ? 'cjs' : 'js'
615
- } else {
616
- extension = format === 'es' ? 'mjs' : 'js'
617
- }
630
+ extension ??= resolveOutputJsExtension ( format , packageJson . type )
618
631
619
632
if ( format === 'cjs' || format === 'es' ) {
620
633
return `${ name } .${ extension } `
0 commit comments