Skip to content

Commit 5f8fb21

Browse files
committed
docs: progress
1 parent 2756cdd commit 5f8fb21

File tree

6 files changed

+226
-5
lines changed

6 files changed

+226
-5
lines changed

docs/Plugin.md

+32
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,38 @@ module.exports = api => {
9898
}
9999
```
100100

101+
#### Resolving Webpack Config in Plugins
102+
103+
A plugin can retrieve the resolved webpack config by calling `api.resolveWebpackConfig()`. Every call generates a fresh webpack config which can be further mutated as needed:
104+
105+
``` js
106+
api.regsiterCommand('my-build', args => {
107+
// make sure to set mode and load env variables
108+
api.setMode('production')
109+
110+
const configA = api.resolveWebpackConfig()
111+
const configB = api.resolveWebpackConfig()
112+
113+
// mutate configA and configB for different purposes...
114+
})
115+
```
116+
117+
Alternatively, a plugin can also obtain a fresh [chainable config](https://github.com/mozilla-neutrino/webpack-chain) by calling `api.resolveChainableWebpackConfig()`:
118+
119+
``` js
120+
api.regsiterCommand('my-build', args => {
121+
api.setMode('production')
122+
123+
const configA = api.resolveChainableWebpackConfig()
124+
const configB = api.resolveChainableWebpackConfig()
125+
126+
// chain-modify configA and configB for different purposes...
127+
128+
const finalConfigA = configA.toConfig()
129+
const finalConfigB = configB.toConfig()
130+
})
131+
```
132+
101133
#### Custom Options for 3rd Party Plugins
102134

103135
The exports from `vue.config.js` will be [validated against a schema](https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-service/lib/options.js#L3) to avoid typos and wrong config values. However, a 3rd party plugin can still allow the user to configure its behavior via the `pluginOptions` field. For example, with the following `vue.config.js`:

docs/README.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,30 @@ For full details on what the `vue` command can do, see the [full CLI docs](./cli
4343

4444
## CLI Service
4545

46-
`@vue/cli-service` is a dependency installed locally into every project created by `@vue/cli`. It contains the core service that loads other plugins, resolves the final webpack config, and provides the `vue-cli-service` binary to your project. The binary exposes commands such as `vue-cli-service serve`, which can be used in npm scripts. If you are familiar with [create-react-app](https://github.com/facebookincubator/create-react-app), `@vue/cli-service` is essentially the equivalent of `react-scripts`, but more flexible.
46+
`@vue/cli-service` is a dependency installed locally into every project created by `@vue/cli`. It contains the core service that loads other plugins, resolves the final webpack config, and provides the `vue-cli-service` binary to your project. If you are familiar with [create-react-app](https://github.com/facebookincubator/create-react-app), `@vue/cli-service` is essentially the equivalent of `react-scripts`, but more flexible.
47+
48+
Inside a project, you can access the binary directly as `vue-cli-service` in npm scripts, or as `./node_modules/.bin/vue-cli-service` from the terminal. This is what you will see in the `package.json` of a project using the default preset:
49+
50+
``` json
51+
{
52+
"scripts": {
53+
"serve": "vue-cli-service serve --open",
54+
"build": "vue-cli-service build"
55+
}
56+
}
57+
```
58+
59+
Some CLI plugins will inject additional commands to `vue-cli-service`. For example, `@vue/cli-plugin-eslint` injects the `vue-cli-service lint` command. You can see all injected commands by running:
60+
61+
``` sh
62+
./node_modules/.bin/vue-cli-service help
63+
```
64+
65+
You can also learn about the available options of each command with:
66+
67+
``` sh
68+
./node_modules/.bin/vue-cli-service help [command]
69+
```
4770

4871
## Configuration
4972

docs/cli.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
1-
## CLI Usage
1+
## CLI
2+
3+
- [Installation](#installation)
4+
- [Usage](#usage)
5+
- [Creating a New Project](#creating-a-new-project)
6+
- [Zero-config Prototyping](#zero-config-prototyping)
7+
- [Installing Plugins in an Existing Project](#installing-plugins-in-an-existing-project)
8+
- [Inspecting the webpack Config](#inspecting-the-projects-webpack-config)
9+
- [Pulling 2.x Templates](#pulling-vue-cli2x-templates-legacy)
10+
11+
### Installation
12+
13+
``` sh
14+
npm install -g @vue/cli
15+
vue create my-project
16+
```
17+
18+
### Usage
19+
20+
```
21+
Usage: vue <command> [options]
22+
23+
Commands:
24+
25+
create [options] <app-name> create a new project powered by vue-cli-service
26+
invoke <plugin> [pluginOptions] invoke the generator of a plugin in an already created project
27+
inspect [options] [paths...] inspect the webpack config in a project with vue-cli-service
28+
serve [options] [entry] serve a .js or .vue file in development mode with zero config
29+
build [options] [entry] build a .js or .vue file in production mode with zero config
30+
init <template> <app-name> generate a project from a remote template (legacy API, requires @vue/cli-init)
31+
```
32+
33+
For each command, you can also use `vue <command> --help` to see more detailed usage.
234

335
### Creating a New Project
436

@@ -47,6 +79,27 @@ vue invoke eslint --config airbnb --lintOn save
4779

4880
It is recommended to commit your project's current state before running `vue invoke`, so that after file generation you can review the changes and revert if needed.
4981

82+
### Inspecting the Project's Webpack Config
83+
84+
Since `@vue/cli-service` abstracts away the webpack config, it may be more difficult to understand what is included in the config, especially when you are trying to make tweaks yourself.
85+
86+
`vue-cli-service` exposes the `inspect` command for inspecting the resolved webpack config. The global `vue` binary also provides the `inspect` command, and it simply proxies to `vue-cli-service inspect` in your project.
87+
88+
The command prints to stdout by default, so you can redirect that into a file for easier inspection:
89+
90+
``` sh
91+
vue inspect > output.js
92+
```
93+
94+
Note the output is not a valid webpack config file, it's a serialized format only meant for inspection.
95+
96+
You can also inspect a certain path of the config to narrow it down:
97+
98+
``` sh
99+
# only inspect the first rule
100+
vue inspect module.rules.0
101+
```
102+
50103
### Pulling `vue-cli@2.x` Templates (Legacy)
51104

52105
`@vue/cli` uses the same `vue` binary, so it overwrites `vue-cli@2.x`. If you still need the legacy `vue init` functionality, you can install a global bridge:

docs/css.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## CSS
22

3+
- [PostCSS](#postcss)
4+
- [CSS Modules](#css-modules)
5+
- [Pre-Processors](#pre-processors)
6+
- [Passing Options to Pre-Processor Loaders](#passing-options-to-pre-processor-loaders)
7+
38
### PostCSS
49

510
Vue CLI uses PostCSS internally, and enables [autoprefixer](https://github.com/postcss/autoprefixer) by default. You can configure PostCSS via `.postcssrc` or any config source supported by [postcss-load-config](https://github.com/michael-ciniawsky/postcss-load-config).

docs/env.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## Environment Variables and Modes
22

3+
- [Overview](#overview)
4+
- [Modes](#modes)
5+
- [Using Env Variables in Client-side Code](#using-env-variables-in-client-side-code)
6+
- [Local Only Variables](#local-only-variables)
7+
8+
### Overview
9+
310
You can specify env variables by placing the following files in your project root:
411

512
``` sh
@@ -45,7 +52,7 @@ In addition to `VUE_APP_*` variables, there are also two special variables that
4552
- `NODE_ENV` - this will be one of `"development"`, `"production"` or `"test"` depending on the [mode](#modes) the app is running in.
4653
- `BASE_URL` - this corresponds to the `baseUrl` option in `vue.config.js` and is the base path your app is deployed at.
4754

48-
### Local Variables
55+
### Local Only Variables
4956

5057
Sometimes you might have env variables that should not be committed into the codebase, especially if your project is hosted in a public repository. In that case you should use an `.env.local` file instead. Local env files are ignored in `.gitignore` by default.
5158

docs/webpack.md

+103-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,108 @@
1-
# WIP
1+
## Configuring webpack
2+
3+
- [Basic Configuration](#basic-configuration)
4+
- [Chaining](#chaining-advanced)
5+
- [Inspecting the Config](#inspecting-the-projects-webpack-config)
6+
- [Using Resolved Config as a File](#using-resolved-config-as-a-file)
27

38
### Basic Configuration
49

5-
### Chaining
10+
The easiest way to tweak the webpack config is provide an object to the `configureWebpack` option in `vue.config.js`:
11+
12+
``` js
13+
// vue.config.js
14+
module.exports = {
15+
configureWebpack: {
16+
plugins: [
17+
new MyAwesomeWebpackPlugin()
18+
]
19+
}
20+
}
21+
```
22+
23+
The object will be merged into the final webpack config using [webpack-merge](https://github.com/survivejs/webpack-merge).
24+
25+
If you need conditional behavior based on the environment, or want to directly mutate the config, use a function (which will be lazy evaluated after the env variables are set). The function receives the resolved config as the argument. Inside the function, you can either mutate the config directly, OR return an object which will be merged:
26+
27+
``` js
28+
// vue.config.js
29+
module.exports = {
30+
configureWebpack: config => {
31+
if (process.env.NODE_ENV === 'production') {
32+
// mutate config for production...
33+
} else {
34+
// mutate for development...
35+
}
36+
}
37+
}
38+
```
39+
40+
### Chaining (Advanced)
41+
42+
The internal webpack config is maintained using [webpack-chain](https://github.com/mozilla-neutrino/webpack-chain). The library provides an abstraction over the raw webpack config, with the ability to define named loader rules and named plugins, and later "tap" into those rules and modify their options.
43+
44+
This allows us finer-grained control over the internal config. Here are some examples:
45+
46+
#### Transpiling a Dependency Module
47+
48+
By default the Babel configuration skips
49+
50+
``` js
51+
// vue.config.js
52+
module.exports = {
53+
chainWebpack: config => {
54+
config
55+
.rule('js')
56+
.include
57+
.add(/some-module-to-transpile/)
58+
}
59+
}
60+
```
61+
62+
#### Modifying Plugin Options
63+
64+
``` js
65+
// vue.config.js
66+
module.exports = {
67+
chainWebpack: config => {
68+
config
69+
.plugin('html')
70+
.tap(args => {
71+
return [/* new args to pass to html-webpack-plugin's constructor */]
72+
})
73+
}
74+
}
75+
```
76+
77+
You will need to familiarize yourself with [webpack-chain's API](https://github.com/mozilla-neutrino/webpack-chain#getting-started) and [read some source code](https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config) in order to understand how to leverage the full power of this option, but it gives you a more expressive and safer way to modify the webpack config than directly mutation values.
78+
79+
### Inspecting the Project's Webpack Config
80+
81+
Since `@vue/cli-service` abstracts away the webpack config, it may be more difficult to understand what is included in the config, especially when you are trying to make tweaks yourself.
82+
83+
`vue-cli-service` exposes the `inspect` command for inspecting the resolved webpack config. The global `vue` binary also provides the `inspect` command, and it simply proxies to `vue-cli-service inspect` in your project.
84+
85+
The command prints to stdout by default, so you can redirect that into a file for easier inspection:
86+
87+
``` sh
88+
vue inspect > output.js
89+
```
90+
91+
Note the output is not a valid webpack config file, it's a serialized format only meant for inspection.
92+
93+
You can also inspect a certain path of the config to narrow it down:
94+
95+
``` sh
96+
# only inspect the first rule
97+
vue inspect module.rules.0
98+
```
699

7100
### Using Resolved Config as a File
101+
102+
Some external tools may need access to the resolved webpack config as a file, for example IDEs or command line tools that expects a webpack config path. In that case you can use the following path:
103+
104+
```
105+
<projectRoot>/node_modules/@vue/cli-service/webpack.config.js
106+
```
107+
108+
This file dynamically resolves and exports the exact same webpack config used in `vue-cli-service` commands, including those from plugins and even your custom configurations.

0 commit comments

Comments
 (0)