Skip to content

Commit 698a79d

Browse files
authored
Merge pull request #1274 from Jinjiang/zh-cn
[docs] Chinese translation
2 parents 807f193 + b556598 commit 698a79d

17 files changed

+1475
-33
lines changed

docs/.vuepress/config.js

Lines changed: 86 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,96 @@
11
module.exports = {
2-
title: 'Vue Loader',
3-
description: 'Webpack loader for single-file Vue components',
2+
locales: {
3+
'/': {
4+
lang: 'en-US',
5+
title: 'Vue Loader',
6+
description: 'Webpack loader for single-file Vue components'
7+
},
8+
'/zh/': {
9+
lang: 'zh-CN',
10+
title: 'Vue Loader',
11+
description: '处理 Vue 单文件组件的 Webpack loader'
12+
}
13+
},
414
serviceWorker: true,
515
theme: 'vue',
616
themeConfig: {
717
repo: 'vuejs/vue-loader',
818
docsDir: 'docs',
9-
nav: [
10-
{
11-
text: 'Guide',
12-
link: '/guide/'
19+
locales: {
20+
'/': {
21+
label: 'English',
22+
selectText: 'Languages',
23+
editLinkText: 'Edit this page on GitHub',
24+
nav: [
25+
{
26+
text: 'Guide',
27+
link: '/guide/'
28+
},
29+
{
30+
text: 'SFC Spec',
31+
link: '/spec.html'
32+
},
33+
{
34+
text: 'Options Reference',
35+
link: '/options.html'
36+
},
37+
{
38+
text: 'Migrating from v14',
39+
link: '/migrating.md'
40+
}
41+
],
42+
sidebar: [
43+
'/',
44+
'/guide/',
45+
'/guide/asset-url',
46+
'/guide/pre-processors',
47+
'/guide/scoped-css',
48+
'/guide/css-modules',
49+
'/guide/hot-reload',
50+
'/guide/functional',
51+
'/guide/custom-blocks',
52+
'/guide/extract-css',
53+
'/guide/linting',
54+
'/guide/testing'
55+
]
1356
},
14-
{
15-
text: 'SFC Spec',
16-
link: '/spec.html'
17-
},
18-
{
19-
text: 'Options Reference',
20-
link: '/options.html'
21-
},
22-
{
23-
text: 'Migrating from v14',
24-
link: '/migrating.md'
57+
'/zh/': {
58+
label: '简体中文',
59+
selectText: '选择语言',
60+
editLinkText: '在 GitHub 上编辑此页',
61+
nav: [
62+
{
63+
text: '指南',
64+
link: '/zh/guide/'
65+
},
66+
{
67+
text: '单文件组件规范',
68+
link: '/zh/spec.html'
69+
},
70+
{
71+
text: '选项参考',
72+
link: '/zh/options.html'
73+
},
74+
{
75+
text: '如何从 v14 迁移',
76+
link: '/zh/migrating.md'
77+
}
78+
],
79+
sidebar: [
80+
'/zh/',
81+
'/zh/guide/',
82+
'/zh/guide/asset-url',
83+
'/zh/guide/pre-processors',
84+
'/zh/guide/scoped-css',
85+
'/zh/guide/css-modules',
86+
'/zh/guide/hot-reload',
87+
'/zh/guide/functional',
88+
'/zh/guide/custom-blocks',
89+
'/zh/guide/extract-css',
90+
'/zh/guide/linting',
91+
'/zh/guide/testing'
92+
]
2593
}
26-
],
27-
sidebar: [
28-
'/',
29-
'/guide/',
30-
'/guide/asset-url',
31-
'/guide/pre-processors',
32-
'/guide/scoped-css',
33-
'/guide/css-modules',
34-
'/guide/hot-reload',
35-
'/guide/functional',
36-
'/guide/custom-blocks',
37-
'/guide/extract-css',
38-
'/guide/linting',
39-
'/guide/testing'
40-
]
94+
}
4195
}
4296
}

docs/guide/asset-url.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
When Vue Loader compiles the `<template>` blocks in SFCs, it also converts any encountered asset URLs into **webpack module requests**.
44

5-
For example, the following template snippet
5+
For example, the following template snippet:
66

77
``` vue
88
<img src="../image.png">

docs/zh/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 介绍
2+
3+
:::tip 版本说明
4+
这份文档是为 Vue Loader v15 及以上版本撰写的。如果你正在从 v14 或更早的版本往这里迁移,请查阅[迁移指南](../migrating.md)。如果你正在使用老版本,其对应的文档[在此](https://vue-loader-v14.vuejs.org)
5+
:::
6+
7+
## Vue Loader 是什么?
8+
9+
Vue Loader 是一个 [webpack](https://webpack.js.org/) 的 loader,它允许你以一种名为[单文件组件 (SFCs)](./spec.md)的格式撰写 Vue 组件:
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+
Vue Loader 还提供了很多酷炫的特性:
34+
35+
- 允许为 Vue 组件的每个部分使用其它的 webpack loader,例如在 `<style>` 的部分使用 Sass 和在 `<template>` 的部分使用 Pug;
36+
- 允许在一个 `.vue` 文件中使用自定义块,并对其运用自定义的 loader 链;
37+
- 使用 webpack loader 将 `<style>``<template>` 中引用的资源当作模块依赖来处理;
38+
- 为每个组件模拟出 scoped CSS;
39+
- 在开发过程中使用热重载来保持状态。
40+
41+
简而言之,webpack 和 Vue Loader 的结合为你提供了一个现代、灵活且极其强大的前端工作流,来帮助撰写 Vue.js 应用。

docs/zh/guide/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 起步
2+
3+
## Vue CLI
4+
5+
如果你不想手动设置 webpack,我们推荐使用 [Vue CLI](https://github.com/vuejs/vue-cli) 直接创建一个项目的脚手架。通过 Vue CLI 创建的项目会针对多数常见的开发需求进行预先配置,做到开箱即用。
6+
7+
如果 Vue CLI 提供的内建没有满足你的需求,或者你乐于从零开始创建你自己的 webpack 配置,那么请继续阅读这篇指南。
8+
9+
## 手动配置
10+
11+
Vue Loader 的配置和其它的 loader 不太一样。除了通过一条规则将 `vue-loader` 应用到所有扩展名为 `.vue` 的文件上之外,请确保在你的 webpack 配置中添加 Vue Loader 的插件:
12+
13+
``` js
14+
// webpack.config.js
15+
const { VueLoaderPlugin } = require('vue-loader')
16+
17+
module.exports = {
18+
module: {
19+
rules: [
20+
// ... 其它规则
21+
{
22+
test: /\.vue$/,
23+
loader: 'vue-loader'
24+
}
25+
]
26+
},
27+
plugins: [
28+
// 请确保引入这个插件!
29+
new VueLoaderPlugin()
30+
]
31+
}
32+
```
33+
34+
**这个插件是必须的!**它的职责是将你定义过的其它规则复制并应用到 `.vue` 文件里相应语言的块。例如,如果你有一条匹配 `/\.js$/` 的规则,那么它会应用到 `.vue` 文件里的 `<script>` 块。
35+
36+
一个更完整的 webpack 配置示例看起来像这样:
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+
// 它会应用到普通的 `.js` 文件
52+
// 以及 `.vue` 文件中的 `<script>` 块
53+
{
54+
test: /\.js$/,
55+
loader: 'babel-loader'
56+
},
57+
// 它会应用到普通的 `.css` 文件
58+
// 以及 `.vue` 文件中的 `<style>` 块
59+
{
60+
test: /\.css$/,
61+
use: [
62+
'vue-style-loader',
63+
'css-loader'
64+
]
65+
}
66+
]
67+
},
68+
plugins: [
69+
// 请确保引入这个插件来施展魔法
70+
new VueLoaderPlugin()
71+
]
72+
}
73+
```
74+
75+
你也可以在[选项参考](../options.md)查阅所有可用的 loader 选项。

docs/zh/guide/asset-url.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 处理资源路径
2+
3+
当 Vue Loader 编译单文件组件中的 `<template>` 块时,它也会将所有遇到的资源 URL 转换为 **webpack 模块请求**
4+
5+
例如,下面的模板代码片段:
6+
7+
``` vue
8+
<img src="../image.png">
9+
```
10+
11+
将会被编译成为:
12+
13+
``` js
14+
createElement('img', {
15+
attrs: {
16+
src: require('../image.png') // 现在这是一个模块的请求了
17+
}
18+
})
19+
```
20+
21+
默认下列标签/特性的组合会被转换,且这些组合时可以使用 [transformAssetUrls](../options.md#transformasseturls) 选项进行配置的。
22+
23+
``` js
24+
{
25+
video: ['src', 'poster'],
26+
source: 'src',
27+
img: 'src',
28+
image: 'xlink:href'
29+
}
30+
```
31+
32+
此外,如果你配置了为 `<style>` 块使用 [css-loader](https://github.com/webpack-contrib/css-loader),则你的 CSS 中的资源 URL 也会被同等处理。
33+
34+
## 转换规则
35+
36+
资源 URL 转换会遵循如下规则:
37+
38+
- 如果路径是绝对路径 (例如 `/images/foo.png`),会原样保留。
39+
40+
- 如果路径以 `.` 开头,将会被看作相对的模块依赖,并按照你的本地文件系统上的目录结构进行解析。
41+
42+
- 如果路径以 `~` 开头,其后的部分将会被看作模块依赖。这意味着你可以用该特性来引用一个 Node 依赖中的资源:
43+
44+
``` html
45+
<img src="~some-npm-package/foo.png">
46+
```
47+
48+
- 如果路径以 `@` 开头,也会被看作模块依赖。如果你的 webpack 配置中给 `@` 配置了 alias,这就很有用了。所有 `vue-cli` 创建的项目都默认配置了将 `@` 指向 `/src`
49+
50+
## 相关的 Loader
51+
52+
因为像 `.png` 这样的文件不是一个 JavaScript 模块,你需要配置 webpack 使用 [file-loader](https://github.com/webpack/file-loader) 或者 [url-loader](https://github.com/webpack/url-loader) 去合理地处理它们。通过 Vue CLI 创建的项目已经把这些预配置好了。
53+
54+
## 为什么
55+
56+
转换资源 URL 的好处是:
57+
58+
1. `file-loader` 可以指定要复制和放置资源文件的位置,以及如何使用版本哈希命名以获得更好的缓存。此外,这意味着 **你可以就近管理图片文件,可以使用相对路径而不用担心部署时 URL 的问题**。使用正确的配置,webpack 将会在打包输出中自动重写文件路径为正确的 URL。
59+
60+
2. `url-loader` 允许你有条件地将文件转换为内联的 base-64 URL (当文件小于给定的阈值),这会减少小文件的 HTTP 请求数。如果文件大于该阈值,会自动的交给 `file-loader` 处理。

0 commit comments

Comments
 (0)