1
- import fs from 'node:fs'
2
1
import path from 'node:path'
3
2
import colors from 'picocolors'
4
3
import type {
@@ -28,7 +27,6 @@ import type { RollupCommonJSOptions } from 'dep-types/commonjs'
28
27
import type { RollupDynamicImportVarsOptions } from 'dep-types/dynamicImportVars'
29
28
import type { EsbuildTarget } from 'types/internal/esbuildOptions'
30
29
import type { ChunkMetadata } from 'types/metadata'
31
- import { withTrailingSlash } from '../shared/utils'
32
30
import {
33
31
DEFAULT_ASSETS_INLINE_LIMIT ,
34
32
ESBUILD_BASELINE_WIDELY_AVAILABLE_TARGET ,
@@ -49,15 +47,12 @@ import { type TerserOptions, terserPlugin } from './plugins/terser'
49
47
import {
50
48
arraify ,
51
49
asyncFlatten ,
52
- copyDir ,
53
50
createDebugger ,
54
51
displayTime ,
55
- emptyDir ,
56
52
getPkgName ,
57
53
joinUrlSegments ,
58
54
mergeConfig ,
59
55
mergeWithDefaults ,
60
- normalizePath ,
61
56
partialEncodeURIPath ,
62
57
unique ,
63
58
} from './utils'
@@ -85,6 +80,7 @@ import {
85
80
BasicMinimalPluginContext ,
86
81
basePluginContextMeta ,
87
82
} from './server/pluginContainer'
83
+ import { prepareOutDirPlugin } from './plugins/prepareOutDir'
88
84
89
85
export interface BuildEnvironmentOptions {
90
86
/**
@@ -480,6 +476,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
480
476
return {
481
477
pre : [
482
478
completeSystemWrapPlugin ( ) ,
479
+ prepareOutDirPlugin ( ) ,
483
480
perEnvironmentPlugin (
484
481
'vite:rollup-options-plugins' ,
485
482
async ( environment ) =>
@@ -697,12 +694,6 @@ async function buildEnvironment(
697
694
}
698
695
}
699
696
700
- const outputBuildError = ( e : RollupError ) => {
701
- enhanceRollupError ( e )
702
- clearLine ( )
703
- logger . error ( e . message , { error : e } )
704
- }
705
-
706
697
const isSsrTargetWebworkerEnvironment =
707
698
environment . name === 'ssr' &&
708
699
environment . getTopLevelConfig ( ) . ssr ?. target === 'webworker'
@@ -804,22 +795,21 @@ async function buildEnvironment(
804
795
normalizedOutputs . push ( buildOutputOptions ( outputs ) )
805
796
}
806
797
807
- const resolvedOutDirs = getResolvedOutDirs (
808
- root ,
809
- options . outDir ,
810
- options . rollupOptions . output ,
811
- )
812
- const emptyOutDir = resolveEmptyOutDir (
813
- options . emptyOutDir ,
814
- root ,
815
- resolvedOutDirs ,
816
- logger ,
817
- )
818
-
819
798
// watch file changes with rollup
820
799
if ( options . watch ) {
821
800
logger . info ( colors . cyan ( `\nwatching for file changes...` ) )
822
801
802
+ const resolvedOutDirs = getResolvedOutDirs (
803
+ root ,
804
+ options . outDir ,
805
+ options . rollupOptions . output ,
806
+ )
807
+ const emptyOutDir = resolveEmptyOutDir (
808
+ options . emptyOutDir ,
809
+ root ,
810
+ resolvedOutDirs ,
811
+ logger ,
812
+ )
823
813
const resolvedChokidarOptions = resolveChokidarOptions (
824
814
// @ts -expect-error chokidar option does not exist in rolldown but used for backward compat
825
815
options . watch . chokidar ,
@@ -841,14 +831,14 @@ async function buildEnvironment(
841
831
watcher . on ( 'event' , ( event ) => {
842
832
if ( event . code === 'BUNDLE_START' ) {
843
833
logger . info ( colors . cyan ( `\nbuild started...` ) )
844
- if ( options . write ) {
845
- prepareOutDir ( resolvedOutDirs , emptyOutDir , environment )
846
- }
847
834
} else if ( event . code === 'BUNDLE_END' ) {
848
835
event . result . close ( )
849
836
logger . info ( colors . cyan ( `built in ${ event . duration } ms.` ) )
850
837
} else if ( event . code === 'ERROR' ) {
851
- outputBuildError ( event . error )
838
+ const e = event . error
839
+ enhanceRollupError ( e )
840
+ clearLine ( )
841
+ logger . error ( e . message , { error : e } )
852
842
}
853
843
} )
854
844
@@ -859,11 +849,6 @@ async function buildEnvironment(
859
849
const { rolldown } = await import ( 'rolldown' )
860
850
startTime = Date . now ( )
861
851
bundle = await rolldown ( rollupOptions )
862
-
863
- if ( options . write ) {
864
- prepareOutDir ( resolvedOutDirs , emptyOutDir , environment )
865
- }
866
-
867
852
const res : RolldownOutput [ ] = [ ]
868
853
for ( const output of normalizedOutputs ) {
869
854
res . push ( await bundle [ options . write ? 'write' : 'generate' ] ( output ) )
@@ -894,54 +879,6 @@ async function buildEnvironment(
894
879
}
895
880
}
896
881
897
- function prepareOutDir (
898
- outDirs : Set < string > ,
899
- emptyOutDir : boolean | null ,
900
- environment : BuildEnvironment ,
901
- ) {
902
- const { publicDir } = environment . config
903
- const outDirsArray = [ ...outDirs ]
904
- for ( const outDir of outDirs ) {
905
- if ( emptyOutDir !== false && fs . existsSync ( outDir ) ) {
906
- // skip those other outDirs which are nested in current outDir
907
- const skipDirs = outDirsArray
908
- . map ( ( dir ) => {
909
- const relative = path . relative ( outDir , dir )
910
- if (
911
- relative &&
912
- ! relative . startsWith ( '..' ) &&
913
- ! path . isAbsolute ( relative )
914
- ) {
915
- return relative
916
- }
917
- return ''
918
- } )
919
- . filter ( Boolean )
920
- emptyDir ( outDir , [ ...skipDirs , '.git' ] )
921
- }
922
- if (
923
- environment . config . build . copyPublicDir &&
924
- publicDir &&
925
- fs . existsSync ( publicDir )
926
- ) {
927
- if ( ! areSeparateFolders ( outDir , publicDir ) ) {
928
- environment . logger . warn (
929
- colors . yellow (
930
- `\n${ colors . bold (
931
- `(!)` ,
932
- ) } The public directory feature may not work correctly. outDir ${ colors . white (
933
- colors . dim ( outDir ) ,
934
- ) } and publicDir ${ colors . white (
935
- colors . dim ( publicDir ) ,
936
- ) } are not separate folders.\n`,
937
- ) ,
938
- )
939
- }
940
- copyDir ( publicDir , outDir )
941
- }
942
- }
943
- }
944
-
945
882
type JsExt = 'js' | 'cjs' | 'mjs'
946
883
947
884
function resolveOutputJsExtension (
@@ -1567,16 +1504,6 @@ export function toOutputFilePathWithoutRuntime(
1567
1504
export const toOutputFilePathInCss = toOutputFilePathWithoutRuntime
1568
1505
export const toOutputFilePathInHtml = toOutputFilePathWithoutRuntime
1569
1506
1570
- function areSeparateFolders ( a : string , b : string ) {
1571
- const na = normalizePath ( a )
1572
- const nb = normalizePath ( b )
1573
- return (
1574
- na !== nb &&
1575
- ! na . startsWith ( withTrailingSlash ( nb ) ) &&
1576
- ! nb . startsWith ( withTrailingSlash ( na ) )
1577
- )
1578
- }
1579
-
1580
1507
export class BuildEnvironment extends BaseEnvironment {
1581
1508
mode = 'build' as const
1582
1509
0 commit comments