@@ -438,21 +438,26 @@ async function doBuild(
438438 try {
439439 const buildOutputOptions = ( output : OutputOptions = { } ) : OutputOptions => {
440440 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'
441446 return {
442447 dir : outDir ,
443448 // Default format is 'es' for regular and for SSR builds
444- format : cjsSsrBuild ? 'cjs' : 'es' ,
449+ format,
445450 exports : cjsSsrBuild ? 'named' : 'auto' ,
446451 sourcemap : options . sourcemap ,
447452 name : libOptions ? libOptions . name : undefined ,
448453 generatedCode : 'es2015' ,
449454 entryFileNames : ssr
450- ? `[name].js `
455+ ? `[name].${ jsExt } `
451456 : libOptions
452- ? resolveLibFilename ( libOptions , output . format || 'es' , config . root )
457+ ? resolveLibFilename ( libOptions , format , config . root , jsExt )
453458 : path . posix . join ( options . assetsDir , `[name].[hash].js` ) ,
454459 chunkFileNames : libOptions
455- ? `[name].[hash].js `
460+ ? `[name].[hash].${ jsExt } `
456461 : path . posix . join ( options . assetsDir , `[name].[hash].js` ) ,
457462 assetFileNames : libOptions
458463 ? `[name].[ext]`
@@ -591,10 +596,24 @@ function getPkgName(name: string) {
591596 return name ?. startsWith ( '@' ) ? name . split ( '/' ) [ 1 ] : name
592597}
593598
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+
594612export function resolveLibFilename (
595613 libOptions : LibraryOptions ,
596614 format : ModuleFormat ,
597- root : string
615+ root : string ,
616+ extension ?: JsExt
598617) : string {
599618 if ( typeof libOptions . fileName === 'function' ) {
600619 return libOptions . fileName ( format )
@@ -608,13 +627,7 @@ export function resolveLibFilename(
608627 'Name in package.json is required if option "build.lib.fileName" is not provided.'
609628 )
610629
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 )
618631
619632 if ( format === 'cjs' || format === 'es' ) {
620633 return `${ name } .${ extension } `
0 commit comments