Skip to content

Commit

Permalink
fix(emptyOutDir): never remove .git (vitejs#3043)
Browse files Browse the repository at this point in the history
..and empty the out dir in `build --watch` mode when `emptyOutDir` is null.
  • Loading branch information
aleclarson authored and TobiasMelen committed May 3, 2021
1 parent 70ce0d0 commit cf57077
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
58 changes: 30 additions & 28 deletions packages/vite/src/node/build.ts
Expand Up @@ -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()
Expand All @@ -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)) {
Expand All @@ -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']) || `{}`)

Expand Down
12 changes: 8 additions & 4 deletions packages/vite/src/node/utils.ts
Expand Up @@ -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()) {
Expand Down

0 comments on commit cf57077

Please sign in to comment.