From 82dc5880c97daca2b7b8d55c27c30a6d810849a1 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Mon, 26 Apr 2021 16:07:56 -0400 Subject: [PATCH] fix(emptyOutDir): never remove .git (#3043) ..and empty the out dir in `build --watch` mode when `emptyOutDir` is null. --- packages/vite/src/node/build.ts | 58 +++++++++++++++++---------------- packages/vite/src/node/utils.ts | 12 ++++--- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 40c753ff6f9271..825e627b06fbb9 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -471,13 +471,8 @@ async function doBuild( watcher.on('event', (event) => { if (event.code === 'BUNDLE_START') { config.logger.info(chalk.cyanBright(`\nbuild started...`)) - - // clean previous files if (options.write) { - emptyDir(outDir) - if (fs.existsSync(config.publicDir)) { - copyDir(config.publicDir, outDir) - } + prepareOutDir(outDir, options.emptyOutDir, config) } } else if (event.code === 'BUNDLE_END') { event.result.close() @@ -504,28 +499,7 @@ async function doBuild( } if (options.write) { - // warn if outDir is outside of root - if (fs.existsSync(outDir)) { - const inferEmpty = options.emptyOutDir === null - if ( - options.emptyOutDir || - (inferEmpty && normalizePath(outDir).startsWith(config.root + '/')) - ) { - emptyDir(outDir) - } else if (inferEmpty) { - config.logger.warn( - chalk.yellow( - `\n${chalk.bold(`(!)`)} outDir ${chalk.white.dim( - outDir - )} is not inside project root and will not be emptied.\n` + - `Use --emptyOutDir to override.\n` - ) - ) - } - } - if (fs.existsSync(config.publicDir)) { - copyDir(config.publicDir, outDir) - } + prepareOutDir(outDir, options.emptyOutDir, config) } if (Array.isArray(outputs)) { @@ -543,6 +517,34 @@ async function doBuild( } } +function prepareOutDir( + outDir: string, + emptyOutDir: boolean | null, + config: ResolvedConfig +) { + if (fs.existsSync(outDir)) { + if ( + emptyOutDir == null && + !normalizePath(outDir).startsWith(config.root + '/') + ) { + // warn if outDir is outside of root + config.logger.warn( + chalk.yellow( + `\n${chalk.bold(`(!)`)} outDir ${chalk.white.dim( + outDir + )} is not inside project root and will not be emptied.\n` + + `Use --emptyOutDir to override.\n` + ) + ) + } else if (emptyOutDir !== false) { + emptyDir(outDir, ['.git']) + } + } + if (fs.existsSync(config.publicDir)) { + copyDir(config.publicDir, outDir) + } +} + function getPkgName(root: string) { const { name } = JSON.parse(lookupFile(root, ['package.json']) || `{}`) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 1cb6c9c6d90569..033285c290943a 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -323,11 +323,15 @@ export function writeFile( fs.writeFileSync(filename, content) } -export function emptyDir(dir: string): void { - if (!fs.existsSync(dir)) { - return - } +/** + * Delete every file and subdirectory. **The given directory must exist.** + * Pass an optional `skip` array to preserve files in the root directory. + */ +export function emptyDir(dir: string, skip?: string[]): void { for (const file of fs.readdirSync(dir)) { + if (skip?.includes(file)) { + continue + } const abs = path.resolve(dir, file) // baseline is Node 12 so can't use rmSync :( if (fs.lstatSync(abs).isDirectory()) {