@@ -39,8 +39,9 @@ export async function build(
39
39
tryRequire ( "./package.json" , rootDir ) || { } ;
40
40
41
41
// Invoke build for every build config defined in build.config.ts
42
+ const cleanedDirs : string [ ] = [ ] ;
42
43
for ( const buildConfig of buildConfigs ) {
43
- await _build ( rootDir , stub , inputConfig , buildConfig , pkg ) ;
44
+ await _build ( rootDir , stub , inputConfig , buildConfig , pkg , cleanedDirs ) ;
44
45
}
45
46
}
46
47
@@ -50,6 +51,7 @@ async function _build(
50
51
inputConfig : BuildConfig = { } ,
51
52
buildConfig : BuildConfig ,
52
53
pkg : PackageJson & Record < "unbuild" | "build" , BuildConfig > ,
54
+ cleanedDirs : string [ ] ,
53
55
) {
54
56
// Resolve preset
55
57
const preset = resolvePreset (
@@ -207,9 +209,21 @@ async function _build(
207
209
208
210
// Clean dist dirs
209
211
if ( options . clean ) {
210
- for ( const dir of new Set ( options . entries . map ( ( e ) => e . outDir ) . sort ( ) ) ) {
211
- await rmdir ( dir ! ) ;
212
- await fsp . mkdir ( dir ! , { recursive : true } ) ;
212
+ for ( const dir of new Set (
213
+ options . entries
214
+ . map ( ( e ) => e . outDir )
215
+ . filter ( Boolean )
216
+ . sort ( ) as unknown as Set < string > ,
217
+ ) ) {
218
+ if ( cleanedDirs . some ( ( c ) => dir . startsWith ( c ) ) ) {
219
+ continue ;
220
+ }
221
+ cleanedDirs . push ( dir ) ;
222
+ consola . info (
223
+ `Cleaning dist directory: \`./${ relative ( process . cwd ( ) , dir ) } \`` ,
224
+ ) ;
225
+ await rmdir ( dir ) ;
226
+ await fsp . mkdir ( dir , { recursive : true } ) ;
213
227
}
214
228
}
215
229
0 commit comments