Skip to content

Commit

Permalink
fix: clean dist directories only once for multi builds
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 18, 2023
1 parent f40a889 commit 11c47c8
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export async function build(
tryRequire("./package.json", rootDir) || {};

// Invoke build for every build config defined in build.config.ts
const cleanedDirs: string[] = [];
for (const buildConfig of buildConfigs) {
await _build(rootDir, stub, inputConfig, buildConfig, pkg);
await _build(rootDir, stub, inputConfig, buildConfig, pkg, cleanedDirs);
}
}

Expand All @@ -50,6 +51,7 @@ async function _build(
inputConfig: BuildConfig = {},
buildConfig: BuildConfig,
pkg: PackageJson & Record<"unbuild" | "build", BuildConfig>,
cleanedDirs: string[],
) {
// Resolve preset
const preset = resolvePreset(
Expand Down Expand Up @@ -207,9 +209,21 @@ async function _build(

// Clean dist dirs
if (options.clean) {
for (const dir of new Set(options.entries.map((e) => e.outDir).sort())) {
await rmdir(dir!);
await fsp.mkdir(dir!, { recursive: true });
for (const dir of new Set(
options.entries
.map((e) => e.outDir)
.filter(Boolean)
.sort() as unknown as Set<string>,
)) {
if (cleanedDirs.some((c) => dir.startsWith(c))) {
continue;
}
cleanedDirs.push(dir);
consola.info(
`Cleaning dist directory: \`./${relative(process.cwd(), dir)}\``,
);
await rmdir(dir);
await fsp.mkdir(dir, { recursive: true });
}
}

Expand Down

0 comments on commit 11c47c8

Please sign in to comment.