diff --git a/src/.vuepress/config.js b/src/.vuepress/config.js index def1f8ef38..931a707650 100644 --- a/src/.vuepress/config.js +++ b/src/.vuepress/config.js @@ -94,7 +94,8 @@ const sidebar = { '/guide/single-file-component', '/guide/testing', '/guide/typescript-support', - '/guide/mobile' + '/guide/mobile', + '/guide/tooling/deployment' ] }, { diff --git a/src/guide/tooling/deployment.md b/src/guide/tooling/deployment.md new file mode 100644 index 0000000000..fabebe337d --- /dev/null +++ b/src/guide/tooling/deployment.md @@ -0,0 +1,116 @@ +# Production Deployment + +::: info +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. +::: + +## Turn on Production Mode + +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). + +### Without Build Tools + +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). + +### With Build Tools + +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: + +#### Webpack + +In Webpack 4+, you can use the `mode` option: + +```js +module.exports = { + mode: 'production' +} +``` + +#### Browserify + +- 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. + +- 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: + + ```bash + NODE_ENV=production browserify -g envify -e main.js | uglifyjs -c -m > build.js + ``` + +- Or, using [envify](https://github.com/hughsk/envify) with Gulp: + + ```js + // Use the envify custom module to specify environment variables + const envify = require('envify/custom') + + browserify(browserifyOptions) + .transform(vueify) + .transform( + // Required in order to process node_modules files + { global: true }, + envify({ NODE_ENV: 'production' }) + ) + .bundle() + ``` + +- Or, using [envify](https://github.com/hughsk/envify) with Grunt and [grunt-browserify](https://github.com/jmreidy/grunt-browserify): + + ```js + // Use the envify custom module to specify environment variables + const envify = require('envify/custom') + + browserify: { + dist: { + options: { + // Function to deviate from grunt-browserify's default order + configure: (b) => + b + .transform('vueify') + .transform( + // Required in order to process node_modules files + { global: true }, + envify({ NODE_ENV: 'production' }) + ) + .bundle() + } + } + } + ``` + +#### Rollup + +Use [@rollup/plugin-replace](https://github.com/rollup/plugins/tree/master/packages/replace): + +```js +const replace = require('@rollup/plugin-replace') + +rollup({ + // ... + plugins: [ + replace({ + 'process.env.NODE_ENV': JSON.stringify( 'production' ) + }) + ] +}).then(...) +``` + +## Pre-Compiling Templates + +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. + +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. + +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. + +## Extracting Component CSS + +When using Single-File Components, the CSS inside components are injected dynamically as `