1
- import fs from 'node:fs'
2
1
import path from 'node:path'
3
2
import colors from 'picocolors'
4
3
import type {
@@ -33,7 +32,6 @@ import type { RollupCommonJSOptions } from 'dep-types/commonjs'
33
32
import type { RollupDynamicImportVarsOptions } from 'dep-types/dynamicImportVars'
34
33
import type { EsbuildTarget } from 'types/internal/esbuildOptions'
35
34
import type { ChunkMetadata } from 'types/metadata'
36
- import { withTrailingSlash } from '../shared/utils'
37
35
import {
38
36
DEFAULT_ASSETS_INLINE_LIMIT ,
39
37
ESBUILD_MODULES_TARGET ,
@@ -54,14 +52,11 @@ import { type TerserOptions, terserPlugin } from './plugins/terser'
54
52
import {
55
53
arraify ,
56
54
asyncFlatten ,
57
- copyDir ,
58
55
createDebugger ,
59
56
displayTime ,
60
- emptyDir ,
61
57
getPkgName ,
62
58
joinUrlSegments ,
63
59
mergeWithDefaults ,
64
- normalizePath ,
65
60
partialEncodeURIPath ,
66
61
unique ,
67
62
} from './utils'
@@ -90,6 +85,7 @@ import {
90
85
import type { Plugin } from './plugin'
91
86
import type { RollupPluginHooks } from './typeUtils'
92
87
import { buildOxcPlugin } from './plugins/oxc'
88
+ import { prepareOutDirPlugin } from './plugins/prepareOutDir'
93
89
94
90
export interface BuildEnvironmentOptions {
95
91
/**
@@ -486,6 +482,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
486
482
return {
487
483
pre : [
488
484
completeSystemWrapPlugin ( ) ,
485
+ prepareOutDirPlugin ( ) ,
489
486
perEnvironmentPlugin (
490
487
'vite:rollup-options-plugins' ,
491
488
async ( environment ) =>
@@ -742,12 +739,6 @@ async function buildEnvironment(
742
739
}
743
740
}
744
741
745
- const outputBuildError = ( e : RollupError ) => {
746
- enhanceRollupError ( e )
747
- clearLine ( )
748
- logger . error ( e . message , { error : e } )
749
- }
750
-
751
742
const isSsrTargetWebworkerEnvironment =
752
743
environment . name === 'ssr' &&
753
744
environment . getTopLevelConfig ( ) . ssr ?. target === 'webworker'
@@ -849,22 +840,21 @@ async function buildEnvironment(
849
840
normalizedOutputs . push ( buildOutputOptions ( outputs ) )
850
841
}
851
842
852
- const resolvedOutDirs = getResolvedOutDirs (
853
- root ,
854
- options . outDir ,
855
- options . rollupOptions . output ,
856
- )
857
- const emptyOutDir = resolveEmptyOutDir (
858
- options . emptyOutDir ,
859
- root ,
860
- resolvedOutDirs ,
861
- logger ,
862
- )
863
-
864
843
// watch file changes with rollup
865
844
if ( options . watch ) {
866
845
logger . info ( colors . cyan ( `\nwatching for file changes...` ) )
867
846
847
+ const resolvedOutDirs = getResolvedOutDirs (
848
+ root ,
849
+ options . outDir ,
850
+ options . rollupOptions . output ,
851
+ )
852
+ const emptyOutDir = resolveEmptyOutDir (
853
+ options . emptyOutDir ,
854
+ root ,
855
+ resolvedOutDirs ,
856
+ logger ,
857
+ )
868
858
const resolvedChokidarOptions = resolveChokidarOptions (
869
859
// @ts -expect-error chokidar option does not exist in rolldown but used for backward compat
870
860
options . watch . chokidar ,
@@ -886,14 +876,14 @@ async function buildEnvironment(
886
876
watcher . on ( 'event' , ( event ) => {
887
877
if ( event . code === 'BUNDLE_START' ) {
888
878
logger . info ( colors . cyan ( `\nbuild started...` ) )
889
- if ( options . write ) {
890
- prepareOutDir ( resolvedOutDirs , emptyOutDir , environment )
891
- }
892
879
} else if ( event . code === 'BUNDLE_END' ) {
893
880
event . result . close ( )
894
881
logger . info ( colors . cyan ( `built in ${ event . duration } ms.` ) )
895
882
} else if ( event . code === 'ERROR' ) {
896
- outputBuildError ( event . error )
883
+ const e = event . error
884
+ enhanceRollupError ( e )
885
+ clearLine ( )
886
+ logger . error ( e . message , { error : e } )
897
887
}
898
888
} )
899
889
@@ -904,11 +894,6 @@ async function buildEnvironment(
904
894
const { rolldown } = await import ( 'rolldown' )
905
895
startTime = Date . now ( )
906
896
bundle = await rolldown ( rollupOptions )
907
-
908
- if ( options . write ) {
909
- prepareOutDir ( resolvedOutDirs , emptyOutDir , environment )
910
- }
911
-
912
897
const res : RolldownOutput [ ] = [ ]
913
898
for ( const output of normalizedOutputs ) {
914
899
res . push ( await bundle [ options . write ? 'write' : 'generate' ] ( output ) )
@@ -932,54 +917,6 @@ async function buildEnvironment(
932
917
}
933
918
}
934
919
935
- function prepareOutDir (
936
- outDirs : Set < string > ,
937
- emptyOutDir : boolean | null ,
938
- environment : BuildEnvironment ,
939
- ) {
940
- const { publicDir } = environment . config
941
- const outDirsArray = [ ...outDirs ]
942
- for ( const outDir of outDirs ) {
943
- if ( emptyOutDir !== false && fs . existsSync ( outDir ) ) {
944
- // skip those other outDirs which are nested in current outDir
945
- const skipDirs = outDirsArray
946
- . map ( ( dir ) => {
947
- const relative = path . relative ( outDir , dir )
948
- if (
949
- relative &&
950
- ! relative . startsWith ( '..' ) &&
951
- ! path . isAbsolute ( relative )
952
- ) {
953
- return relative
954
- }
955
- return ''
956
- } )
957
- . filter ( Boolean )
958
- emptyDir ( outDir , [ ...skipDirs , '.git' ] )
959
- }
960
- if (
961
- environment . config . build . copyPublicDir &&
962
- publicDir &&
963
- fs . existsSync ( publicDir )
964
- ) {
965
- if ( ! areSeparateFolders ( outDir , publicDir ) ) {
966
- environment . logger . warn (
967
- colors . yellow (
968
- `\n${ colors . bold (
969
- `(!)` ,
970
- ) } The public directory feature may not work correctly. outDir ${ colors . white (
971
- colors . dim ( outDir ) ,
972
- ) } and publicDir ${ colors . white (
973
- colors . dim ( publicDir ) ,
974
- ) } are not separate folders.\n`,
975
- ) ,
976
- )
977
- }
978
- copyDir ( publicDir , outDir )
979
- }
980
- }
981
- }
982
-
983
920
type JsExt = 'js' | 'cjs' | 'mjs'
984
921
985
922
function resolveOutputJsExtension (
@@ -1602,16 +1539,6 @@ export function toOutputFilePathWithoutRuntime(
1602
1539
export const toOutputFilePathInCss = toOutputFilePathWithoutRuntime
1603
1540
export const toOutputFilePathInHtml = toOutputFilePathWithoutRuntime
1604
1541
1605
- function areSeparateFolders ( a : string , b : string ) {
1606
- const na = normalizePath ( a )
1607
- const nb = normalizePath ( b )
1608
- return (
1609
- na !== nb &&
1610
- ! na . startsWith ( withTrailingSlash ( nb ) ) &&
1611
- ! nb . startsWith ( withTrailingSlash ( na ) )
1612
- )
1613
- }
1614
-
1615
1542
export class BuildEnvironment extends BaseEnvironment {
1616
1543
mode = 'build' as const
1617
1544
0 commit comments