Skip to content

Commit

Permalink
fix: disable emptying if there is any outDir outside root
Browse files Browse the repository at this point in the history
  • Loading branch information
qmhc committed Sep 11, 2022
1 parent 8fbd43c commit 6eae0c8
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,12 @@ function prepareOutDir(
emptyOutDir: boolean | null,
config: ResolvedConfig
) {
for (const outDir of new Set(outDirs)) {
if (fs.existsSync(outDir)) {
const nonDuplicateDirs = new Set(outDirs)
let outside = false
if (emptyOutDir == null) {
for (const outDir of nonDuplicateDirs) {
if (
emptyOutDir == null &&
fs.existsSync(outDir) &&
!normalizePath(outDir).startsWith(config.root + '/')
) {
// warn if outDir is outside of root
Expand All @@ -582,24 +584,29 @@ function prepareOutDir(
`Use --emptyOutDir to override.\n`
)
)
} else if (emptyOutDir !== false) {
// skip those other outDirs which are nested in current outDir
const skipDirs = outDirs
.map((dir) => {
const relative = path.relative(outDir, dir)
if (
relative &&
!relative.startsWith('..') &&
!path.isAbsolute(relative)
) {
return relative
}
return ''
})
.filter(Boolean)
emptyDir(outDir, [...skipDirs, '.git'])
outside = true
break
}
}
}
for (const outDir of nonDuplicateDirs) {
if (!outside && emptyOutDir !== false && fs.existsSync(outDir)) {
// skip those other outDirs which are nested in current outDir
const skipDirs = outDirs
.map((dir) => {
const relative = path.relative(outDir, dir)
if (
relative &&
!relative.startsWith('..') &&
!path.isAbsolute(relative)
) {
return relative
}
return ''
})
.filter(Boolean)
emptyDir(outDir, [...skipDirs, '.git'])
}
if (config.publicDir && fs.existsSync(config.publicDir)) {
copyDir(config.publicDir, outDir)
}
Expand Down

0 comments on commit 6eae0c8

Please sign in to comment.