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_MODULES_TARGET ,
@@ -49,14 +47,11 @@ 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
mergeWithDefaults ,
59
- normalizePath ,
60
55
partialEncodeURIPath ,
61
56
unique ,
62
57
} from './utils'
@@ -84,6 +79,7 @@ import {
84
79
import type { Plugin } from './plugin'
85
80
import type { RollupPluginHooks } from './typeUtils'
86
81
import { buildOxcPlugin } from './plugins/oxc'
82
+ import { prepareOutDirPlugin } from './plugins/prepareOutDir'
87
83
88
84
export interface BuildEnvironmentOptions {
89
85
/**
@@ -479,6 +475,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
479
475
return {
480
476
pre : [
481
477
completeSystemWrapPlugin ( ) ,
478
+ prepareOutDirPlugin ( ) ,
482
479
perEnvironmentPlugin (
483
480
'vite:rollup-options-plugins' ,
484
481
async ( environment ) =>
@@ -693,12 +690,6 @@ async function buildEnvironment(
693
690
}
694
691
}
695
692
696
- const outputBuildError = ( e : RollupError ) => {
697
- enhanceRollupError ( e )
698
- clearLine ( )
699
- logger . error ( e . message , { error : e } )
700
- }
701
-
702
693
const isSsrTargetWebworkerEnvironment =
703
694
environment . name === 'ssr' &&
704
695
environment . getTopLevelConfig ( ) . ssr ?. target === 'webworker'
@@ -800,22 +791,21 @@ async function buildEnvironment(
800
791
normalizedOutputs . push ( buildOutputOptions ( outputs ) )
801
792
}
802
793
803
- const resolvedOutDirs = getResolvedOutDirs (
804
- root ,
805
- options . outDir ,
806
- options . rollupOptions . output ,
807
- )
808
- const emptyOutDir = resolveEmptyOutDir (
809
- options . emptyOutDir ,
810
- root ,
811
- resolvedOutDirs ,
812
- logger ,
813
- )
814
-
815
794
// watch file changes with rollup
816
795
if ( options . watch ) {
817
796
logger . info ( colors . cyan ( `\nwatching for file changes...` ) )
818
797
798
+ const resolvedOutDirs = getResolvedOutDirs (
799
+ root ,
800
+ options . outDir ,
801
+ options . rollupOptions . output ,
802
+ )
803
+ const emptyOutDir = resolveEmptyOutDir (
804
+ options . emptyOutDir ,
805
+ root ,
806
+ resolvedOutDirs ,
807
+ logger ,
808
+ )
819
809
const resolvedChokidarOptions = resolveChokidarOptions (
820
810
// @ts -expect-error chokidar option does not exist in rolldown but used for backward compat
821
811
options . watch . chokidar ,
@@ -837,14 +827,14 @@ async function buildEnvironment(
837
827
watcher . on ( 'event' , ( event ) => {
838
828
if ( event . code === 'BUNDLE_START' ) {
839
829
logger . info ( colors . cyan ( `\nbuild started...` ) )
840
- if ( options . write ) {
841
- prepareOutDir ( resolvedOutDirs , emptyOutDir , environment )
842
- }
843
830
} else if ( event . code === 'BUNDLE_END' ) {
844
831
event . result . close ( )
845
832
logger . info ( colors . cyan ( `built in ${ event . duration } ms.` ) )
846
833
} else if ( event . code === 'ERROR' ) {
847
- outputBuildError ( event . error )
834
+ const e = event . error
835
+ enhanceRollupError ( e )
836
+ clearLine ( )
837
+ logger . error ( e . message , { error : e } )
848
838
}
849
839
} )
850
840
@@ -855,11 +845,6 @@ async function buildEnvironment(
855
845
const { rolldown } = await import ( 'rolldown' )
856
846
startTime = Date . now ( )
857
847
bundle = await rolldown ( rollupOptions )
858
-
859
- if ( options . write ) {
860
- prepareOutDir ( resolvedOutDirs , emptyOutDir , environment )
861
- }
862
-
863
848
const res : RolldownOutput [ ] = [ ]
864
849
for ( const output of normalizedOutputs ) {
865
850
res . push ( await bundle [ options . write ? 'write' : 'generate' ] ( output ) )
@@ -883,54 +868,6 @@ async function buildEnvironment(
883
868
}
884
869
}
885
870
886
- function prepareOutDir (
887
- outDirs : Set < string > ,
888
- emptyOutDir : boolean | null ,
889
- environment : BuildEnvironment ,
890
- ) {
891
- const { publicDir } = environment . config
892
- const outDirsArray = [ ...outDirs ]
893
- for ( const outDir of outDirs ) {
894
- if ( emptyOutDir !== false && fs . existsSync ( outDir ) ) {
895
- // skip those other outDirs which are nested in current outDir
896
- const skipDirs = outDirsArray
897
- . map ( ( dir ) => {
898
- const relative = path . relative ( outDir , dir )
899
- if (
900
- relative &&
901
- ! relative . startsWith ( '..' ) &&
902
- ! path . isAbsolute ( relative )
903
- ) {
904
- return relative
905
- }
906
- return ''
907
- } )
908
- . filter ( Boolean )
909
- emptyDir ( outDir , [ ...skipDirs , '.git' ] )
910
- }
911
- if (
912
- environment . config . build . copyPublicDir &&
913
- publicDir &&
914
- fs . existsSync ( publicDir )
915
- ) {
916
- if ( ! areSeparateFolders ( outDir , publicDir ) ) {
917
- environment . logger . warn (
918
- colors . yellow (
919
- `\n${ colors . bold (
920
- `(!)` ,
921
- ) } The public directory feature may not work correctly. outDir ${ colors . white (
922
- colors . dim ( outDir ) ,
923
- ) } and publicDir ${ colors . white (
924
- colors . dim ( publicDir ) ,
925
- ) } are not separate folders.\n`,
926
- ) ,
927
- )
928
- }
929
- copyDir ( publicDir , outDir )
930
- }
931
- }
932
- }
933
-
934
871
type JsExt = 'js' | 'cjs' | 'mjs'
935
872
936
873
function resolveOutputJsExtension (
@@ -1553,16 +1490,6 @@ export function toOutputFilePathWithoutRuntime(
1553
1490
export const toOutputFilePathInCss = toOutputFilePathWithoutRuntime
1554
1491
export const toOutputFilePathInHtml = toOutputFilePathWithoutRuntime
1555
1492
1556
- function areSeparateFolders ( a : string , b : string ) {
1557
- const na = normalizePath ( a )
1558
- const nb = normalizePath ( b )
1559
- return (
1560
- na !== nb &&
1561
- ! na . startsWith ( withTrailingSlash ( nb ) ) &&
1562
- ! nb . startsWith ( withTrailingSlash ( na ) )
1563
- )
1564
- }
1565
-
1566
1493
export class BuildEnvironment extends BaseEnvironment {
1567
1494
mode = 'build' as const
1568
1495
0 commit comments