|
| 1 | +--- |
| 2 | +outline: deep |
| 3 | +--- |
| 4 | + |
| 5 | +# Compile-Time Flags {#compile-time-flags} |
| 6 | + |
| 7 | +When using Vue with a build step, it is possible to configure a number of compile-time flags to enable / disable certain features. The benefit of using compile-time flags is that features disabled this way can be removed from the final bundle via tree-shaking. |
| 8 | + |
| 9 | +Vue will work even if these flags are not explicitly configured. However, it is recommended to always configure them so that the relevant features can be properly removed when possible. |
| 10 | + |
| 11 | +See [Configuration Guides](#configuration-guides) on how to configure them depending on your build tool. |
| 12 | + |
| 13 | +## `__VUE_OPTIONS_API__` |
| 14 | + |
| 15 | +- **Default:** `true` |
| 16 | + |
| 17 | + Enable / disable Options API support. Disabling this will result in smaller bundles, but may affect compatibility with 3rd party libraries if they rely on Options API. |
| 18 | + |
| 19 | +## `__VUE_PROD_DEVTOOLS__` |
| 20 | + |
| 21 | +- **Default:** `false` |
| 22 | + |
| 23 | + Enable / disable devtools support in production builds. This will result in more code included in the bundle, so it is recommended to only enable this for debugging purposes. |
| 24 | + |
| 25 | +## `__VUE_PROD_HYDRATION_MISMATCH_DETAILS__` <sup class="vt-badge" data-text="3.4+" /> |
| 26 | + |
| 27 | +- **Default:** `false` |
| 28 | + |
| 29 | + Enable/disable detailed warnings for hydration mismatches in production builds. This will result in more code included in the bundle, so it is recommended to only enable this for debugging purposes. |
| 30 | + |
| 31 | +## Configuration Guides |
| 32 | + |
| 33 | +### Vite |
| 34 | + |
| 35 | +`@vitejs/plugin-vue` automatically provides default values for these flags. To change the default values, use Vite's [`define` config option](https://vitejs.dev/config/shared-options.html#define): |
| 36 | + |
| 37 | +```js |
| 38 | +// vite.config.js |
| 39 | +import { defineConfig } from 'vite' |
| 40 | + |
| 41 | +export default defineConfig({ |
| 42 | + define: { |
| 43 | + // enable hydration mismatch details in production build |
| 44 | + __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'true' |
| 45 | + } |
| 46 | +}) |
| 47 | +``` |
| 48 | + |
| 49 | +### vue-cli |
| 50 | + |
| 51 | +`@vue/cli-service` automatically provides default values for some of these flags. To configure /change the values: |
| 52 | + |
| 53 | +```js |
| 54 | +// vue.config.js |
| 55 | +module.exports = { |
| 56 | + chainWebpack: (config) => { |
| 57 | + config.plugin('define').tap((definitions) => { |
| 58 | + Object.assign(definitions[0], { |
| 59 | + __VUE_OPTIONS_API__: 'true', |
| 60 | + __VUE_PROD_DEVTOOLS__: 'false', |
| 61 | + __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false' |
| 62 | + }) |
| 63 | + return definitions |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### webpack |
| 70 | + |
| 71 | +Flags should be defined using webpack's [DefinePlugin](https://webpack.js.org/plugins/define-plugin/): |
| 72 | + |
| 73 | +```js |
| 74 | +// webpack.config.js |
| 75 | +module.exports = { |
| 76 | + // ... |
| 77 | + plugins: [ |
| 78 | + new webpack.DefinePlugin({ |
| 79 | + __VUE_OPTIONS_API__: 'true', |
| 80 | + __VUE_PROD_DEVTOOLS__: 'false', |
| 81 | + __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false' |
| 82 | + }) |
| 83 | + ] |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### Rollup |
| 88 | + |
| 89 | +Flags should be defined using [@rollup/plugin-replace](https://github.com/rollup/plugins/tree/master/packages/replace): |
| 90 | + |
| 91 | +```js |
| 92 | +// rollup.config.js |
| 93 | +import replace from '@rollup/plugin-replace' |
| 94 | + |
| 95 | +export default { |
| 96 | + plugins: [ |
| 97 | + replace({ |
| 98 | + __VUE_OPTIONS_API__: 'true', |
| 99 | + __VUE_PROD_DEVTOOLS__: 'false', |
| 100 | + __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false' |
| 101 | + }) |
| 102 | + ] |
| 103 | +} |
| 104 | +``` |
0 commit comments