Skip to content

Commit 990df27

Browse files
committed
docs (#18): add deployment page
1 parent 35b4a9b commit 990df27

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

src/.vuepress/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ const sidebar = {
8585
children: [
8686
'/guide/single-file-component',
8787
'/guide/testing',
88-
'/guide/typescript-support'
88+
'/guide/typescript-support',
89+
'/guide/tooling/deployment'
8990
]
9091
},
9192
{

src/guide/tooling/deployment.md

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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.
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 (`vue.min.js`) for production. Both versions can be found in the [Installation guide](installation.html#Direct-lt-script-gt-Include).
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. All `vue-cli` templates have these 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+
But in Webpack 3 and earlier, you'll need to use [DefinePlugin](https://webpack.js.org/plugins/define-plugin/):
30+
31+
```js
32+
var webpack = require('webpack')
33+
34+
module.exports = {
35+
// ...
36+
plugins: [
37+
// ...
38+
new webpack.DefinePlugin({
39+
'process.env.NODE_ENV': JSON.stringify('production')
40+
})
41+
]
42+
}
43+
```
44+
45+
#### Browserify
46+
47+
- 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.
48+
49+
- 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:
50+
51+
```bash
52+
NODE_ENV=production browserify -g envify -e main.js | uglifyjs -c -m > build.js
53+
```
54+
55+
- Or, using [envify](https://github.com/hughsk/envify) with Gulp:
56+
57+
```js
58+
// Use the envify custom module to specify environment variables
59+
var envify = require('envify/custom')
60+
61+
browserify(browserifyOptions)
62+
.transform(vueify)
63+
.transform(
64+
// Required in order to process node_modules files
65+
{ global: true },
66+
envify({ NODE_ENV: 'production' })
67+
)
68+
.bundle()
69+
```
70+
71+
- Or, using [envify](https://github.com/hughsk/envify) with Grunt and [grunt-browserify](https://github.com/jmreidy/grunt-browserify):
72+
73+
```js
74+
// Use the envify custom module to specify environment variables
75+
var envify = require('envify/custom')
76+
77+
browserify: {
78+
dist: {
79+
options: {
80+
// Function to deviate from grunt-browserify's default order
81+
configure: b =>
82+
b
83+
.transform('vueify')
84+
.transform(
85+
// Required in order to process node_modules files
86+
{ global: true },
87+
envify({ NODE_ENV: 'production' })
88+
)
89+
.bundle()
90+
}
91+
}
92+
}
93+
```
94+
95+
#### Rollup
96+
97+
Use [@rollup/plugin-replace](https://github.com/rollup/plugins/tree/master/packages/replace):
98+
99+
```js
100+
const replace = require('@rollup/plugin-replace')
101+
102+
rollup({
103+
// ...
104+
plugins: [
105+
replace({
106+
'process.env.NODE_ENV': JSON.stringify( 'production' )
107+
})
108+
]
109+
}).then(...)
110+
```
111+
112+
## Pre-Compiling Templates
113+
114+
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.
115+
116+
The easiest way to pre-compile templates is using [Single-File Components](single-file-components.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.
117+
118+
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.
119+
120+
## Extracting Component CSS
121+
122+
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.
123+
124+
Refer to the respective build tool documentations to see how it's done:
125+
126+
- [Webpack + vue-loader](https://vue-loader.vuejs.org/en/configurations/extract-css.html) (the `vue-cli` webpack template has this pre-configured)
127+
- [Browserify + vueify](https://github.com/vuejs/vueify#css-extraction)
128+
- [Rollup + rollup-plugin-vue](https://vuejs.github.io/rollup-plugin-vue/#/en/2.3/?id=custom-handler)
129+
130+
## Tracking Runtime Errors
131+
132+
If a runtime error occurs during a component's render, it will be passed to the global `Vue.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

Comments
 (0)