@@ -31,7 +31,10 @@ import { manifestPlugin } from './plugins/manifest'
31
31
import type { Logger } from './logger'
32
32
import { dataURIPlugin } from './plugins/dataUri'
33
33
import { buildImportAnalysisPlugin } from './plugins/importAnalysisBuild'
34
- import { resolveSSRExternal , shouldExternalizeForSSR } from './ssr/ssrExternal'
34
+ import {
35
+ cjsShouldExternalizeForSSR ,
36
+ cjsSsrResolveExternals
37
+ } from './ssr/ssrExternal'
35
38
import { ssrManifestPlugin } from './ssr/ssrManifestPlugin'
36
39
import type { DepOptimizationMetadata } from './optimizer'
37
40
import {
@@ -342,7 +345,6 @@ async function doBuild(
342
345
const config = await resolveConfig ( inlineConfig , 'build' , 'production' )
343
346
const options = config . build
344
347
const ssr = ! ! options . ssr
345
- const esm = config . ssr ?. format === 'es' || ! ssr
346
348
const libOptions = options . lib
347
349
348
350
config . logger . info (
@@ -374,27 +376,14 @@ async function doBuild(
374
376
ssr ? config . plugins . map ( ( p ) => injectSsrFlagToHooks ( p ) ) : config . plugins
375
377
) as Plugin [ ]
376
378
377
- // inject ssrExternal if present
378
379
const userExternal = options . rollupOptions ?. external
379
380
let external = userExternal
380
- if ( ssr ) {
381
- // see if we have cached deps data available
382
- let knownImports : string [ ] | undefined
383
- const dataPath = path . join ( getDepsCacheDir ( config ) , '_metadata.json' )
384
- try {
385
- const data = JSON . parse (
386
- fs . readFileSync ( dataPath , 'utf-8' )
387
- ) as DepOptimizationMetadata
388
- knownImports = Object . keys ( data . optimized )
389
- } catch ( e ) { }
390
- if ( ! knownImports ) {
391
- // no dev deps optimization data, do a fresh scan
392
- knownImports = await findKnownImports ( config )
393
- }
394
- external = resolveExternal (
395
- resolveSSRExternal ( config , knownImports ) ,
396
- userExternal
397
- )
381
+
382
+ // In CJS, we can pass the externals to rollup as is. In ESM, we need to
383
+ // do it in the resolve plugin so we can add the resolved extension for
384
+ // deep node_modules imports
385
+ if ( ssr && config . ssr ?. format === 'cjs' ) {
386
+ external = await cjsSsrResolveExternal ( config , userExternal )
398
387
}
399
388
400
389
if ( isDepsOptimizerEnabled ( config ) && ! ssr ) {
@@ -432,10 +421,12 @@ async function doBuild(
432
421
433
422
try {
434
423
const buildOutputOptions = ( output : OutputOptions = { } ) : OutputOptions => {
424
+ const cjsSsrBuild = ssr && config . ssr ?. format === 'cjs'
435
425
return {
436
426
dir : outDir ,
437
- format : esm ? 'es' : 'cjs' ,
438
- exports : esm ? 'auto' : 'named' ,
427
+ // Default format is 'es' for regular and for SSR builds
428
+ format : cjsSsrBuild ? 'cjs' : 'es' ,
429
+ exports : cjsSsrBuild ? 'named' : 'auto' ,
439
430
sourcemap : options . sourcemap ,
440
431
name : libOptions ? libOptions . name : undefined ,
441
432
generatedCode : 'es2015' ,
@@ -697,26 +688,51 @@ export function onRollupWarning(
697
688
}
698
689
}
699
690
700
- function resolveExternal (
701
- ssrExternals : string [ ] ,
691
+ async function cjsSsrResolveExternal (
692
+ config : ResolvedConfig ,
702
693
user : ExternalOption | undefined
703
- ) : ExternalOption {
694
+ ) : Promise < ExternalOption > {
695
+ // see if we have cached deps data available
696
+ let knownImports : string [ ] | undefined
697
+ const dataPath = path . join ( getDepsCacheDir ( config ) , '_metadata.json' )
698
+ try {
699
+ const data = JSON . parse (
700
+ fs . readFileSync ( dataPath , 'utf-8' )
701
+ ) as DepOptimizationMetadata
702
+ knownImports = Object . keys ( data . optimized )
703
+ } catch ( e ) { }
704
+ if ( ! knownImports ) {
705
+ // no dev deps optimization data, do a fresh scan
706
+ knownImports = await findKnownImports ( config )
707
+ }
708
+ const ssrExternals = cjsSsrResolveExternals ( config , knownImports )
709
+
704
710
return ( id , parentId , isResolved ) => {
705
- if ( shouldExternalizeForSSR ( id , ssrExternals ) ) {
711
+ const isExternal = cjsShouldExternalizeForSSR ( id , ssrExternals )
712
+ if ( isExternal ) {
706
713
return true
707
714
}
708
715
if ( user ) {
709
- if ( typeof user === 'function' ) {
710
- return user ( id , parentId , isResolved )
711
- } else if ( Array . isArray ( user ) ) {
712
- return user . some ( ( test ) => isExternal ( id , test ) )
713
- } else {
714
- return isExternal ( id , user )
715
- }
716
+ return resolveUserExternal ( user , id , parentId , isResolved )
716
717
}
717
718
}
718
719
}
719
720
721
+ function resolveUserExternal (
722
+ user : ExternalOption ,
723
+ id : string ,
724
+ parentId : string | undefined ,
725
+ isResolved : boolean
726
+ ) {
727
+ if ( typeof user === 'function' ) {
728
+ return user ( id , parentId , isResolved )
729
+ } else if ( Array . isArray ( user ) ) {
730
+ return user . some ( ( test ) => isExternal ( id , test ) )
731
+ } else {
732
+ return isExternal ( id , user )
733
+ }
734
+ }
735
+
720
736
function isExternal ( id : string , test : string | RegExp ) {
721
737
if ( typeof test === 'string' ) {
722
738
return id === test
0 commit comments