File tree Expand file tree Collapse file tree 19 files changed +1304
-0
lines changed
Expand file tree Collapse file tree 19 files changed +1304
-0
lines changed Original file line number Diff line number Diff line change 1+ # 介绍
2+
3+ ### ` vue-loader ` 是什么?
4+
5+ ` vue-loader ` 是一个 Webpack 的 loader,可以将用下面这个格式编写的 Vue 组件转换为 JavaScript 模块:
6+
7+ ![ screenshot] ( http://blog.evanyou.me/images/vue-component.png )
8+
9+ 这里有一些 ` vue-loader ` 提供的很酷的特性:
10+
11+ - ES2015 默认支持;
12+ - 允许对 Vue 组件的组成部分使用其它 Webpack loaders,比如对 ` <style> ` 使用 SASS 和对 ` <template> ` 使用 Jade;
13+ - ` .vue ` 文件中允许自定义节点,然后使用自定义的 loader 处理他们;
14+ - 把 ` <style> ` 和 ` <template> ` 中的静态资源当作模块来对待,并使用 Webpack loaders 进行处理;
15+ - 对每个组件模拟出 CSS 作用域;
16+ - 支持开发期组件的热重载。
17+
18+ 简而言之,编写 Vue.js 应用程序时,组合使用 Webpack 和 ` vue-loader ` 能带来一个现代,灵活并且非常强大的前端工作流程。
19+
20+ ### Webpack 是什么?
21+
22+ 如果你已经熟悉了 Webpack,随时可以跳过下面的说明。如果你没有使用过 Webpack,下面是一个快速介绍:
23+
24+ [ Webpack] ( http://webpack.github.io/ ) 是一个模块打包工具。它将一堆文件中的每个文件都作为一个模块,找出他们的依赖关系,将它们打包为可部署的静态资源。
25+
26+ ![ webpack] ( http://webpack.github.io/assets/what-is-webpack.png )
27+
28+ 一个基本的例子,想像我们有一些 CommonJS 模块,它不能直接在浏览器中运行,所以我们需要打包成一个文件,这样就可以通过` <script> ` 标签加载。Webpack 可以遵循 ` require() ` 调用的依赖关系,为我们完成这些工作。
29+
30+ 但是 Webpack 可以做的不止这些。使用 "loaders",我们可以配置 Webpack 以任何方式去转换所有类型的文件。一些例子包括:
31+
32+ - 转换 ES2015,CoffeeScript 或者 TypeScript 模块为普通的 ES5 CommonJS 模块;
33+ - 可以选择在编译之前检验你的源代码;
34+ - 将 Jade 模版转换为纯 HTML 并且嵌入 Javascript 字符串中;
35+ - 将 SASS 文件转换为纯 CSS,then convert it into a JavaScript snippet that insert the resulting CSS as a ` <style> ` tag;
36+ - 处理 HTML 或者 CSS 中引用的图片,移动到配置的路径中,并且使用 md5 hash 重命名。
37+
38+ 当你理解 Webpack 原理后会感觉它是如此强大,它可以大大优化你的前端工作流程。它主要的缺点是配置复杂麻烦,但是使用本指南, Vue.js 和 ` vue-loader ` 使用时的常见问题,应该可以帮助你找到解决方案。
Original file line number Diff line number Diff line change 1+ - 开始
2+ - [ Vue 组件细则] ( start/spec.md )
3+ - [ 创建项目] ( start/setup.md )
4+ - 特性
5+ - [ ES2015] ( features/es2015.md )
6+ - [ CSS 作用域] ( features/scoped-css.md )
7+ - [ CSS 模块] ( features/css-modules.md )
8+ - [ PostCSS] ( features/postcss.md )
9+ - [ 热重载] ( features/hot-reload.md )
10+ - 配置
11+ - [ 预处理器] ( configurations/pre-processors.md )
12+ - [ 处理资源路径] ( configurations/asset-url.md )
13+ - [ 进阶配置] ( configurations/advanced.md )
14+ - [ 提取 CSS 文件] ( configurations/extract-css.md )
15+ - [ 自定义块] ( configurations/custom-blocks.md )
16+ - 工作流程
17+ - [ 生产环境构建] ( workflow/production.md )
18+ - [ 代码检验] ( workflow/linting.md )
19+ - [ 测试] ( workflow/testing.md )
20+ - [ 使用 Mocks 测试] ( workflow/testing-with-mocks.md )
21+ - [ 选项参考] ( options.md )
Original file line number Diff line number Diff line change 1+ # Loader 进阶配置
2+
3+ 有些时候你想要这样:
4+
5+ 1 . 对语言应用自定义 loader string,而不是让 ` vue-loader ` 去推断;
6+
7+ 2 . 覆盖默认语言的内置配置。
8+
9+ 3 . 默认语言预处理或者后处理配置。
10+
11+ 为了实现这些,详细说明 ` vue-loader ` 的 ` loaders ` 选项:
12+
13+ > 注意 ` preLoaders ` 和 ` postLoaders ` 只在版本 >=10.3.0 支持
14+
15+ ### Webpack 2.x
16+
17+ ``` js
18+ module .exports = {
19+ // other options...
20+ module: {
21+ // module.rules is the same as module.loaders in 1.x
22+ rules: [
23+ {
24+ test: / \. vue$ / ,
25+ loader: ' vue-loader' ,
26+ options: {
27+ // `loaders` 覆盖默认 loaders.
28+ // 以下配置会导致所有的 <script> 标签 "lang" 属性失效。
29+ // attribute to be loaded with coffee-loader
30+ loaders: {
31+ js: ' coffee-loader'
32+ },
33+
34+ // `preLoaders` 会在默认 loaders 之前加载.
35+ // 你可以用来预处理语言块 - 一个例子是用来处理构建时的 i18n
36+ preLoaders: {
37+ js: ' /path/to/custom/loader'
38+ },
39+
40+ // `postLoaders` 会在默认 loaders 之后加载.
41+ //
42+ // - 对于 `html`, 默认 loader 返回会编译为 JavaScript 渲染函数
43+ //
44+ // - 对于 `css`, 由`vue-style-loader` 返回的结果通常不太有用。使用 postcss 插件将会是更好的选择。
45+ postLoaders: {
46+ html: ' babel-loader'
47+ }
48+ }
49+ }
50+ ]
51+ }
52+ }
53+ ```
54+
55+ ### Webpack 1.x
56+
57+ ``` js
58+ // webpack.config.js
59+ module .exports = {
60+ // other options...
61+ module: {
62+ loaders: [
63+ {
64+ test: / \. vue$ / ,
65+ loader: ' vue'
66+ }
67+ ]
68+ },
69+ // vue-loader configurations
70+ vue: {
71+ loaders: {
72+ // same configuration rules as above
73+ }
74+ }
75+ }
76+ ```
77+
78+ 进阶配置更实际的用法是 [ 提取组件中的 CSS 到单个文件] ( ./extract-css.md ) 。
Original file line number Diff line number Diff line change 1+ # 处理资源路径
2+
3+ 默认情况下,` vue-loader ` 使用 [ css-loader] ( https://github.com/webpack/css-loader ) 和 Vue 模版编译器自动处理你的样式和模版文件。在处理过程中,所有的资源路径例如 ` <img src="..."> ` , ` background: url(...) ` 和 ` @import ` ** 会作为模块依赖** 。
4+
5+ 例如,` url(./image.png) ` 会被转换为 ` require('./image.png') ` ,
6+
7+ ``` html
8+ <img src =" ../image.png" >
9+ ```
10+
11+ 将会编译为:
12+
13+ ``` js
14+ createElement (' img' , { attrs: { src: require (' ../image.png' ) }})
15+ ```
16+
17+ 因为 ` .png ` 不是一个 JavaScript 文件,你需要配置 Webpack 使用 [ file-loader] ( https://github.com/webpack/file-loader ) 或者 [ url-loader] ( https://github.com/webpack/url-loader ) 去处理它们。脚手器工具 ` vue-cli ` 已经为你配置好了。
18+
19+ 使用它们的好处:
20+
21+ 1 . ` file-loader ` 允许你指定资源文件的位置,允许使用 hashes 命名以获得长时间的缓存。此外,这意味着 ** 你可以就近管理你的图片文件,可以使用相对路径而不用担心布署** 。使用正确的配置,Webpack 将会在输出中自动重写为正常的文件路径。
22+
23+ 2 . ` url-loader ` 允许你设置转换为内联 base64 的文件最小值,这会减少小文件的 HTTP 请求。如果文件大于设置值,会自动的交给 ` file-loader ` 处理。
Original file line number Diff line number Diff line change 1+ # 自定义块
2+
3+ > 在大于 10.2.0 中支持
4+
5+ 在 ` .vue ` 文件中,你可以自定义语言块。自定义块将会被 ` vue-loader ` 选项 ` loaders ` 中指定的 loaders 处理,然后被组件模块依赖。配置类似 [ Loader 进阶配置] ( ../configurations/advanced.md ) ,除了匹配使用的是标签名称,而不是 ` lang ` 属性。
6+
7+ 如果 loader 匹配到自定义块,它会被处理;其它情况会被忽略。
8+
9+ ## 例子
10+
11+ 这个例子是提取自定义块 ` <docs> ` 的内容到单个 docs 文件中:
12+
13+ #### component.vue
14+
15+ ``` html
16+ <docs >
17+ ## This is an Example component.
18+ </docs >
19+
20+ <template >
21+ <h2 class =" red" >{{msg}}</h2 >
22+ </template >
23+
24+ <script >
25+ export default {
26+ data () {
27+ return {
28+ msg: ' Hello from Component A!'
29+ }
30+ }
31+ }
32+ </script >
33+
34+ <style >
35+ comp-a h2 {
36+ color : #f00 ;
37+ }
38+ </style >
39+ ```
40+
41+ #### webpack.config.js
42+
43+ ``` js
44+ // Webpack 2.x
45+ var ExtractTextPlugin = require (" extract-text-webpack-plugin" )
46+
47+ module .exports = {
48+ module: {
49+ rules: [
50+ {
51+ test: / \. vue$ / ,
52+ loader: ' vue' ,
53+ options: {
54+ loaders: {
55+ // 提取 <docs> 中的内容为原始文本
56+ ' docs' : ExtractTextPlugin .extract (' raw-loader' ),
57+ }
58+ }
59+ }
60+ ]
61+ },
62+ plugins: [
63+ // 输出 docs 到当个文件中
64+ new ExtractTextPlugin (' docs.md' )
65+ ]
66+ }
67+ ```
Original file line number Diff line number Diff line change 1+ # 提取 CSS 到单个文件
2+
3+ 提取所有 Vue 组件中的 CSS 到单个文件的例子:
4+
5+ ### Webpack 2.x
6+
7+ ``` bash
8+ npm install extract-text-webpack-plugin@2.x --save-dev
9+ ```
10+
11+ ``` js
12+ // webpack.config.js
13+ var ExtractTextPlugin = require (" extract-text-webpack-plugin" )
14+
15+ module .exports = {
16+ // other options...
17+ module: {
18+ rules: [
19+ {
20+ test: / \. vue$ / ,
21+ loader: ' vue-loader' ,
22+ options: {
23+ loaders: {
24+ css: ExtractTextPlugin .extract ({
25+ use: ' css-loader' ,
26+ fallback: ' vue-style-loader' // <- this is a dep of vue-loader, so no need to explicitly install if using npm3
27+ })
28+ }
29+ }
30+ }
31+ ]
32+ },
33+ plugins: [
34+ new ExtractTextPlugin (" style.css" )
35+ ]
36+ }
37+ ```
38+
39+ ### Webpack 1.x
40+
41+ ``` bash
42+ npm install extract-text-webpack-plugin --save-dev
43+ ```
44+
45+ ``` js
46+ // webpack.config.js
47+ var ExtractTextPlugin = require (" extract-text-webpack-plugin" )
48+
49+ module .exports = {
50+ // other options...
51+ module: {
52+ loaders: [
53+ {
54+ test: / \. vue$ / ,
55+ loader: ' vue'
56+ },
57+ ]
58+ },
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+ },
66+ plugins: [
67+ new ExtractTextPlugin (" style.css" )
68+ ]
69+ }
70+ ```
Original file line number Diff line number Diff line change 1+ # 使用预处理器
2+
3+ 在 Webpack 中,所有的预处理器需要匹配对应的 loader。 ` vue-loader ` 允许你使用其它 Webpack loaders 处理 Vue 组件的某一部分。它会根据 ` lang ` 属性自动推断出要使用的 loaders。
4+
5+ ### CSS
6+
7+ 例如,使用 SASS 编译我们的 ` <style> ` 语言块:
8+
9+ ``` bash
10+ npm install sass-loader node-sass --save-dev
11+ ```
12+
13+ ``` html
14+ <style lang =" sass" >
15+ /* write sass here */
16+ </style >
17+ ```
18+
19+ 在内部,` <style> ` 标签中的内容将会先由 ` sass-loader ` 进行处理,然后再传递进行下一步处理。
20+
21+ #### sass-loader 警告
22+
23+ 与名称相反,[ * sass* -loader] ( https://github.com/jtangelder/sass-loader ) 默认解析 * SCSS* 语法。如果你想要使用 * SASS* 语法,你需要配置 ` vue-loader ` 的选项:
24+
25+ ``` javascript
26+ {
27+ test: / \. vue$ / ,
28+ loader: ' vue-loader' ,
29+ options: {
30+ loaders: {
31+ scss: ' vue-style-loader!css-loader!sass-loader' , // <style lang="scss">
32+ sass: ' vue-style-loader!css-loader!sass-loader?indentedSyntax' // <style lang="sass">
33+ }
34+ }
35+ }
36+ ```
37+
38+ 如要获得更多关于 ` vue-loader ` 的配置信息,请查看 [ Loader 进阶配置] ( ./advanced.md ) 章节。
39+
40+ ### JavaScript
41+
42+ Vue 组件中的所有 JavaScript 默认使用 ` babel-loader ` 处理。你也可以改变处理方式:
43+
44+ ``` bash
45+ npm install coffee-loader --save-dev
46+ ```
47+
48+ ``` html
49+ <script lang =" coffee" >
50+ # Write coffeescript!
51+ </script >
52+ ```
53+
54+ ### 模版
55+
56+ 模版的处理方式略有不同,因为大多数 Webpack 模版处理器(比如 ` pug-loader ` )会返回模版处理函数,而不是编译的 HTML 字符串,我们使用原始的 ` pug ` 替代 ` pug-loader ` :
57+
58+ ``` bash
59+ npm install pug --save-dev
60+ ```
61+
62+ ``` html
63+ <template lang =" pug" >
64+ div
65+ h1 Hello world!
66+ </template >
67+ ```
68+
69+ > ** 重要:** 如果你使用 ` vue-loader@<8.2.0 ` , 你还需要安装 ` template-html-loader ` 。
70+
71+ ### 行内 Loader Requests
72+
73+ 你可以在 ` lang ` 属性中使用 [ Webpack loader requests] ( https://webpack.github.io/docs/loaders.html#introduction ) :
74+
75+ ``` html
76+ <style lang =" sass?outputStyle=expanded" >
77+ /* use sass here with expanded output */
78+ </style >
79+ ```
80+
81+ 但是,这使你的 Vue 组件只适用于 Webpack,不能与 Browserify and [ vueify] ( https://github.com/vuejs/vueify ) 一同使用。** 如果你打算将你的 Vue 组件作为可重复使用的第三方组件,请避免使用这个语法。**
You can’t perform that action at this time.
0 commit comments