@@ -10,7 +10,9 @@ import Rollup, {
10
10
WarningHandler ,
11
11
OutputOptions ,
12
12
RollupOutput ,
13
- ExternalOption
13
+ ExternalOption ,
14
+ GetManualChunk ,
15
+ GetModuleInfo
14
16
} from 'rollup'
15
17
import { buildReporterPlugin } from './plugins/reporter'
16
18
import { buildDefinePlugin } from './plugins/define'
@@ -338,8 +340,8 @@ async function doBuild(
338
340
const generate = ( output : OutputOptions = { } ) => {
339
341
return bundle [ options . write ? 'write' : 'generate' ] ( {
340
342
dir : outDir ,
341
- format : options . ssr ? 'cjs' : 'es' ,
342
- exports : options . ssr ? 'named' : 'auto' ,
343
+ format : ssr ? 'cjs' : 'es' ,
344
+ exports : ssr ? 'named' : 'auto' ,
343
345
sourcemap : options . sourcemap ,
344
346
name : libOptions ? libOptions . name : undefined ,
345
347
entryFileNames : ssr
@@ -359,6 +361,13 @@ async function doBuild(
359
361
// #1048 add `Symbol.toStringTag` for module default export
360
362
namespaceToStringTag : true ,
361
363
inlineDynamicImports : ssr && typeof input === 'string' ,
364
+ manualChunks :
365
+ ! ssr &&
366
+ ! libOptions &&
367
+ output ?. format !== 'umd' &&
368
+ output ?. format !== 'iife'
369
+ ? moveToVendorChunk
370
+ : undefined ,
362
371
...output
363
372
} )
364
373
}
@@ -418,6 +427,33 @@ async function doBuild(
418
427
}
419
428
}
420
429
430
+ const moveToVendorChunk : GetManualChunk = ( id , { getModuleInfo } ) => {
431
+ if ( id . includes ( 'node_modules' ) && ! hasDynamicImporter ( id , getModuleInfo ) ) {
432
+ return 'vendor'
433
+ }
434
+ }
435
+
436
+ function hasDynamicImporter (
437
+ id : string ,
438
+ getModuleInfo : GetModuleInfo ,
439
+ importStack : string [ ] = [ ]
440
+ ) : boolean {
441
+ if ( importStack . includes ( id ) ) {
442
+ // circular deps!
443
+ return false
444
+ }
445
+ const mod = getModuleInfo ( id )
446
+ if ( ! mod ) {
447
+ return false
448
+ }
449
+ if ( mod . dynamicImporters . length ) {
450
+ return true
451
+ }
452
+ return mod . importers . some ( ( importer ) =>
453
+ hasDynamicImporter ( importer , getModuleInfo , importStack . concat ( id ) )
454
+ )
455
+ }
456
+
421
457
function resolveBuildOutputs (
422
458
outputs : OutputOptions | OutputOptions [ ] | undefined ,
423
459
libOptions : LibraryOptions | false ,
0 commit comments