Skip to content

Commit ca77b66

Browse files
committed
[docs][zh] init
1 parent 1204381 commit ca77b66

15 files changed

+1380
-0
lines changed

docs/zh/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Introduction
2+
3+
:::tip VERSION NOTE
4+
This is the documentation for Vue Loader v15 and above. If you are upgrading from v14 or an earlier version, check out the [Migration Guide](../migrating.md). If you are using an older version, the old docs are [here](https://vue-loader-v14.vuejs.org).
5+
:::
6+
7+
## What is Vue Loader?
8+
9+
`vue-loader` is a loader for [webpack](https://webpack.js.org/) that allows you to author Vue components in a format called [Single-File Components (SFCs)](./spec.md):
10+
11+
``` vue
12+
<template>
13+
<div class="example">{{ msg }}</div>
14+
</template>
15+
16+
<script>
17+
export default {
18+
data () {
19+
return {
20+
msg: 'Hello world!'
21+
}
22+
}
23+
}
24+
</script>
25+
26+
<style>
27+
.example {
28+
color: red;
29+
}
30+
</style>
31+
```
32+
33+
There are many cool features provided by `vue-loader`:
34+
35+
- Allows using other webpack loaders for each part of a Vue component, for example Sass for `<style>` and Pug for `<template>`;
36+
- Allows custom blocks in a `.vue` file that can have custom loader chains applied to them;
37+
- Treat static assets referenced in `<style>` and `<template>` as module dependencies and handle them with webpack loaders;
38+
- Simulate scoped CSS for each component;
39+
- State-preserving hot-reloading during development.
40+
41+
In a nutshell, the combination of webpack and `vue-loader` gives you a modern, flexible and extremely powerful front-end workflow for authoring Vue.js applications.

docs/zh/guide/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 are pre-configured with most of the common development needs working 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+
```
74+
75+
Also see [Options Reference](../options.md) for all available loader options.

docs/zh/guide/asset-url.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Asset URL Handling
2+
3+
When Vue Loader compiles the `<template>` blocks in SFCs, it also converts any encountered asset URLs into **webpack module requests**.
4+
5+
For example, the following template snippet
6+
7+
``` vue
8+
<img src="../image.png">
9+
```
10+
11+
will be compiled into:
12+
13+
``` js
14+
createElement('img', {
15+
attrs: {
16+
src: require('../image.png') // this is now a module request
17+
}
18+
})
19+
```
20+
21+
By default the following tag/attribute combinations are transformed, and can be configured using the [transformAssetUrls](../options.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:
28+
29+
- If the URL is an absolute path (e.g. `/images/foo.png`), it will be preserved as-is.
30+
31+
- If the URL starts with `.`, it's interpreted as a relative module request and resolved based on the folder structure on your file system.
32+
33+
- If the URL starts with `~`, anything after it is interpreted as a module request. This means you can even reference assets inside node modules:
34+
35+
``` html
36+
<img src="~some-npm-package/foo.png">
37+
```
38+
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`.
40+
41+
## Related Loaders
42+
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.
44+
45+
## Why
46+
47+
The benefits of asset URL transforms are:
48+
49+
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.
50+
51+
2. `url-loader` allows you to conditionally inline a file as base-64 data URL if they are smaller than a given threshold. This can reduce the amount of HTTP requests for trivial files. If the file is larger than the threshold, it automatically falls back to `file-loader`.

docs/zh/guide/css-modules.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# CSS Modules
2+
3+
[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.
4+
5+
## Usage
6+
7+
First, CSS Modules must be enabled by passing `modules: true` to `css-loader`:
8+
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
38+
<style module>
39+
.red {
40+
color: red;
41+
}
42+
.bold {
43+
font-weight: bold;
44+
}
45+
</style>
46+
```
47+
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:
49+
50+
``` vue
51+
<template>
52+
<p :class="$style.red">
53+
This should be red
54+
</p>
55+
</template>
56+
```
57+
58+
Since it's a computed property, it also works with the object/array syntax of `:class`:
59+
60+
``` vue
61+
<template>
62+
<div>
63+
<p :class="{ [$style.red]: isRed }">
64+
Am I red?
65+
</p>
66+
<p :class="[$style.red, $style.bold]">
67+
Red and bold
68+
</p>
69+
</div>
70+
</template>
71+
```
72+
73+
And you can also access it from JavaScript:
74+
75+
``` vue
76+
<script>
77+
export default {
78+
created () {
79+
console.log(this.$style.red)
80+
// -> "red_1VyoJ-uZ"
81+
// an identifier generated based on filename and className.
82+
}
83+
}
84+
</script>
85+
```
86+
87+
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).
88+
89+
## Opt-in Usage
90+
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:
92+
93+
``` js
94+
// webpack.config.js -> module.rules
95+
{
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+
]
120+
}
121+
```
122+
123+
## Using with Pre-Processors
124+
125+
CSS Modules can be used along with other pre-processors:
126+
127+
``` js
128+
// webpack.config.js -> module.rules
129+
{
130+
test: /\.scss$/,
131+
use: [
132+
'vue-style-loader',
133+
{
134+
loader: 'css-loader',
135+
options: { modules: true }
136+
},
137+
'sass-loader'
138+
]
139+
}
140+
```
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)