|
1 | 1 | # Advanced Loader Configuration
|
2 | 2 |
|
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: |
4 | 4 |
|
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 |
6 | 16 |
|
7 | 17 | ``` js
|
8 |
| -// webpack.config.js |
9 | 18 | module.exports = {
|
10 | 19 | // other options...
|
11 | 20 | module: {
|
12 |
| - loaders: [ |
| 21 | + // module.rules is the same as module.loaders in 1.x |
| 22 | + rules: [ |
13 | 23 | {
|
14 | 24 | 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 | + } |
16 | 53 | }
|
17 | 54 | ]
|
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', |
25 |
| - // load <template> directly as HTML string, without piping it |
26 |
| - // through vue-html-loader first |
27 |
| - html: 'raw' |
28 |
| - } |
29 | 55 | }
|
30 | 56 | }
|
31 | 57 | ```
|
32 | 58 |
|
33 |
| -### Webpack 2.x (^2.1.0-beta.25) |
| 59 | +### Webpack 1.x |
34 | 60 |
|
35 | 61 | ``` js
|
| 62 | +// webpack.config.js |
36 | 63 | module.exports = {
|
37 | 64 | // other options...
|
38 | 65 | module: {
|
39 |
| - // module.rules is the same as module.loaders in 1.x |
40 |
| - rules: [ |
| 66 | + loaders: [ |
41 | 67 | {
|
42 | 68 | test: /\.vue$/,
|
43 |
| - loader: 'vue-loader', |
44 |
| - // vue-loader options goes here |
45 |
| - options: { |
46 |
| - loaders: { |
47 |
| - // ... |
48 |
| - } |
49 |
| - } |
| 69 | + loader: 'vue' |
50 | 70 | }
|
51 | 71 | ]
|
| 72 | + }, |
| 73 | + // vue-loader configurations |
| 74 | + vue: { |
| 75 | + loaders: { |
| 76 | + // same configuration rules as above |
| 77 | + } |
52 | 78 | }
|
53 | 79 | }
|
54 | 80 | ```
|
|
0 commit comments