Skip to content

Commit 1874208

Browse files
committed
document pre/post loaders
1 parent 56e845d commit 1874208

File tree

3 files changed

+122
-74
lines changed

3 files changed

+122
-74
lines changed

docs/en/configurations/advanced.md

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,80 @@
11
# Advanced Loader Configuration
22

3-
Sometimes you may want to apply a custom loader string to a language instead of letting `vue-loader` infer it. Or you may simply want to overwrite the built-in loader configuration for the default languages. To do that, add a `vue` block in your Webpack config file, and specify the `loaders` option.
3+
Sometimes the you may want to:
44

5-
### Webpack 1.x
5+
1. Apply a custom loader string to a language instead of letting `vue-loader` infer it;
6+
7+
2. Overwrite the built-in loader configuration for the default languages;
8+
9+
3. Pre-process or post-process a specific language block with custom loaders.
10+
11+
To do that, specify the `loaders` option for `vue-loader`:
12+
13+
> Note that `preLoaders` and `postLoaders` are only supported in >=10.3.0
14+
15+
### Webpack 2.x
616

717
``` js
8-
// webpack.config.js
918
module.exports = {
1019
// other options...
1120
module: {
12-
loaders: [
21+
// module.rules is the same as module.loaders in 1.x
22+
rules: [
1323
{
1424
test: /\.vue$/,
15-
loader: 'vue'
25+
loader: 'vue-loader',
26+
options: {
27+
// `loaders` will overwrite the default loaders.
28+
// The following config will cause all <script> tags without "lang"
29+
// attribute to be loaded with coffee-loader
30+
loaders: {
31+
js: 'coffee-loader'
32+
},
33+
34+
// `preLoaders` are attached before the default loaders.
35+
// You can use this to pre-process language blocks - a common use
36+
// case would be build-time i18n.
37+
preLoaders: {
38+
js: '/path/to/custom/loader'
39+
},
40+
41+
// `postLoaders` are attached after the default loaders.
42+
//
43+
// - For `html`, the result returned by the default loader
44+
// will be compiled JavaScript render function code.
45+
//
46+
// - For `css`, the result will be returned by vue-style-loader
47+
// which isn't particularly useful in most cases. Using a postcss
48+
// plugin will be a better option.
49+
postLoaders: {
50+
html: 'babel-loader'
51+
}
52+
}
1653
}
1754
]
18-
},
19-
// vue-loader configurations
20-
vue: {
21-
// ... other vue options
22-
loaders: {
23-
// load all <script> without "lang" attribute with coffee-loader
24-
js: 'coffee-loader',
25-
// allows you to write markdown inside <template> tags...
26-
// (note this only works for 10.2.0+)
27-
html: 'marked'
28-
}
2955
}
3056
}
3157
```
3258

33-
### Webpack 2.x
59+
### Webpack 1.x
3460

3561
``` js
62+
// webpack.config.js
3663
module.exports = {
3764
// other options...
3865
module: {
39-
// module.rules is the same as module.loaders in 1.x
40-
rules: [
66+
loaders: [
4167
{
4268
test: /\.vue$/,
43-
loader: 'vue-loader',
44-
options: {
45-
loaders: {
46-
// load all <script> without "lang" attribute with coffee-loader
47-
js: 'coffee-loader',
48-
// allows you to write markdown inside <template> tags...
49-
// (note this only works for 10.2.0+)
50-
html: 'marked'
51-
}
52-
}
69+
loader: 'vue'
5370
}
5471
]
72+
},
73+
// vue-loader configurations
74+
vue: {
75+
loaders: {
76+
// same configuration rules as above
77+
}
5578
}
5679
}
5780
```

docs/en/configurations/extract-css.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
Example config to extract all the processed CSS in all Vue components into a single CSS file:
44

5-
### Webpack 1.x
5+
### Webpack 2.x
66

77
``` bash
8-
npm install extract-text-webpack-plugin --save-dev
8+
npm install extract-text-webpack-plugin@2.x --save-dev
99
```
1010

1111
``` js
@@ -15,30 +15,31 @@ var ExtractTextPlugin = require("extract-text-webpack-plugin")
1515
module.exports = {
1616
// other options...
1717
module: {
18-
loaders: [
18+
rules: [
1919
{
2020
test: /\.vue$/,
21-
loader: 'vue'
22-
},
21+
loader: 'vue-loader',
22+
options: {
23+
loaders: {
24+
css: ExtractTextPlugin.extract({
25+
loader: 'css-loader',
26+
fallbackLoader: 'vue-style-loader' // <- this is a dep of vue-loader, so no need to explicitly install if using npm3
27+
})
28+
}
29+
}
30+
}
2331
]
2432
},
25-
vue: {
26-
loaders: {
27-
css: ExtractTextPlugin.extract("css"),
28-
// you can also include <style lang="less"> or other langauges
29-
less: ExtractTextPlugin.extract("css!less")
30-
}
31-
},
3233
plugins: [
3334
new ExtractTextPlugin("style.css")
3435
]
3536
}
3637
```
3738

