Skip to content

Commit

Permalink
feat: require explicit option to empty outDir when it is out of root
Browse files Browse the repository at this point in the history
close #1501
  • Loading branch information
yyx990803 committed Jan 12, 2021
1 parent a09a030 commit 730d2f0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ export default ({ command, mode }) => {

Set to `false` to disable writing the bundle to disk. This is mostly used in [programmatic `build()` calls](/guide/api-javascript#build) where further post processing of the bundle is needed before writing to disk.

### build.emptyOutDir

- **Type:** `boolean`
- **Default:** `true` if `outDir` is inside `root`

By default, Vite will empty the `outDir` on build if it is inside project root. It will emit a warning if `outDir` is outside of root to avoid accidentially removing important files. You can explicitly set this option to suppress the warning. This is also available via command line as `--emptyOutDir`.

## Dep Optimization Options

- **Related:** [Dependency Pre-Bundling](/guide/dep-pre-bundling)
Expand Down
1 change: 1 addition & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"debug": "^4.3.1",
"dotenv": "^8.2.0",
"dotenv-expand": "^5.1.0",
"enquirer": "^2.3.6",
"es-module-lexer": "^0.3.26",
"etag": "^1.8.1",
"execa": "^5.0.0",
Expand Down
28 changes: 26 additions & 2 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { buildHtmlPlugin } from './plugins/html'
import { buildEsbuildPlugin } from './plugins/esbuild'
import { terserPlugin } from './plugins/terser'
import { Terser } from 'types/terser'
import { copyDir, emptyDir, lookupFile } from './utils'
import { copyDir, emptyDir, lookupFile, normalizePath } from './utils'
import { manifestPlugin } from './plugins/manifest'
import commonjsPlugin from '@rollup/plugin-commonjs'
import { RollupCommonJSOptions } from 'types/commonjs'
Expand Down Expand Up @@ -118,6 +118,11 @@ export interface BuildOptions {
* @default true
*/
write?: boolean
/**
* Empty outDir on write.
* @default true when outDir is a sub directory of project root
*/
emptyOutDir?: boolean | null
/**
* Whether to emit a manifest.json under assets dir to map hash-less filenames
* to their hashed versions. Useful when you want to generate your own HTML
Expand Down Expand Up @@ -176,6 +181,7 @@ export function resolveBuildOptions(
terserOptions: {},
cleanCssOptions: {},
write: true,
emptyOutDir: null,
manifest: false,
lib: false,
ssr: false,
Expand Down Expand Up @@ -319,7 +325,25 @@ async function doBuild(
}

if (options.write) {
emptyDir(outDir)
// 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(publicDir)) {
copyDir(publicDir, outDir)
}
Expand Down
4 changes: 4 additions & 0 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ cli
`or specify minifier to use (default: terser)`
)
.option('--manifest', `[boolean] emit build manifest json`)
.option(
'--emptyOutDir',
`[boolean] force empty outDir when it's outside of root`
)
.option('-m, --mode <mode>', `[string] set env mode`)
.action(async (root: string, options: BuildOptions & GlobalCLIOptions) => {
const { build } = await import('./build')
Expand Down

0 comments on commit 730d2f0

Please sign in to comment.