You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+83-42Lines changed: 83 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,48 +27,20 @@ It allows you to write your components in this format:
27
27
</script>
28
28
```
29
29
30
-
You can also mix preprocessor languages in the component file:
31
-
32
-
```html
33
-
// app.vue
34
-
<stylelang="stylus">
35
-
.red
36
-
color #f00
37
-
</style>
38
-
39
-
<template lang="jade">
40
-
h1(class="red") {{msg}}
41
-
</template>
42
-
43
-
<script lang="coffee">
44
-
module.exports =
45
-
data: ->
46
-
msg: 'Hello world!'
47
-
</script>
48
-
```
49
-
50
-
And you can import using the `src` attribute:
51
-
52
-
``` html
53
-
<style lang="stylus" src="./style.styl"></style>
54
-
```
55
-
56
-
**NOTE**: Starting from version 2.1.0, `src` imports follow similar rules to `require()` calls, which means for relative paths you need to start with `./`, and you can import resources from node modules: `<style src="todomvc-app-css/index.css">`.
57
-
58
-
## Usage
30
+
## Basic Usage
59
31
60
32
Config Webpack:
61
33
62
34
```js
63
35
// webpack.config.js
64
36
module.exports= {
65
-
entry: "./main.js",
37
+
entry:'./main.js',
66
38
output: {
67
-
filename: "build.js"
39
+
filename:'build.js'
68
40
},
69
41
module: {
70
42
loaders: [
71
-
{ test: /\.vue$/, loader: "vue-loader" },
43
+
{ test:/\.vue$/, loader:'vue' },
72
44
]
73
45
}
74
46
}
@@ -85,28 +57,97 @@ var app = new Vue(appOptions).$mount('#app')
85
57
86
58
## Pre-Processors
87
59
88
-
By default `vue-loader` needs `html-loader`, `css-loader` and `style-loader` as peer dependencies. In order to usepre-processors, you need to install the corresponding Webpack loader for that language.
60
+
`vue-loader` allows you to use per-file pre-processors inside `*.vue` components with the `lang` attribute:
61
+
62
+
```html
63
+
<stylelang="stylus">
64
+
/* use stylus here */
65
+
</style>
66
+
```
67
+
68
+
The `lang` attribute will be used to automatically locate the loader to use, and you can pass Webpack loader queries in it as well:
69
+
70
+
```html
71
+
<stylelang="sass?outputStyle=expanded">
72
+
/* use sass here with expanded output */
73
+
</style>
74
+
```
75
+
76
+
#### A Note on Dependencies
77
+
78
+
By default, `vue-loader` requires `html-loader`, `css-loader` and `style-loader` as peer dependencies. In order to use pre-processors, you also need to install the corresponding Webpack loader for that language.
79
+
80
+
Also, for template pre-processors, you should install `template-html-loader` plus the raw templating engine. For example to use `jade`, you will need to install both `template-html-loader` and `jade` instead of `jade-loader`.
81
+
82
+
## Style Imports
83
+
84
+
If you want, you can separate your styles into a separate file and import it using the `src` attribute:
89
85
90
-
**Note** For templatepre-processors, use `template-html-loader` plus the raw templating engine. For example to use `jade`, you will need to install both `template-html-loader` and `jade` instead of `jade-loader`.
86
+
```html
87
+
<stylesrc="./style.css"></style>
88
+
```
89
+
90
+
Beware that `src` imports follow similar rules to `require()` calls, which means for relative paths you need to start with `./`, and you can import resources from node modules: `<style src="todomvc-app-css/index.css">`.
91
+
92
+
## Asset URL Handling
93
+
94
+
By default, `vue-loader` automatically processes your style and template files with `css-loader` and `html-loader` - this means that all asset URLs such as `<img src="...">`, `background: url(...)` and CSS `@import` are **resolved as module dependencies**.
95
+
96
+
For example, `url(image.png)` will be translated into `require('./image.png')`. Because `.png` is not JavaScript, 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. This may feel cumbersome, but it gives you some very powerful benefits in managing your static assets this way:
97
+
98
+
1.`file-loader` allows you to customize where to copy and place the asset file (by specifying `publicPath` in your Webpack config), and how they should be named with version hashes.
99
+
100
+
2.`url-loader` allows you to conditionally load a file as a inline Data URL if they are smaller than a given threshold.
91
101
92
-
## Loader configuration
102
+
For more details, see the respective documentations for [html-loader](https://github.com/webpack/html-loader) and [css-loader](https://github.com/webpack/css-loader).
103
+
104
+
## Advanced Loader configuration
93
105
94
106
By default, `vue-loader` will try to use the loader with the same name as
95
107
the `lang` attribute, but you can configure which loader should be used.
96
108
97
-
For example, to extract out the generated css into a separate file,
98
-
use this configuration:
109
+
#### Example: Using ES6 with Babel
110
+
111
+
To apply Babel transforms to all your JavaScript, use this Webpack config:
112
+
113
+
```js
114
+
var vue =require('vue-loader')
115
+
116
+
module.exports= {
117
+
// entry, output...
118
+
module: {
119
+
loaders: [
120
+
{
121
+
test:/\.vue$/,
122
+
loader:vue.withLoaders({
123
+
// apply babel transform to all javascript
124
+
// inside *.vue files.
125
+
js:'babel?optional[]=runtime'
126
+
})
127
+
}
128
+
]
129
+
},
130
+
devtool:'source-map'
131
+
}
132
+
```
133
+
134
+
Some explanantions:
135
+
136
+
1. Here `js` is the default language for `<script>` blocks.
137
+
138
+
2. The `?optional[]=runtime` query string passed to the loader. This instructs Babel to use helper functions from the `babel-runtime` NPM package instead of injecting the helpers into every single file. You'll want this most of the time.
139
+
140
+
#### Example: Extracting CSS into a Single File
141
+
142
+
To extract out the generated css into a separate file,
143
+
use this Webpack config:
99
144
100
145
```js
101
-
// webpack.config.js
102
146
var ExtractTextPlugin =require("extract-text-webpack-plugin");
0 commit comments