|
| 1 | +# Production Deployment |
| 2 | + |
| 3 | +::: info |
| 4 | +Most of the tips below are enabled by default if you are using [Vue CLI](https://cli.vuejs.org). This section is only relevant if you are using a custom build setup. |
| 5 | +::: |
| 6 | + |
| 7 | +## Turn on Production Mode |
| 8 | + |
| 9 | +During development, Vue provides a lot of warnings to help you with common errors and pitfalls. However, these warning strings become useless in production and bloat your app's payload size. In addition, some of these warning checks have small runtime costs that can be avoided in [production mode](https://cli.vuejs.org/guide/mode-and-env.html#modes). |
| 10 | + |
| 11 | +### Without Build Tools |
| 12 | + |
| 13 | +If you are using the full build, i.e. directly including Vue via a script tag without a build tool, make sure to use the minified version for production. This can be found in the [Installation guide](installation.html#cdn). |
| 14 | + |
| 15 | +### With Build Tools |
| 16 | + |
| 17 | +When using a build tool like Webpack or Browserify, the production mode will be determined by `process.env.NODE_ENV` inside Vue's source code, and it will be in development mode by default. Both build tools provide ways to overwrite this variable to enable Vue's production mode, and warnings will be stripped by minifiers during the build. Vue CLI has this pre-configured for you, but it would be beneficial to know how it is done: |
| 18 | + |
| 19 | +#### Webpack |
| 20 | + |
| 21 | +In Webpack 4+, you can use the `mode` option: |
| 22 | + |
| 23 | +```js |
| 24 | +module.exports = { |
| 25 | + mode: 'production' |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +#### Browserify |
| 30 | + |
| 31 | +- Run your bundling command with the actual `NODE_ENV` environment variable set to `"production"`. This tells `vueify` to avoid including hot-reload and development related code. |
| 32 | + |
| 33 | +- Apply a global [envify](https://github.com/hughsk/envify) transform to your bundle. This allows the minifier to strip out all the warnings in Vue's source code wrapped in env variable conditional blocks. For example: |
| 34 | + |
| 35 | + ```bash |
| 36 | + NODE_ENV=production browserify -g envify -e main.js | uglifyjs -c -m > build.js |
| 37 | + ``` |
| 38 | + |
| 39 | +- Or, using [envify](https://github.com/hughsk/envify) with Gulp: |
| 40 | + |
| 41 | + ```js |
| 42 | + // Use the envify custom module to specify environment variables |
| 43 | + const envify = require('envify/custom') |
| 44 | + |
| 45 | + browserify(browserifyOptions) |
| 46 | + .transform(vueify) |
| 47 | + .transform( |
| 48 | + // Required in order to process node_modules files |
| 49 | + { global: true }, |
| 50 | + envify({ NODE_ENV: 'production' }) |
| 51 | + ) |
| 52 | + .bundle() |
| 53 | + ``` |
| 54 | + |
| 55 | +- Or, using [envify](https://github.com/hughsk/envify) with Grunt and [grunt-browserify](https://github.com/jmreidy/grunt-browserify): |
| 56 | + |
| 57 | + ```js |
| 58 | + // Use the envify custom module to specify environment variables |
| 59 | + const envify = require('envify/custom') |
| 60 | + |
| 61 | + browserify: { |
| 62 | + dist: { |
| 63 | + options: { |
| 64 | + // Function to deviate from grunt-browserify's default order |
| 65 | + configure: (b) => |
| 66 | + b |
| 67 | + .transform('vueify') |
| 68 | + .transform( |
| 69 | + // Required in order to process node_modules files |
| 70 | + { global: true }, |
| 71 | + envify({ NODE_ENV: 'production' }) |
| 72 | + ) |
| 73 | + .bundle() |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + ``` |
| 78 | + |
| 79 | +#### Rollup |
| 80 | + |
| 81 | +Use [@rollup/plugin-replace](https://github.com/rollup/plugins/tree/master/packages/replace): |
| 82 | + |
| 83 | +```js |
| 84 | +const replace = require('@rollup/plugin-replace') |
| 85 | + |
| 86 | +rollup({ |
| 87 | + // ... |
| 88 | + plugins: [ |
| 89 | + replace({ |
| 90 | + 'process.env.NODE_ENV': JSON.stringify( 'production' ) |
| 91 | + }) |
| 92 | + ] |
| 93 | +}).then(...) |
| 94 | +``` |
| 95 | + |
| 96 | +## Pre-Compiling Templates |
| 97 | + |
| 98 | +When using in-DOM templates or in-JavaScript template strings, the template-to-render-function compilation is performed on the fly. This is usually fast enough in most cases, but is best avoided if your application is performance-sensitive. |
| 99 | + |
| 100 | +The easiest way to pre-compile templates is using [Single-File Components](single-file-component.html) - the associated build setups automatically performs pre-compilation for you, so the built code contains the already compiled render functions instead of raw template strings. |
| 101 | + |
| 102 | +If you are using Webpack, and prefer separating JavaScript and template files, you can use [vue-template-loader](https://github.com/ktsn/vue-template-loader), which also transforms the template files into JavaScript render functions during the build step. |
| 103 | + |
| 104 | +## Extracting Component CSS |
| 105 | + |
| 106 | +When using Single-File Components, the CSS inside components are injected dynamically as `<style>` tags via JavaScript. This has a small runtime cost, and if you are using server-side rendering it will cause a "flash of unstyled content". Extracting the CSS across all components into the same file will avoid these issues, and also result in better CSS minification and caching. |
| 107 | + |
| 108 | +Refer to the respective build tool documentations to see how it's done: |
| 109 | + |
| 110 | +- [Webpack + vue-loader](https://vue-loader.vuejs.org/en/configurations/extract-css.html) (the `vue-cli` webpack template has this pre-configured) |
| 111 | +- [Browserify + vueify](https://github.com/vuejs/vueify#css-extraction) |
| 112 | +- [Rollup + rollup-plugin-vue](https://rollup-plugin-vue.vuejs.org/) |
| 113 | + |
| 114 | +## Tracking Runtime Errors |
| 115 | + |
| 116 | +If a runtime error occurs during a component's render, it will be passed to the global `app.config.errorHandler` config function if it has been set. It might be a good idea to leverage this hook together with an error-tracking service like [Sentry](https://sentry.io), which provides [an official integration](https://sentry.io/for/vue/) for Vue. |
0 commit comments