diff --git a/src/build.ts b/src/build.ts index e82fbe2a..51231b0c 100644 --- a/src/build.ts +++ b/src/build.ts @@ -5,7 +5,8 @@ import consola from 'consola' import defu from 'defu' import prettyBytes from 'pretty-bytes' import jiti from 'jiti' -import { dumpObject, cleanDir } from './utils' +import mkdirp from 'mkdirp' +import { dumpObject, rmdir } from './utils' import type { BuildContext } from './types' import { validateDependencies } from './validate' import { rollupBuild } from './builder/rollup' @@ -85,7 +86,8 @@ export async function build (rootDir: string, stub: boolean) { // Clean dist dirs if (ctx.clean) { for (const dir of new Set(ctx.entries.map(e => e.outDir).sort())) { - await cleanDir(dir!) + await rmdir(dir!) + await mkdirp(dir!) } } diff --git a/src/builder/mkdist.ts b/src/builder/mkdist.ts index 7d8ea0d1..9e3b2650 100644 --- a/src/builder/mkdist.ts +++ b/src/builder/mkdist.ts @@ -1,11 +1,12 @@ import { mkdist } from 'mkdist' -import { symlink } from '../utils' +import { symlink, rmdir } from '../utils' import type { BuildContext } from '../types' export async function mkdistBuild (ctx: BuildContext) { for (const entry of ctx.entries.filter(e => e.builder === 'mkdist')) { const distDir = entry.outDir! if (ctx.stub) { + await rmdir(distDir) await symlink(entry.input, distDir) } else { const { writtenFiles } = await mkdist({ diff --git a/src/utils.ts b/src/utils.ts index 035fef00..10db75c1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,7 +2,7 @@ import fsp from 'fs/promises' import { promisify } from 'util' import { dirname } from 'upath' import mkdirp from 'mkdirp' -import rimraf from 'rimraf' +import _rimraf from 'rimraf' export async function ensuredir (path: string) { await mkdirp(dirname(path)) @@ -25,8 +25,9 @@ export function getpkg (id: string = '') { return s[0][0] === '@' ? `${s[0]}/${s[1]}` : s[0] } -export async function cleanDir (dir: string) { +const rimraf = promisify(_rimraf) + +export async function rmdir (dir: string) { await fsp.unlink(dir).catch(() => { }) - await promisify(rimraf)(dir) - await mkdirp(dir) + await rimraf(dir) }