10000 improve production deployment section · daniiell3/vuejs.org@0a4e653 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a4e653

Browse files
committed
improve production deployment section
1 parent 740adbb commit 0a4e653

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

src/v2/guide/deployment.md

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
---
2-
title: Deploying For Production
2+
title: Production Deployment Tips
33
type: guide
44
order: 20
55
---
66

7-
## Stripping Out Warnings
7+
## Turn on Production Mode
88

9-
The minified standalone build of Vue has already stripped out all the warnings for you for a smaller file size, but when you are using tools like Webpack or Browserify, you will need some additional configuration to achieve this.
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.
1010

11-
### Webpack
11+
### Without Build Tools
12+
13+
If you are using the standalone 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.
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
1220

1321
Use Webpack's [DefinePlugin](http://webpack.github.io/docs/list-of-plugins.html#defineplugin) to indicate a production environment, so that warning blocks can be automatically dropped by UglifyJS during minification. Example config:
1422

@@ -23,38 +31,52 @@ module.exports = {
2331
'process.env': {
2432
NODE_ENV: '"production"'
2533
}
26-
}),
27-
new webpack.optimize.UglifyJsPlugin({
28-
compress: {
29-
warnings: false
30-
}
3134
})
3235
]
3336
}
3437
```
3538

36-
### Browserify
39+
#### Browserify
40+
41+
- 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.
3742

38-
- Run your bundling command with `NODE_ENV` set to `"production"`. This tells `vueify` to avoid including hot-reload and development related code.
3943
- 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:
4044

45+
``` bash
46+
NODE_ENV=production browserify -g envify -e main.js | uglifyjs -c -m > build.js
47+
```
4148

42-
``` bash
43-
NODE_ENV=production browserify -g envify -e main.js | uglifyjs -c -m > build.js
44-
```
49+
#### Rollup
4550

46-
- To extract styles to a separate css file use a extract-css plugin which is included in vueify.
51+
Use [rollup-plugin-replace](https://github.com/rollup/rollup-plugin-replace):
4752

48-
``` bash
49-
NODE_ENV=production browserify -g envify -p [ vueify/plugins/extract-css -o build.css ] -e main.js | uglifyjs -c -m > build.js
53+
``` js
54+
const replace = require('rollup-plugin-replace')
55+
56+
rollup({
57+
// ...
58+
plugins: [
59+
replace({
60+
'process.env.NODE_ENV': JSON.stringify( 'production' )
61+
})
62+
]
63+
}).then(...)
5064
```
5165

52-
## Tracking Runtime Errors
66+
## Pre-Compiling Templates
5367

54-
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.
68+
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-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.
5569

56-
## Extracting CSS
70+
## Extracting Component CSS
5771

58-
When using [Single-File Components](./single-file-components.html), the `<style>` tags are injected dynamically at runtime during development. In production you may want to extract the styles across all components into a single CSS file. For details on how to achieve this, consult the respective documentation for [vue-loader](http://vue-loader.vuejs.org/en/configurations/extract-css.html) and [vueify](https://github.com/vuejs/vueify#css-extraction).
72+
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 and avoid these issues, and also result in better CSS minification and caching.
5973

60-
The official `webpack` template from `vue-cli` has this already configured out of the box.
74+
Refer to the respective build tool documentations to see how it's done:
75+
76+
- [Webpack + vue-loader](http://vue-loader.vuejs.org/en/configurations/extract-css.html) (the `vue-cli` webpack template has this pre-configured)
77+
- [Browserify + vueify](https://github.com/vuejs/vueify#css-extraction)
78+
- [Rollup + rollup-plugin-vue](https://github.com/znck/rollup-plugin-vue#options)
79+
80+
## Tracking Runtime Errors
81+
82+
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.

src/v2/guide/single-file-components.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,22 @@ As promised, we can also use preprocessors such as Jade, Babel (with ES2015 modu
3131

3232
<img src="/images/vue-component-with-preprocessors.png" style="display: block; margin: 30px auto">
3333

34-
These specific languages are just examples. You could just as easily use Bublé, TypeScript, SCSS, PostCSS - or whatever other preprocessors that help you be productive.
34+
These specific languages are just examples. You could just as easily use Bublé, TypeScript, SCSS, PostCSS - or whatever other preprocessors that help you be productive. If using Webpack with `vue-loader`, it also has first-class support for CSS Modules.
3535

36-
<!-- TODO: include CSS modules once it's supported in vue-loader 9.x -->
36+
### What About Separation of Concerns?
37+
38+
One important thing to note is that **separation of concerns is not equal to separation of file types.** In modern UI development, we have found that instead of dividing the codebase into three huge layers that interweaves with one another, it makes much more sense to divide them into loosely-coupled components and compose them. Inside a component, its template, logic and styles are inherently coupled, and collocating them actually makes the component more cohesive and maintainable.
39+
40+
Even if you don't like the idea of Single-File Components, you can still leverage its hot-reloading and pre-compilation features by separating your JavaScript and CSS into separate files:
41+
42+
``` html
43+
<!-- my-component.vue -->
44+
<template>
45+
<div>This will be pre-compiled</div>
46+
</template>
47+
<script src="./my-component.js"></script>
48+
<style src="./my-component.css"></style>
49+
```
3750

3851
## Getting Started
3952

0 commit comments

Comments
 (0)
0