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 ) =>
@@ -706,12 +703,6 @@ async function buildEnvironment(
706
703
}
707
704
}
708
705
709
- const outputBuildError = ( e : RollupError ) => {
710
- enhanceRollupError ( e )
711
- clearLine ( )
712
- logger . error ( e . message , { error : e } )
713
- }
714
-
715
706
const isSsrTargetWebworkerEnvironment =
716
707
environment . name === 'ssr' &&
717
708
environment . getTopLevelConfig ( ) . ssr ?. target === 'webworker'
@@ -814,22 +805,21 @@ async function buildEnvironment(
814
805
normalizedOutputs . push ( buildOutputOptions ( outputs ) )
815
806
}
816
807
817
- const resolvedOutDirs = getResolvedOutDirs (
818
- root ,
819
- options . outDir ,
820
- options . rollupOptions . output ,
821
- )
822
- const emptyOutDir = resolveEmptyOutDir (
823
- options . emptyOutDir ,
824
- root ,
825
- resolvedOutDirs ,
826
- logger ,
827
- )
828
-
829
808
// watch file changes with rollup
830
809
if ( options . watch ) {
831
810
logger . info ( colors . cyan ( `\nwatching for file changes...` ) )
832
811
812
+ const resolvedOutDirs = getResolvedOutDirs (
813
+ root ,
814
+ options . outDir ,
815
+ options . rollupOptions . output ,
816
+ )
817
+ const emptyOutDir = resolveEmptyOutDir (
818
+ options . emptyOutDir ,
819
+ root ,
820
+ resolvedOutDirs ,
821
+ logger ,
822
+ )
833
823
const resolvedChokidarOptions = resolveChokidarOptions (
834
824
// @ts -expect-error chokidar option does not exist in rolldown but used for backward compat
835
825
options . watch . chokidar ,
@@ -851,14 +841,14 @@ async function buildEnvironment(
851
841
watcher . on ( 'event' , ( event ) => {
852
842
if ( event . code === 'BUNDLE_START' ) {
853
843
logger . info ( colors . cyan ( `\nbuild started...` ) )
854
- if ( options . write ) {
855
- prepareOutDir ( resolvedOutDirs , emptyOutDir , environment )
856
- }
857
844
} else if ( event . code === 'BUNDLE_END' ) {
858
845
event . result . close ( )
859
846
logger . info ( colors . cyan ( `built in ${ event . duration } ms.` ) )
860
847
} else if ( event . code === 'ERROR' ) {
861
- outputBuildError ( event . error )
848
+ const e = event . error
849
+ enhanceRollupError ( e )
850
+ clearLine ( )
851
+ logger . error ( e . message , { error : e } )
862
852
}
863
853
} )
864
854
@@ -869,11 +859,6 @@ async function buildEnvironment(
869
859
const { rolldown } = await import ( 'rolldown' )
870
860
startTime = Date . now ( )
871
861
bundle = await rolldown ( rollupOptions )
872
-
873
- if ( options . write ) {
874
- prepareOutDir ( resolvedOutDirs , emptyOutDir , environment )
875
- }
876
-
877
862
const res : RolldownOutput [ ] = [ ]
878
863
for ( const output of normalizedOutputs ) {
879
864
res . push ( await bundle [ options . write ? 'write' : 'generate' ] ( output ) )
@@ -904,54 +889,6 @@ async function buildEnvironment(
904
889
}
905
890
}
906
891
907
- function prepareOutDir (
908
- outDirs : Set < string > ,
909
- emptyOutDir : boolean | null ,
910
- environment : BuildEnvironment ,
911
- ) {
912
- const { publicDir } = environment . config
913
- const outDirsArray = [ ...outDirs ]
914
- for ( const outDir of outDirs ) {
915
- if ( emptyOutDir !== false && fs . existsSync ( outDir ) ) {
916
- // skip those other outDirs which are nested in current outDir
917
- const skipDirs = outDirsArray
918
- . map ( ( dir ) => {
919
- const relative = path . relative ( outDir , dir )
920
- if (
921
- relative &&
922
- ! relative . startsWith ( '..' ) &&
923
- ! path . isAbsolute ( relative )
924
- ) {
925
- return relative
926
- }
927
- return ''
928
- } )
929
- . filter ( Boolean )
930
- emptyDir ( outDir , [ ...skipDirs , '.git' ] )
931
- }
932
- if (
933
- environment . config . build . copyPublicDir &&
934
- publicDir &&
935
- fs . existsSync ( publicDir )
936
- ) {
937
- if ( ! areSeparateFolders ( outDir , publicDir ) ) {
938
- environment . logger . warn (
939
- colors . yellow (
940
- `\n${ colors . bold (
941
- `(!)` ,
942
- ) } The public directory feature may not work correctly. outDir ${ colors . white (
943
- colors . dim ( outDir ) ,
944
- ) } and publicDir ${ colors . white (
945
- colors . dim ( publicDir ) ,
946
- ) } are not separate folders.\n`,
947
- ) ,
948
- )
949
- }
950
- copyDir ( publicDir , outDir )
951
- }
952
- }
953
- }
954
-
955
892
type JsExt = 'js' | 'cjs' | 'mjs'
956
893
957
894
function resolveOutputJsExtension (
@@ -1577,16 +1514,6 @@ export function toOutputFilePathWithoutRuntime(
1577
1514
export const toOutputFilePathInCss = toOutputFilePathWithoutRuntime
1578
1515
export const toOutputFilePathInHtml = toOutputFilePathWithoutRuntime
1579
1516
1580
- function areSeparateFolders ( a : string , b : string ) {
1581
- const na = normalizePath ( a )
1582
- const nb = normalizePath ( b )
1583
- return (
1584
- na !== nb &&
1585
- ! na . startsWith ( withTrailingSlash ( nb ) ) &&
1586
- ! nb . startsWith ( withTrailingSlash ( na ) )
1587
- )
1588
- }
1589
-
1590
1517
export class BuildEnvironment extends BaseEnvironment {
1591
1518
mode = 'build' as const
1592
1519
0 commit comments