|
| 1 | +# Build Options |
| 2 | + |
| 3 | +## build.target |
| 4 | + |
| 5 | +- **Type:** `string | string[]` |
| 6 | +- **Default:** `'modules'` |
| 7 | +- **Related:** [Browser Compatibility](/guide/build#browser-compatibility) |
| 8 | + |
| 9 | +Browser compatibility target for the final bundle. The default value is a Vite special value, `'modules'`, which targets browsers with [native ES Modules](https://caniuse.com/es6-module) and [native ESM dynamic import](https://caniuse.com/es6-module-dynamic-import) support. |
| 10 | + |
| 11 | +Another special value is `'esnext'` - which assumes native dynamic imports support and will transpile as little as possible: |
| 12 | + |
| 13 | +- If the [`build.minify`](#build-minify) option is `'terser'`, `'esnext'` will be forced down to `'es2021'`. |
| 14 | +- In other cases, it will perform no transpilation at all. |
| 15 | + |
| 16 | +The transform is performed with esbuild and the value should be a valid [esbuild target option](https://esbuild.github.io/api/#target). Custom targets can either be a ES version (e.g. `es2015`), a browser with version (e.g. `chrome58`), or an array of multiple target strings. |
| 17 | + |
| 18 | +Note the build will fail if the code contains features that cannot be safely transpiled by esbuild. See [esbuild docs](https://esbuild.github.io/content-types/#javascript) for more details. |
| 19 | + |
| 20 | +## build.polyfillModulePreload |
| 21 | + |
| 22 | +- **Type:** `boolean` |
| 23 | +- **Default:** `true` |
| 24 | + |
| 25 | +Whether to automatically inject [module preload polyfill](https://guybedford.com/es-module-preloading-integrity#modulepreload-polyfill). |
| 26 | + |
| 27 | +If set to `true`, the polyfill is auto injected into the proxy module of each `index.html` entry. If the build is configured to use a non-html custom entry via `build.rollupOptions.input`, then it is necessary to manually import the polyfill in your custom entry: |
| 28 | + |
| 29 | +```js |
| 30 | +import 'vite/modulepreload-polyfill' |
| 31 | +``` |
| 32 | + |
| 33 | +Note: the polyfill does **not** apply to [Library Mode](/guide/build#library-mode). If you need to support browsers without native dynamic import, you should probably avoid using it in your library. |
| 34 | + |
| 35 | +## build.outDir |
| 36 | + |
| 37 | +- **Type:** `string` |
| 38 | +- **Default:** `dist` |
| 39 | + |
| 40 | +Specify the output directory (relative to [project root](/guide/#index-html-and-project-root)). |
| 41 | + |
| 42 | +## build.assetsDir |
| 43 | + |
| 44 | +- **Type:** `string` |
| 45 | +- **Default:** `assets` |
| 46 | + |
| 47 | +Specify the directory to nest generated assets under (relative to `build.outDir`). |
| 48 | + |
| 49 | +## build.assetsInlineLimit |
| 50 | + |
| 51 | +- **Type:** `number` |
| 52 | +- **Default:** `4096` (4kb) |
| 53 | + |
| 54 | +Imported or referenced assets that are smaller than this threshold will be inlined as base64 URLs to avoid extra http requests. Set to `0` to disable inlining altogether. |
| 55 | + |
| 56 | +::: tip Note |
| 57 | +If you specify `build.lib`, `build.assetsInlineLimit` will be ignored and assets will always be inlined, regardless of file size. |
| 58 | +::: |
| 59 | + |
| 60 | +## build.cssCodeSplit |
| 61 | + |
| 62 | +- **Type:** `boolean` |
| 63 | +- **Default:** `true` |
| 64 | + |
| 65 | +Enable/disable CSS code splitting. When enabled, CSS imported in async chunks will be inlined into the async chunk itself and inserted when the chunk is loaded. |
| 66 | + |
| 67 | +If disabled, all CSS in the entire project will be extracted into a single CSS file. |
| 68 | + |
| 69 | +::: tip Note |
| 70 | +If you specify `build.lib`, `build.cssCodeSplit` will be `false` as default. |
| 71 | +::: |
| 72 | + |
| 73 | +## build.cssTarget |
| 74 | + |
| 75 | +- **Type:** `string | string[]` |
| 76 | +- **Default:** the same as [`build.target`](/config/#build-target) |
| 77 | + |
| 78 | +This options allows users to set a different browser target for CSS minification from the one used for JavaScript transpilation. |
| 79 | + |
| 80 | +It should only be used when you are targeting a non-mainstream browser. |
| 81 | +One example is Android WeChat WebView, which supports most modern JavaScript features but not the [`#RGBA` hexadecimal color notation in CSS](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#rgb_colors). |
| 82 | +In this case, you need to set `build.cssTarget` to `chrome61` to prevent vite from transform `rgba()` colors into `#RGBA` hexadecimal notations. |
| 83 | + |
| 84 | +## build.sourcemap |
| 85 | + |
| 86 | +- **Type:** `boolean | 'inline' | 'hidden'` |
| 87 | +- **Default:** `false` |
| 88 | + |
| 89 | +Generate production source maps. If `true`, a separate sourcemap file will be created. If `'inline'`, the sourcemap will be appended to the resulting output file as a data URI. `'hidden'` works like `true` except that the corresponding sourcemap comments in the bundled files are suppressed. |
| 90 | + |
| 91 | +## build.rollupOptions |
| 92 | + |
| 93 | +- **Type:** [`RollupOptions`](https://rollupjs.org/guide/en/#big-list-of-options) |
| 94 | + |
| 95 | +Directly customize the underlying Rollup bundle. This is the same as options that can be exported from a Rollup config file and will be merged with Vite's internal Rollup options. See [Rollup options docs](https://rollupjs.org/guide/en/#big-list-of-options) for more details. |
| 96 | + |
| 97 | +## build.commonjsOptions |
| 98 | + |
| 99 | +- **Type:** [`RollupCommonJSOptions`](https://github.com/rollup/plugins/tree/master/packages/commonjs#options) |
| 100 | + |
| 101 | +Options to pass on to [@rollup/plugin-commonjs](https://github.com/rollup/plugins/tree/master/packages/commonjs). |
| 102 | + |
| 103 | +## build.dynamicImportVarsOptions |
| 104 | + |
| 105 | +- **Type:** [`RollupDynamicImportVarsOptions`](https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#options) |
| 106 | +- **Related:** [Dynamic Import](/guide/features#dynamic-import) |
| 107 | + |
| 108 | +Options to pass on to [@rollup/plugin-dynamic-import-vars](https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars). |
| 109 | + |
| 110 | +## build.lib |
| 111 | + |
| 112 | +- **Type:** `{ entry: string, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string | ((format: ModuleFormat) => string) }` |
| 113 | +- **Related:** [Library Mode](/guide/build#library-mode) |
| 114 | + |
| 115 | +Build as a library. `entry` is required since the library cannot use HTML as entry. `name` is the exposed global variable and is required when `formats` includes `'umd'` or `'iife'`. Default `formats` are `['es', 'umd']`. `fileName` is the name of the package file output, default `fileName` is the name option of package.json, it can also be defined as function taking the `format` as an argument. |
| 116 | + |
| 117 | +## build.manifest |
| 118 | + |
| 119 | +- **Type:** `boolean | string` |
| 120 | +- **Default:** `false` |
| 121 | +- **Related:** [Backend Integration](/guide/backend-integration) |
| 122 | + |
| 123 | +When set to `true`, the build will also generate a `manifest.json` file that contains a mapping of non-hashed asset filenames to their hashed versions, which can then be used by a server framework to render the correct asset links. When the value is a string, it will be used as the manifest file name. |
| 124 | + |
| 125 | +## build.ssrManifest |
| 126 | + |
| 127 | +- **Type:** `boolean | string` |
| 128 | +- **Default:** `false` |
| 129 | +- **Related:** [Server-Side Rendering](/guide/ssr) |
| 130 | + |
| 131 | +When set to `true`, the build will also generate a SSR manifest for determining style links and asset preload directives in production. When the value is a string, it will be used as the manifest file name. |
| 132 | + |
| 133 | +## build.ssr |
| 134 | + |
| 135 | +- **Type:** `boolean | string` |
| 136 | +- **Default:** `undefined` |
| 137 | +- **Related:** [Server-Side Rendering](/guide/ssr) |
| 138 | + |
| 139 | +Produce SSR-oriented build. The value can be a string to directly specify the SSR entry, or `true`, which requires specifying the SSR entry via `rollupOptions.input`. |
| 140 | + |
| 141 | +## build.minify |
| 142 | + |
| 143 | +- **Type:** `boolean | 'terser' | 'esbuild'` |
| 144 | +- **Default:** `'esbuild'` |
| 145 | + |
| 146 | +Set to `false` to disable minification, or specify the minifier to use. The default is [esbuild](https://github.com/evanw/esbuild) which is 20 ~ 40x faster than terser and only 1 ~ 2% worse compression. [Benchmarks](https://github.com/privatenumber/minification-benchmarks) |
| 147 | + |
| 148 | +Note the `build.minify` option is not available when using the `'es'` format in lib mode. |
| 149 | + |
| 150 | +## build.terserOptions |
| 151 | + |
| 152 | +- **Type:** `TerserOptions` |
| 153 | + |
| 154 | +Additional [minify options](https://terser.org/docs/api-reference#minify-options) to pass on to Terser. |
| 155 | + |
| 156 | +## build.write |
| 157 | + |
| 158 | +- **Type:** `boolean` |
| 159 | +- **Default:** `true` |
| 160 | + |
| 161 | +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. |
| 162 | + |
| 163 | +## build.emptyOutDir |
| 164 | + |
| 165 | +- **Type:** `boolean` |
| 166 | +- **Default:** `true` if `outDir` is inside `root` |
| 167 | + |
| 168 | +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 accidentally removing important files. You can explicitly set this option to suppress the warning. This is also available via command line as `--emptyOutDir`. |
| 169 | + |
| 170 | +## build.reportCompressedSize |
| 171 | + |
| 172 | +- **Type:** `boolean` |
| 173 | +- **Default:** `true` |
| 174 | + |
| 175 | +Enable/disable gzip-compressed size reporting. Compressing large output files can be slow, so disabling this may increase build performance for large projects. |
| 176 | + |
| 177 | +### build.chunkSizeWarningLimit |
| 178 | + |
| 179 | +- **Type:** `number` |
| 180 | +- **Default:** `500` |
| 181 | + |
| 182 | + Limit for chunk size warnings (in kbs). |
| 183 | + |
| 184 | +## build.watch |
| 185 | + |
| 186 | +- **Type:** [`WatcherOptions`](https://rollupjs.org/guide/en/#watch-options)`| null` |
| 187 | +- **Default:** `null` |
| 188 | + |
| 189 | +Set to `{}` to enable rollup watcher. This is mostly used in cases that involve build-only plugins or integrations processes. |
0 commit comments