38-
### Webpack 2.x
39+
### Webpack 1.x
3940

4041
``` bash
41-
npm install extract-text-webpack-plugin@2.x --save-dev
42+
npm install extract-text-webpack-plugin --save-dev
4243
```
4344

4445
``` js
@@ -48,21 +49,20 @@ var ExtractTextPlugin = require("extract-text-webpack-plugin")
4849
module.exports = {
4950
// other options...
5051
module: {
51-
rules: [
52+
loaders: [
5253
{
5354
test: /\.vue$/,
54-
loader: 'vue-loader',
55-
options: {
56-
loaders: {
57-
css: ExtractTextPlugin.extract({
58-
loader: 'css-loader',
59-
fallbackLoader: 'vue-style-loader' // <- this is a dep of vue-loader, so no need to explicitly install if using npm3
60-
})
61-
}
62-
}
63-
}
55+
loader: 'vue'
56+
},
6457
]
6558
},
59+
vue: {
60+
loaders: {
61+
css: ExtractTextPlugin.extract("css"),
62+
// you can also include <style lang="less"> or other langauges
63+
less: ExtractTextPlugin.extract("css!less")
64+
}
65+
},
6666
plugins: [
6767
new ExtractTextPlugin("style.css")
6868
]

docs/en/options.md

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,7 @@
22

33
## Usage Difference Between Webpack 1 & 2
44

5-
For Webpack 1.x: add a root `vue` block in your Webpack config:
6-
7-
``` js
8-
module.exports = {
9-
// ...
10-
vue: {
11-
// vue-loader options
12-
}
13-
}
14-
```
15-
16-
For Webpack 2 (^2.1.0-beta.25):
5+
For Webpack 2: pass the options directly to the loader rule.
176

187
``` js
198
module.exports = {
@@ -32,11 +21,22 @@ module.exports = {
3221
}
3322
```
3423

24+
For Webpack 1.x: add a root `vue` block in your Webpack config.
25+
26+
``` js
27+
module.exports = {
28+
// ...
29+
vue: {
30+
// vue-loader options
31+
}
32+
}
33+
```
34+
3535
### loaders
3636

37-
- type: `Object`
37+
- type: `{ [lang: string]: string }`
3838

39-
An object specifying Webpack loaders to use for language blocks inside `*.vue` files. The key corresponds to the `lang` attribute for language blocks, if specified. The default `lang` for each type is:
39+
An object specifying Webpack loaders to overwrite the default loaders used for language blocks inside `*.vue` files. The key corresponds to the `lang` attribute for language blocks, if specified. The default `lang` for each type is:
4040

4141
- `<template>`: `html`
4242
- `<script>`: `js`
@@ -45,18 +45,43 @@ module.exports = {
4545
For example, to use `babel-loader` and `eslint-loader` to process all `<script>` blocks:
4646

4747
``` js
48-
// ...
49-
vue: {
50-
loaders: {
51-
js: 'babel!eslint'
52-
}
48+
// Webpack 2.x config
49+
module: {
50+
rules: [
51+
{
52+
test: /\.vue$/,
53+
loader: 'vue-loader',
54+
options: {
55+
loaders: {
56+
js: 'babel-loader!eslint-loader'
57+
}
58+
}
59+
}
60+
]
5361
}
5462
```
5563

64+
### preLoaders
65+
66+
- type: `{ [lang: string]: string }`
67+
- only supported in >=10.3.0
68+
69+
The config format is the same as `loaders`, but `preLoaders` are applied to corresponding language blocks before the default loaders. You can use this to pre-process language blocks - a common use case would be build-time i18n.
70+
71+
### postLoaders
72+
73+
- type: `{ [lang: string]: string }`
74+
- only supported in >=10.3.0
75+
76+
The config format is the same as `loaders`, but `postLoaders` are applied after the default loaders. You can use this to post-process language blocks. However note that this is a bit more complicated:
77+
78+
- For `html`, the result returned by the default loader will be compiled JavaScript render function code.
79+
80+
- For `css`, the result will be returned by `vue-style-loader` which isn't particularly useful in most cases. Using a postcss plugin will be a better option.
81+
5682
### postcss
5783

5884
- type: `Array` or `Function` or `Object`
59-
- `Object` format only supported in ^8.5.0
6085

6186
Specify custom PostCSS plugins to be applied to CSS inside `*.vue` files. If using a function, the function will called using the same loader context and should return an Array of plugins.
6287

0 commit comments

Comments
 (0)