Skip to content

Commit 511bd11

Browse files
kazuponegoist
authored andcommitted
salvage docs (vuejs#603)
1 parent 319c351 commit 511bd11

File tree

1 file changed

+298
-0
lines changed

1 file changed

+298
-0
lines changed

docs/build.md

+298
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
# vue build
2+
3+
`vue build` command gives you a zero-configuration development setup, install once and build everywhere.
4+
5+
## Features
6+
7+
- **Not a boilerplate**: run a single command to develop your app
8+
- **Out of the box**: ES2015, single-file component with hot reloading and custom CSS preprocessors
9+
- **Customizable**: populate a `~/.vue/webpack.config.js` for custom webpack config
10+
- **Single-file component mode**: simply run `vue build Component.vue` and test it out in the browser!
11+
12+
## Get started
13+
14+
Make sure that you've installed `vue-cli` with `npm >= 3` or `yarn >= 0.7`.
15+
16+
Populate an app entry `./index.js` in your project:
17+
18+
```js
19+
import Vue from 'vue'
20+
21+
new Vue({
22+
el: '#app',
23+
render: h => h('h2', 'hello world')
24+
})
25+
```
26+
27+
And then run `vue build index.js` and go to `http://localhost:4000`
28+
29+
**To build for production (minimized and optimized):**
30+
31+
```bash
32+
$ vue build index.js --prod
33+
```
34+
35+
If you want to directly test a component without manually create a Vue instance for it, try:
36+
37+
```bash
38+
$ vue build Component.vue
39+
```
40+
41+
<details><summary>How does this work?</summary><br>
42+
When the input file ends with `.vue` extension, we use a [default app entry](/lib/default-entry.es6) to load the given component, otherwise we treat it as a normal webpack entry. For jsx component which ends with `.js` extension, you can enable this behavior manually by adding `--mount`.
43+
</details>
44+
45+
**To distribute component:**
46+
47+
```bash
48+
$ vue build Component.vue --prod --lib
49+
```
50+
51+
This will create an optimized bundle in UMD format, and the name of exported library is set to `Component`, you can use `--lib [CustomLibraryName]` to customize it.
52+
53+
Note that in some cases you may use [`externals`](https://webpack.js.org/configuration/externals/) to exclude some modules from your bundle.
54+
55+
**Watch mode:**
56+
57+
```bash
58+
$ vue build index.js --watch
59+
```
60+
61+
It's similar to `development mode` but does not add hot-reloading support and uses a real file system.
62+
63+
**For more CLI usages:**
64+
65+
```bash
66+
$ vue build -h
67+
```
68+
69+
## Configuration files
70+
71+
By default, we use `~/.vue/config.js` and `~/.vue/webpack.config.js` if they exist.
72+
73+
To use a custom config file, add `--config [file]`
74+
75+
To use a custom webpack config file, add `--webpack [file]`
76+
77+
### config.js
78+
79+
You can define CLI options in this file.
80+
81+
#### entry
82+
83+
Type: `string` `Array` `Object`
84+
85+
It's the first argument of `vue build` command, eg: `vue build entry.js`. You can set it here to omit it in CLI arguments.
86+
87+
The single-component mode (`--mount`) will not work if you set `entry` to an `Array` or `Object`.
88+
89+
- `Array`: Override `webpackConfig.entry.client`
90+
- `Object`: Override `webpackConfig.entry`
91+
- `string`: Added to `webpackConfig.entry.client` or used as `webpackConfig.resolve.alias['your-tasteful-component']` in single-component mode.
92+
93+
#### port
94+
95+
Type: `number`<br>
96+
Default: `4000`
97+
98+
Port of dev server.
99+
100+
#### webpack
101+
102+
Type: `function` `string` `object`
103+
104+
##### function
105+
106+
`webpack(webpackConfig, options, webpack)`
107+
108+
- webpackConfig: current webpack config
109+
- options: CLI options (assigned with config.js)
110+
- webpack: The `webpack` module
111+
112+
Return a new webpack config.
113+
114+
##### string
115+
116+
Used as the path to webpack config file, eg: `--webpack webpack.config.js`
117+
118+
##### object
119+
120+
Directly use as webpack config.
121+
122+
Note that we use [webpack-merge](https://github.com/survivejs/webpack-merge) to merge your webpack config with default webpack config.
123+
124+
#### autoprefixer
125+
126+
Type: `object`
127+
128+
Autoprefixer options, default value:
129+
130+
```js
131+
{
132+
browsers: ['ie > 8', 'last 5 versions']
133+
}
134+
```
135+
136+
#### postcss
137+
138+
Type: `Object` `Array` `Function`
139+
140+
PostCSS options, if it's an `Array` or `Function`, the default value will be override:
141+
142+
```js
143+
{
144+
plugins: [
145+
require('autoprefixer')(options.autoprefixer)
146+
]
147+
}
148+
```
149+
150+
#### babel
151+
152+
Type: `Object`
153+
154+
[Babel options](https://github.com/babel/babel-loader#options). You can set `babel.babelrc` to false to disable using `.babelrc`.
155+
156+
#### html
157+
158+
Type: `Object` `Array` `boolean`
159+
160+
[html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin) options, use this option to customize `index.html` output, default value:
161+
162+
```js
163+
{
164+
title: 'Vue App',
165+
template: path.join(__dirname, '../lib/template.html')
166+
}
167+
```
168+
169+
Check out the [default template](/lib/template.html) file we use. To disable generating html file, you can set `html` to `false`.
170+
171+
#### filename
172+
173+
Set custom filename for `js` `css` `static` files:
174+
175+
```js
176+
{
177+
filename: {
178+
js: 'index.js',
179+
css: 'style.css',
180+
static: 'static/[name].[ext]'
181+
}
182+
}
183+
```
184+
185+
#### disableCompress
186+
187+
Type: `boolean`
188+
189+
In production mode, all generated files will be compressed and produce sourcemaps file. You can use `--disableCompress` to disable this behavior.
190+
191+
#### hmrEntries
192+
193+
Type: `Array`<br>
194+
Default: `['client']`
195+
196+
Add `webpack-hot-middleware` HMR client to specific webpack entries. By default your app is loaded in `client` entry, so we insert it here.
197+
198+
#### proxy
199+
200+
Type: `string`, `Object`
201+
202+
To tell the development server to serve any `/api/*` request to your API server in development, use the `proxy` options:
203+
204+
```js
205+
module.exports = {
206+
proxy: 'http://localhost:8080/api'
207+
}
208+
```
209+
210+
This way, when you fetch `/api/todos` in your Vue app, the development server will proxy your request to `http://localhost:8080/api/todos`.
211+
212+
We use [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware) under the hood, so the `proxy` option can also be an object:
213+
214+
```js
215+
module.exports = {
216+
proxy: {
217+
'/api/foo': 'http://localhost:8080/api',
218+
'/api/fake-data': {
219+
target: 'http://jsonplaceholder.typicode.com',
220+
changeOrigin: true,
221+
pathRewrite: {
222+
'^/api/fake-data': ''
223+
}
224+
}
225+
}
226+
}
227+
```
228+
229+
Keep in mind that proxy only has effect in development.
230+
231+
#### setup
232+
233+
Type: `function`
234+
235+
Perform some custom logic to development server:
236+
237+
```js
238+
module.exports = {
239+
setup(app) {
240+
app.get('/api', (req, res) => {
241+
res.end('This is the API')
242+
})
243+
}
244+
}
245+
```
246+
247+
#### run(webpackConfig, options)
248+
249+
Type: `function`
250+
251+
You can use a custom `run` function to perform your own build process instead of the default one. For example, run karma with the processed webpack config:
252+
253+
```js
254+
const Server = require('karma').Server
255+
256+
module.exports = {
257+
run(webpackConfig) {
258+
const server = new Server({
259+
webpack: webpackConfig,
260+
// other karma options...
261+
}, exitCode => {
262+
console.log('Karma has exited with ' + exitCode)
263+
process.exit(exitCode)
264+
})
265+
server.start()
266+
}
267+
}
268+
```
269+
270+
### webpack.config.js
271+
272+
All the webpack options are available here.
273+
274+
## Recipes
275+
276+
### Custom CSS preprocessors
277+
278+
CSS preprocessors (and CSS extraction) work out of the box, install relevant loaders and you're all set! For example, add `sass` support:
279+
280+
```bash
281+
$ npm i -D node-sass sass-loader
282+
```
283+
284+
Since all CSS will be piped through `postcss-loader`, `autoprefixer` and `postcss` options will always work no matter what CSS preprocessors you're using.
285+
286+
### Custom babel config
287+
288+
By default we only use a single babel preset: [babel-preset-vue-app](https://github.com/egoist/babel-preset-vue-app) which includes following features:
289+
290+
- ES2015/2016/2017 and Stage-2 features
291+
- Transform `async/await` and `generator`
292+
- Transform Vue JSX
293+
294+
You can set `babel` option in config file or populate a `.babelrc` in project root directory to override it.
295+
296+
### Copy static files
297+
298+
Everything in `./static` folder will be copied to dist folder, for example: `static/favicon.ico` will be copied to `dist/favicon.ico`.

0 commit comments

Comments
 (0)