@@ -559,28 +559,30 @@ async function doBuild(
559
559
libOptions ,
560
560
config . logger
561
561
)
562
+ const normalizedOutputs : OutputOptions [ ] = [ ]
563
+
564
+ if ( Array . isArray ( outputs ) ) {
565
+ for ( const resolvedOutput of outputs ) {
566
+ normalizedOutputs . push ( buildOutputOptions ( resolvedOutput ) )
567
+ }
568
+ } else {
569
+ normalizedOutputs . push ( buildOutputOptions ( outputs ) )
570
+ }
571
+
572
+ const outDirs = normalizedOutputs . map ( ( { dir } ) => resolve ( dir ! ) )
562
573
563
574
// watch file changes with rollup
564
575
if ( config . build . watch ) {
565
576
config . logger . info ( colors . cyan ( `\nwatching for file changes...` ) )
566
577
567
- const output : OutputOptions [ ] = [ ]
568
- if ( Array . isArray ( outputs ) ) {
569
- for ( const resolvedOutput of outputs ) {
570
- output . push ( buildOutputOptions ( resolvedOutput ) )
571
- }
572
- } else {
573
- output . push ( buildOutputOptions ( outputs ) )
574
- }
575
-
576
578
const resolvedChokidarOptions = resolveChokidarOptions (
577
579
config . build . watch . chokidar
578
580
)
579
581
580
582
const { watch } = await import ( 'rollup' )
581
583
const watcher = watch ( {
582
584
...rollupOptions ,
583
- output,
585
+ output : normalizedOutputs ,
584
586
watch : {
585
587
...config . build . watch ,
586
588
chokidar : resolvedChokidarOptions
@@ -591,7 +593,7 @@ async function doBuild(
591
593
if ( event . code === 'BUNDLE_START' ) {
592
594
config . logger . info ( colors . cyan ( `\nbuild started...` ) )
593
595
if ( options . write ) {
594
- prepareOutDir ( outDir , options . emptyOutDir , config )
596
+ prepareOutDir ( outDirs , options . emptyOutDir , config )
595
597
}
596
598
} else if ( event . code === 'BUNDLE_END' ) {
597
599
event . result . close ( )
@@ -610,55 +612,72 @@ async function doBuild(
610
612
parallelBuilds . push ( bundle )
611
613
612
614
const generate = ( output : OutputOptions = { } ) => {
613
- return bundle [ options . write ? 'write' : 'generate' ] (
614
- buildOutputOptions ( output )
615
- )
615
+ return bundle [ options . write ? 'write' : 'generate' ] ( output )
616
616
}
617
617
618
618
if ( options . write ) {
619
- prepareOutDir ( outDir , options . emptyOutDir , config )
619
+ prepareOutDir ( outDirs , options . emptyOutDir , config )
620
620
}
621
621
622
- if ( Array . isArray ( outputs ) ) {
623
- const res = [ ]
624
- for ( const output of outputs ) {
625
- res . push ( await generate ( output ) )
626
- }
627
- return res
628
- } else {
629
- return await generate ( outputs )
622
+ const res = [ ]
623
+ for ( const output of normalizedOutputs ) {
624
+ res . push ( await generate ( output ) )
630
625
}
626
+ return Array . isArray ( outputs ) ? res : res [ 0 ]
631
627
} catch ( e ) {
632
628
outputBuildError ( e )
633
629
throw e
634
630
}
635
631
}
636
632
637
633
function prepareOutDir (
638
- outDir : string ,
634
+ outDirs : string [ ] ,
639
635
emptyOutDir : boolean | null ,
640
636
config : ResolvedConfig
641
637
) {
642
- if ( fs . existsSync ( outDir ) ) {
643
- if (
644
- emptyOutDir == null &&
645
- ! normalizePath ( outDir ) . startsWith ( config . root + '/' )
646
- ) {
647
- // warn if outDir is outside of root
648
- config . logger . warn (
649
- colors . yellow (
650
- `\n${ colors . bold ( `(!)` ) } outDir ${ colors . white (
651
- colors . dim ( outDir )
652
- ) } is not inside project root and will not be emptied.\n` +
653
- `Use --emptyOutDir to override.\n`
638
+ const nonDuplicateDirs = new Set ( outDirs )
639
+ let outside = false
640
+ if ( emptyOutDir == null ) {
641
+ for ( const outDir of nonDuplicateDirs ) {
642
+ if (
643
+ fs . existsSync ( outDir ) &&
644
+ ! normalizePath ( outDir ) . startsWith ( config . root + '/' )
645
+ ) {
646
+ // warn if outDir is outside of root
647
+ config . logger . warn (
648
+ colors . yellow (
649
+ `\n${ colors . bold ( `(!)` ) } outDir ${ colors . white (
650
+ colors . dim ( outDir )
651
+ ) } is not inside project root and will not be emptied.\n` +
652
+ `Use --emptyOutDir to override.\n`
653
+ )
654
654
)
655
- )
656
- } else if ( emptyOutDir !== false ) {
657
- emptyDir ( outDir , [ '.git' ] )
655
+ outside = true
656
+ break
657
+ }
658
658
}
659
659
}
660
- if ( config . publicDir && fs . existsSync ( config . publicDir ) ) {
661
- copyDir ( config . publicDir , outDir )
660
+ for ( const outDir of nonDuplicateDirs ) {
661
+ if ( ! outside && emptyOutDir !== false && fs . existsSync ( outDir ) ) {
662
+ // skip those other outDirs which are nested in current outDir
663
+ const skipDirs = outDirs
664
+ . map ( ( dir ) => {
665
+ const relative = path . relative ( outDir , dir )
666
+ if (
667
+ relative &&
668
+ ! relative . startsWith ( '..' ) &&
669
+ ! path . isAbsolute ( relative )
670
+ ) {
671
+ return relative
672
+ }
673
+ return ''
674
+ } )
675
+ . filter ( Boolean )
676
+ emptyDir ( outDir , [ ...skipDirs , '.git' ] )
677
+ }
678
+ if ( config . publicDir && fs . existsSync ( config . publicDir ) ) {
679
+ copyDir ( config . publicDir , outDir )
680
+ }
662
681
}
663
682
}
664
683
0 commit comments