Skip to content

Commit 72ea25f

Browse files
committed
docs: update for v15
1 parent 27c60e0 commit 72ea25f

13 files changed

+479
-325
lines changed

Diff for: docs/.vuepress/config.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ module.exports = {
2727
sidebar: [
2828
'/',
2929
'/guide/',
30-
'/guide/pre-processors',
3130
'/guide/asset-url',
31+
'/guide/pre-processors',
3232
'/guide/scoped-css',
3333
'/guide/css-modules',
34-
'/guide/postcss',
3534
'/guide/hot-reload',
3635
'/guide/functional',
37-
'/guide/extract-css',
3836
'/guide/custom-blocks',
37+
'/guide/extract-css',
3938
'/guide/linting',
4039
'/guide/testing'
4140
]

Diff for: docs/.vuepress/public/_redirects

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# redirect v14 urls to root
2-
/en/start/* /guide/:splat
3-
/en/features/* /guide/:splat
4-
/en/configurations/* /guide/:splat
5-
/en/workflow/* /guide/:splat
2+
/en/features/postcss.html /guide/pre-processors.html#postcss
3+
/en/start/* /guide/:splat
4+
/en/features/* /guide/:splat
5+
/en/configurations/* /guide/:splat
6+
/en/workflow/* /guide/:splat
67

78
# redirect any other langs to legacy
89
/zh-cn/* https://vue-loader-v14.vuejs.org/zh-cn/:splat

Diff for: docs/guide/README.md

+73-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,73 @@
1-
# Basic Configuration
1+
# Getting Started
2+
3+
## Vue CLI
4+
5+
If you are not interested in manually setting up webpack, it is recommended to scaffold a project with [Vue CLI](https://github.com/vuejs/vue-cli) instead. Projects created by Vue CLI is pre-configured with most of the common development needs and works out of the box.
6+
7+
Follow this guide if the built-in configuration of Vue CLI does not suit your needs, or you'd rather create your own webpack config from scratch.
8+
9+
## Manual Configuration
10+
11+
Vue Loader's configuration is a bit different form other loaders. In addition to a rule that applies `vue-loader` to any files with extension `.vue`, make sure to add Vue Loader's plugin to your webpack config:
12+
13+
``` js
14+
// webpack.config.js
15+
const { VueLoaderPlugin } = require('vue-loader')
16+
17+
module.exports = {
18+
module: {
19+
rules: [
20+
// ... other rules
21+
{
22+
test: /\.vue$/,
23+
loader: 'vue-loader'
24+
}
25+
]
26+
},
27+
plugins: [
28+
// make sure to include the plugin!
29+
new VueLoaderPlugin()
30+
]
31+
}
32+
```
33+
34+
**The plugin is required.** It is responsible for cloning any other rules you have defined and applying them to the corresponding language blocks in `.vue` files. For example, if you have a rule matching `/\.js$/`, it will be applied to `<script>` blocks in `.vue` files.
35+
36+
A more complete example webpack config will look like this:
37+
38+
``` js
39+
// webpack.config.js
40+
const path = require('path')
41+
const { VueLoaderPlugin } = require('vue-loader')
42+
43+
module.exports = {
44+
mode: 'development',
45+
module: {
46+
rules: [
47+
{
48+
test: /\.vue$/,
49+
loader: 'vue-loader'
50+
},
51+
// this will apply to both plain .js files
52+
// AND <script> blocks in vue files
53+
{
54+
test: /\.js$/,
55+
loader: 'babel-loader'
56+
},
57+
// this will apply to both plain .css files
58+
// AND <style> blocks in vue files
59+
{
60+
test: /\.css$/,
61+
use: [
62+
'vue-style-loader',
63+
'css-loader'
64+
]
65+
}
66+
]
67+
},
68+
plugins: [
69+
// make sure to include the plugin for the magic
70+
new VueLoaderPlugin()
71+
]
72+
}
73+
```

Diff for: docs/guide/asset-url.md

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
# Asset URL Handling
22

3-
By default, `vue-loader` automatically processes your style and template files with [css-loader](https://github.com/webpack/css-loader) and the Vue template compiler. In this compilation process, all asset URLs such as `<img src="...">`, `background: url(...)` and CSS `@import` are **resolved as module dependencies**.
3+
When Vue Loader compiles the `<template>` blocks in SFCs, it also converts any encountered asset URLs into **webpack module requests**.
44

5-
For example, `url(./image.png)` will be translated into `require('./image.png')`, and
5+
For example, the following template snippet
66

7-
``` html
7+
``` vue
88
<img src="../image.png">
99
```
1010

1111
will be compiled into:
1212

1313
``` js
14-
createElement('img', { attrs: { src: require('../image.png') }})
14+
createElement('img', {
15+
attrs: {
16+
src: require('../image.png') // this is now a module request
17+
}
18+
})
1519
```
1620

17-
### Transform Rules
21+
By default the following tag/attribute combinations are transformed, and can be configured using the [transformAssetUrls](../config.md#transformasseturls) option.
22+
23+
In addition, if you have configured to use [css-loader](https://github.com/webpack-contrib/css-loader) for the `<style>` blocks, asset URLs in your CSS will also be processed in a similar fashion.
24+
25+
## Transform Rules
26+
27+
Asset URL transforms adhere to the following rules:
1828

1929
- If the URL is an absolute path (e.g. `/images/foo.png`), it will be preserved as-is.
2030

@@ -26,15 +36,15 @@ createElement('img', { attrs: { src: require('../image.png') }})
2636
<img src="~some-npm-package/foo.png">
2737
```
2838

29-
- (13.7.0+) If the URL starts with `@`, it's also interpreted as a module request. This is useful if your webpack config has an alias for `@`, which by default points to `/src` in any project created by `vue-cli`.
39+
- If the URL starts with `@`, it's also interpreted as a module request. This is useful if your webpack config has an alias for `@`, which by default points to `/src` in any project created by `vue-cli`.
3040

31-
### Related Loaders
41+
## Related Loaders
3242

33-
Because `.png` is not a JavaScript file, you will need to configure webpack to use [file-loader](https://github.com/webpack/file-loader) or [url-loader](https://github.com/webpack/url-loader) to handle them. The project scaffolded with `vue-cli` has also configured this for you.
43+
Because extensions like `.png` are not JavaScript modules, you will need to configure webpack to use [file-loader](https://github.com/webpack/file-loader) or [url-loader](https://github.com/webpack/url-loader) to properly handle them. Projects created with Vue CLI has this pre-configured.
3444

35-
### Why
45+
## Why
3646

37-
The benefits of all this are:
47+
The benefits of asset URL transforms are:
3848

3949
1. `file-loader` allows you to designate where to copy and place the asset file, and how to name it using version hashes for better caching. Moreover, this also means **you can just place images next to your `*.vue` files and use relative paths based on the folder structure instead of worrying about deployment URLs**. With proper config, webpack will auto-rewrite the file paths into correct URLs in the bundled output.
4050

Diff for: docs/guide/css-modules.md

+89-41
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
# CSS Modules
22

3-
> requires ^9.8.0
4-
53
[CSS Modules](https://github.com/css-modules/css-modules) is a popular system for modularizing and composing CSS. `vue-loader` provides first-class integration with CSS Modules as an alternative for simulated scoped CSS.
64

7-
### Usage
5+
## Usage
86

9-
Just add the `module` attribute to your `<style>`:
7+
First, CSS Modules must be enabled by passing `modules: true` to `css-loader`:
108

11-
``` html
9+
``` js
10+
// webpack.config.js
11+
{
12+
module: {
13+
rules: [
14+
// ... other rules omitted
15+
{
16+
test: /\.css$/,
17+
use: [
18+
'vue-style-loader',
19+
{
20+
loader: 'css-loader',
21+
options: {
22+
// enable CSS Modules
23+
modules: true,
24+
// customize generated class names
25+
localIdentName: '[local]_[hash:base64:8]'
26+
}
27+
}
28+
]
29+
}
30+
]
31+
}
32+
}
33+
```
34+
35+
Then, add the `module` attribute to your `<style>`:
36+
37+
``` vue
1238
<style module>
1339
.red {
1440
color: red;
@@ -19,9 +45,9 @@ Just add the `module` attribute to your `<style>`:
1945
</style>
2046
```
2147

22-
This will turn on CSS Modules mode for `css-loader`, and the resulting class identifier object will be injected into the component as a computed property with the name `$style`. You can use it in your templates with a dynamic class binding:
48+
The `module` attribute instructs Vue Loader to inject the CSS modules locals object into the component as a computed property with the name `$style`. You can then use it in your templates with a dynamic class binding:
2349

24-
``` html
50+
``` vue
2551
<template>
2652
<p :class="$style.red">
2753
This should be red
@@ -31,7 +57,7 @@ This will turn on CSS Modules mode for `css-loader`, and the resulting class ide
3157

3258
Since it's a computed property, it also works with the object/array syntax of `:class`:
3359

34-
``` html
60+
``` vue
3561
<template>
3662
<div>
3763
<p :class="{ [$style.red]: isRed }">
@@ -46,12 +72,12 @@ Since it's a computed property, it also works with the object/array syntax of `:
4672

4773
And you can also access it from JavaScript:
4874

49-
``` html
75+
``` vue
5076
<script>
5177
export default {
5278
created () {
5379
console.log(this.$style.red)
54-
// -> "_1VyoJ-uZOjlOxP7jWUy19_0"
80+
// -> "red_1VyoJ-uZ"
5581
// an identifier generated based on filename and className.
5682
}
5783
}
@@ -60,47 +86,69 @@ export default {
6086

6187
Refer to the [CSS Modules spec](https://github.com/css-modules/css-modules) for mode details such as [global exceptions](https://github.com/css-modules/css-modules#exceptions) and [composition](https://github.com/css-modules/css-modules#composition).
6288

63-
### Custom Inject Name
64-
65-
You can have more than one `<style>` tags in a single `*.vue` component. To avoid injected styles to overwrite each other, you can customize the name of the injected computed property by giving the `module` attribute a value:
66-
67-
``` html
68-
<style module="a">
69-
/* identifiers injected as a */
70-
</style>
71-
72-
<style module="b">
73-
/* identifiers injected as b */
74-
</style>
75-
```
89+
## Opt-in Usage
7690

77-
### Configuring `css-loader` Query
78-
79-
CSS Modules are processed via [css-loader](https://github.com/webpack/css-loader). With `<style module>`, the default query used for `css-loader` is:
91+
If you only want to use CSS Modules in some of your Vue components, you can use a `oneOf` rule and check for the `module` string in resourceQuery:
8092

8193
``` js
94+
// webpack.config.js -> module.rules
8295
{
83-
modules: true,
84-
importLoaders: 1,
85-
localIdentName: '[hash:base64]'
96+
test: /\.css$/,
97+
oneOf: [
98+
// this matches <style module>
99+
{
100+
resourceQuery: /module/,
101+
use: [
102+
'vue-style-loader',
103+
{
104+
loader: 'css-loader',
105+
options: {
106+
modules: true,
107+
localIdentName: '[local]_[hash:base64:5]'
108+
}
109+
}
110+
]
111+
},
112+
// this matches plain <style> or <style scoped>
113+
{
114+
use: [
115+
'vue-style-loader',
116+
'css-loader'
117+
]
118+
}
119+
]
86120
}
87121
```
88122

89-
You can use vue-loader's `cssModules` option to provide additional query options to `css-loader`:
123+
## Using with Pre-Processors
124+
125+
CSS Modules can be used along with other pre-processors:
90126

91127
``` js
92-
module: {
93-
rules: [
128+
// webpack.config.js -> module.rules
129+
{
130+
test: /\.scss$/,
131+
use: [
132+
'vue-style-loader',
94133
{
95-
test: '\.vue$',
96-
loader: 'vue-loader',
97-
options: {
98-
cssModules: {
99-
localIdentName: '[path][name]---[local]---[hash:base64:5]',
100-
camelCase: true
101-
}
102-
}
103-
}
134+
loader: 'css-loader',
135+
options: { modules: true }
136+
},
137+
'sass-loader'
104138
]
105139
}
106140
```
141+
142+
## Custom Inject Name
143+
144+
You can have more than one `<style>` tags in a single `*.vue` component. To avoid injected styles to overwrite each other, you can customize the name of the injected computed property by giving the `module` attribute a value:
145+
146+
``` html
147+
<style module="a">
148+
/* identifiers injected as a */
149+
</style>
150+
151+
<style module="b">
152+
/* identifiers injected as b */
153+
</style>
154+
```

0 commit comments

Comments
 (0)