Skip to content

Commit a045070

Browse files
committed
improve readme
1 parent 490383a commit a045070

File tree

1 file changed

+83
-42
lines changed

1 file changed

+83
-42
lines changed

README.md

Lines changed: 83 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,48 +27,20 @@ It allows you to write your components in this format:
2727
</script>
2828
```
2929

30-
You can also mix preprocessor languages in the component file:
31-
32-
``` html
33-
// app.vue
34-
<style lang="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
5931

6032
Config Webpack:
6133

6234
``` js
6335
// webpack.config.js
6436
module.exports = {
65-
entry: "./main.js",
37+
entry: './main.js',
6638
output: {
67-
filename: "build.js"
39+
filename: 'build.js'
6840
},
6941
module: {
7042
loaders: [
71-
{ test: /\.vue$/, loader: "vue-loader" },
43+
{ test: /\.vue$/, loader: 'vue' },
7244
]
7345
}
7446
}
@@ -85,28 +57,97 @@ var app = new Vue(appOptions).$mount('#app')
8557

8658
## Pre-Processors
8759

88-
By default `vue-loader` needs `html-loader`, `css-loader` and `style-loader` as peer dependencies. In order to use pre-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+
<style lang="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+
<style lang="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:
8985

90-
**Note** For template pre-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+
<style src="./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.
91101

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
93105

94106
By default, `vue-loader` will try to use the loader with the same name as
95107
the `lang` attribute, but you can configure which loader should be used.
96108

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:
99144

100145
``` js
101-
// webpack.config.js
102146
var ExtractTextPlugin = require("extract-text-webpack-plugin");
103147
var vue = require("vue-loader");
104148

105149
module.exports = {
106-
entry: "./main.js",
107-
output: {
108-
filename: "build.js"
109-
},
150+
// entry, output...
110151
module: {
111152
loaders: [
112153
{

0 commit comments

Comments
 (0)