1
- import fs from 'node:fs'
2
1
import path from 'node:path'
3
2
import colors from 'picocolors'
4
3
import type {
@@ -24,7 +23,6 @@ import commonjsPlugin from '@rollup/plugin-commonjs'
24
23
import type { RollupCommonJSOptions } from 'dep-types/commonjs'
25
24
import type { RollupDynamicImportVarsOptions } from 'dep-types/dynamicImportVars'
26
25
import type { TransformOptions } from 'esbuild'
27
- import { withTrailingSlash } from '../shared/utils'
28
26
import {
29
27
DEFAULT_ASSETS_INLINE_LIMIT ,
30
28
ESBUILD_BASELINE_WIDELY_AVAILABLE_TARGET ,
@@ -45,15 +43,12 @@ import { type TerserOptions, terserPlugin } from './plugins/terser'
45
43
import {
46
44
arraify ,
47
45
asyncFlatten ,
48
- copyDir ,
49
46
createDebugger ,
50
47
displayTime ,
51
- emptyDir ,
52
48
getPkgName ,
53
49
joinUrlSegments ,
54
50
mergeConfig ,
55
51
mergeWithDefaults ,
56
- normalizePath ,
57
52
partialEncodeURIPath ,
58
53
} from './utils'
59
54
import { perEnvironmentPlugin } from './plugin'
@@ -80,6 +75,7 @@ import {
80
75
BasicMinimalPluginContext ,
81
76
basePluginContextMeta ,
82
77
} from './server/pluginContainer'
78
+ import { prepareOutDirPlugin } from './plugins/prepareOutDir'
83
79
84
80
export interface BuildEnvironmentOptions {
85
81
/**
@@ -466,6 +462,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
466
462
return {
467
463
pre : [
468
464
completeSystemWrapPlugin ( ) ,
465
+ prepareOutDirPlugin ( ) ,
469
466
perEnvironmentPlugin ( 'commonjs' , ( environment ) => {
470
467
const { commonjsOptions } = environment . config . build
471
468
const usePluginCommonjs =
@@ -665,12 +662,6 @@ async function buildEnvironment(
665
662
}
666
663
}
667
664
668
- const outputBuildError = ( e : RollupError ) => {
669
- enhanceRollupError ( e )
670
- clearLine ( )
671
- logger . error ( e . message , { error : e } )
672
- }
673
-
674
665
const isSsrTargetWebworkerEnvironment =
675
666
environment . name === 'ssr' &&
676
667
environment . getTopLevelConfig ( ) . ssr ?. target === 'webworker'
@@ -766,22 +757,21 @@ async function buildEnvironment(
766
757
normalizedOutputs . push ( buildOutputOptions ( outputs ) )
767
758
}
768
759
769
- const resolvedOutDirs = getResolvedOutDirs (
770
- root ,
771
- options . outDir ,
772
- options . rollupOptions . output ,
773
- )
774
- const emptyOutDir = resolveEmptyOutDir (
775
- options . emptyOutDir ,
776
- root ,
777
- resolvedOutDirs ,
778
- logger ,
779
- )
780
-
781
760
// watch file changes with rollup
782
761
if ( options . watch ) {
783
762
logger . info ( colors . cyan ( `\nwatching for file changes...` ) )
784
763
764
+ const resolvedOutDirs = getResolvedOutDirs (
765
+ root ,
766
+ options . outDir ,
767
+ options . rollupOptions . output ,
768
+ )
769
+ const emptyOutDir = resolveEmptyOutDir (
770
+ options . emptyOutDir ,
771
+ root ,
772
+ resolvedOutDirs ,
773
+ logger ,
774
+ )
785
775
const resolvedChokidarOptions = resolveChokidarOptions (
786
776
options . watch . chokidar ,
787
777
resolvedOutDirs ,
@@ -802,14 +792,14 @@ async function buildEnvironment(
802
792
watcher . on ( 'event' , ( event ) => {
803
793
if ( event . code === 'BUNDLE_START' ) {
804
794
logger . info ( colors . cyan ( `\nbuild started...` ) )
805
- if ( options . write ) {
806
- prepareOutDir ( resolvedOutDirs , emptyOutDir , environment )
807
- }
808
795
} else if ( event . code === 'BUNDLE_END' ) {
809
796
event . result . close ( )
810
797
logger . info ( colors . cyan ( `built in ${ event . duration } ms.` ) )
811
798
} else if ( event . code === 'ERROR' ) {
812
- outputBuildError ( event . error )
799
+ const e = event . error
800
+ enhanceRollupError ( e )
801
+ clearLine ( )
802
+ logger . error ( e . message , { error : e } )
813
803
}
814
804
} )
815
805
@@ -821,10 +811,6 @@ async function buildEnvironment(
821
811
startTime = Date . now ( )
822
812
bundle = await rollup ( rollupOptions )
823
813
824
- if ( options . write ) {
825
- prepareOutDir ( resolvedOutDirs , emptyOutDir , environment )
826
- }
827
-
828
814
const res : RollupOutput [ ] = [ ]
829
815
for ( const output of normalizedOutputs ) {
830
816
res . push ( await bundle [ options . write ? 'write' : 'generate' ] ( output ) )
@@ -848,54 +834,6 @@ async function buildEnvironment(
848
834
}
849
835
}
850
836
851
- function prepareOutDir (
852
- outDirs : Set < string > ,
853
- emptyOutDir : boolean | null ,
854
- environment : BuildEnvironment ,
855
- ) {
856
- const { publicDir } = environment . config
857
- const outDirsArray = [ ...outDirs ]
858
- for ( const outDir of outDirs ) {
859
- if ( emptyOutDir !== false && fs . existsSync ( outDir ) ) {
860
- // skip those other outDirs which are nested in current outDir
861
- const skipDirs = outDirsArray
862
- . map ( ( dir ) => {
863
- const relative = path . relative ( outDir , dir )
864
- if (
865
- relative &&
866
- ! relative . startsWith ( '..' ) &&
867
- ! path . isAbsolute ( relative )
868
- ) {
869
- return relative
870
- }
871
- return ''
872
- } )
873
- . filter ( Boolean )
874
- emptyDir ( outDir , [ ...skipDirs , '.git' ] )
875
- }
876
- if (
877
- environment . config . build . copyPublicDir &&
878
- publicDir &&
879
- fs . existsSync ( publicDir )
880
- ) {
881
- if ( ! areSeparateFolders ( outDir , publicDir ) ) {
882
- environment . logger . warn (
883
- colors . yellow (
884
- `\n${ colors . bold (
885
- `(!)` ,
886
- ) } The public directory feature may not work correctly. outDir ${ colors . white (
887
- colors . dim ( outDir ) ,
888
- ) } and publicDir ${ colors . white (
889
- colors . dim ( publicDir ) ,
890
- ) } are not separate folders.\n`,
891
- ) ,
892
- )
893
- }
894
- copyDir ( publicDir , outDir )
895
- }
896
- }
897
- }
898
-
899
837
type JsExt = 'js' | 'cjs' | 'mjs'
900
838
901
839
function resolveOutputJsExtension (
@@ -1458,16 +1396,6 @@ export function toOutputFilePathWithoutRuntime(
1458
1396
export const toOutputFilePathInCss = toOutputFilePathWithoutRuntime
1459
1397
export const toOutputFilePathInHtml = toOutputFilePathWithoutRuntime
1460
1398
1461
- function areSeparateFolders ( a : string , b : string ) {
1462
- const na = normalizePath ( a )
1463
- const nb = normalizePath ( b )
1464
- return (
1465
- na !== nb &&
1466
- ! na . startsWith ( withTrailingSlash ( nb ) ) &&
1467
- ! nb . startsWith ( withTrailingSlash ( na ) )
1468
- )
1469
- }
1470
-
1471
1399
export class BuildEnvironment extends BaseEnvironment {
1472
1400
mode = 'build' as const
1473
1401
0 commit comments