Skip to content

Commit 11c47c8

Browse files
committed
fix: clean dist directories only once for multi builds
1 parent f40a889 commit 11c47c8

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/build.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ export async function build(
3939
tryRequire("./package.json", rootDir) || {};
4040

4141
// Invoke build for every build config defined in build.config.ts
42+
const cleanedDirs: string[] = [];
4243
for (const buildConfig of buildConfigs) {
43-
await _build(rootDir, stub, inputConfig, buildConfig, pkg);
44+
await _build(rootDir, stub, inputConfig, buildConfig, pkg, cleanedDirs);
4445
}
4546
}
4647

@@ -50,6 +51,7 @@ async function _build(
5051
inputConfig: BuildConfig = {},
5152
buildConfig: BuildConfig,
5253
pkg: PackageJson & Record<"unbuild" | "build", BuildConfig>,
54+
cleanedDirs: string[],
5355
) {
5456
// Resolve preset
5557
const preset = resolvePreset(
@@ -207,9 +209,21 @@ async function _build(
207209

208210
// Clean dist dirs
209211
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 });
213227
}
214228
}
215229

0 commit comments

Comments
 (0)