Skip to content

Commit b385a1a

Browse files
committed
docs wip
1 parent b80f4e8 commit b385a1a

22 files changed

+818
-26
lines changed

Diff for: .gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
/node_modules
2-
/npm-debug.log
1+
node_modules
2+
npm-debug.log
33
test/output
4+
docs/_book

Diff for: README.md

+6-23
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,7 @@
44
55
It allows you to write your components in this format:
66

7-
``` html
8-
<!-- app.vue -->
9-
<template>
10-
<h1 class="red">{{msg}}</h1>
11-
</template>
12-
13-
<script>
14-
export default {
15-
data () {
16-
return {
17-
msg: 'Hello world!'
18-
}
19-
}
20-
}
21-
</script>
22-
23-
<style>
24-
.red {
25-
color: #f00;
26-
}
27-
</style>
28-
```
7+
![screenshot](http://blog.evanyou.me/images/vue-component.png)
298

309
## Table of Contents
3110

@@ -39,6 +18,9 @@ export default {
3918
- [Asset URL Handling](#asset-url-handling)
4019
- [Scoped CSS](#scoped-css)
4120
- [Hot Reload](#hot-reload)
21+
- [Syntax Highlighting](#syntax-highlighting)
22+
- [Linting](#linting)
23+
- [Testing](#testing)
4224
- [Advanced Loader Configuration](#advanced-loader-configuration)
4325
- [Example Project](https://github.com/vuejs/vue-loader-example)
4426

@@ -172,11 +154,12 @@ For template pre-processors, you should install `template-html-loader` plus the
172154

173155
## Src Imports
174156

175-
If you want, you can separate your template and styles into separate files and import them using the `src` attribute:
157+
If you want, you can separate your templates, styles or scripts into separate files and import them using the `src` attribute:
176158

177159
``` html
178160
<template src="./template.html"></template>
179161
<style src="./style.css"></style>
162+
<script src="./script.js"></script>
180163
```
181164

182165
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 installed NPM packages, e.g. `<style src="todomvc-app-css/index.css">`.

Diff for: docs/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Introduction
2+
3+
### What is Webpack?
4+
5+
Before we talk about `vue-loader`, let's first introduce [Webpack](http://webpack.github.io/). If you are already familiar with Webpack, feel free to skip the following explanation. But for those of you who are new to Webpack, here's a quick intro:
6+
7+
Webpack is a module bundler. It takes a bunch of files, treating each as a module, figuring out the dependencies between them, and bundle them into static assets that are ready for deployment.
8+
9+
![webpack](http://webpack.github.io/assets/what-is-webpack.png)
10+
11+
For a basic example, imagine we have a bunch of CommonJS modules. They cannot run directly inside the browser, so we need to "bundle" them into a single file that can be included via a `<script>` tag. Webpack can follow the dependencies of the `require()` calls and do that for us.
12+
13+
But Webpack can do more than that. With "loaders", we can teach Webpack to transform all types of files in anyway we want before outputting the final bundle. Some examples include:
14+
15+
- Transpile ES2015, CoffeeScript or TypeScript modules into plain ES5 CommonJS modules;
16+
- Optionally you can pipe the source code through a linter before doing the compilation;
17+
- Transpile Jade templates into plain HTML and inline it as a JavaScript string;
18+
- Transpile SASS files into plain CSS, then convert it into a JavaScript snippet that insert the resulting CSS as a `<style>` tag;
19+
- Process an image file referenced in HTML or CSS, moved it to the desired destination based on the path configurations, and naming it using its md5 hash.
20+
21+
Webpack is so powerful that when you understand how it works, it can dramatically improve your front-end workflow. Its primary drawback is verbose and complex configuration; but with this guide you should be able to find solutions for most common issues when using Webpack with Vue.js and `vue-loader`.
22+
23+
### What is `vue-loader`?
24+
25+
`vue-loader` is a loader for Webpack that can transform Vue components written in the following format into a plain JavaScript module:
26+
27+
![screenshot](http://blog.evanyou.me/images/vue-component.png)
28+
29+
There are many cool features provided by `vue-loader`:
30+
31+
- ES2015 enabled by default;
32+
- Allows using other Webpack loaders for each part of a Vue component, for example SASS for `<style>` and Jade for `<template>`;
33+
- Treat static assets referenced in `<style>` and `<template>` as module dependencies and handle them with Webpack loaders;
34+
- Can simulate scoped CSS for each component;
35+
- Supports component hot-reloading during development.
36+
37+
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.

Diff for: docs/SUMMARY.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
- Getting Started
2+
- [Vue Component Spec](start/spec.md)
3+
- [Basic Tutorial](start/tutorial.md)
4+
- Configurations
5+
- [Inline Loaders](configurations/inline-loaders.md)
6+
- [Asset URL Handling](configurations/asset-url.md)
7+
- [Babel and ES2015](configurations/es2015.md)
8+
- [PostCSS and Autoprefixer](configurations/postcss.md)
9+
- [Advanced Loader Configuration](configurations/advanced.md)
10+
- [Extracting CSS File](configurations/extract-css.md)
11+
- Features
12+
- [Scoped CSS](features/scoped-css.md)
13+
- [Hot Reload](features/hot-reload.md)
14+
- Workflow
15+
- [Linting](workflow/linting.md)
16+
- [Testing](workflow/testing.md)

Diff for: docs/assets/CNAME

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vue-loader.vuejs.org

Diff for: docs/assets/circle.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
general:
2+
branches:
3+
ignore:
4+
- gh-pages

Diff for: docs/book.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"gitbook": "2.x.x",
3+
"plugins": ["edit-link", "github"],
4+
"pluginsConfig": {
5+
"edit-link": {
6+
"base": "https://github.com/vuejs/vue-loader/tree/master/docs",
7+
"label": "Edit This Page"
8+
},
9+
"github": {
10+
"url": "https://github.com/vuejs/vue-loader/"
11+
}
12+
},
13+
"links": {
14+
"sharing": {
15+
"facebook": false,
16+
"twitter": false
17+
}
18+
}
19+
}

Diff for: docs/configurations/advanced.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Advanced Loader Configuration
2+
3+
Sometimes you may want to apply a custom loader string to a language instead of letting `vue-loader` infer it. Or you may simply want to overwrite the built-in loader configuration for the default languages. To do that, add a `vue` block in your Webpack config file, and specify the `loaders` option.
4+
5+
Example:
6+
7+
``` js
8+
// webpack.config.js
9+
module.exports = {
10+
// other options...
11+
module: {
12+
loaders: [
13+
{
14+
test: /\.vue$/,
15+
loader: 'vue'
16+
}
17+
]
18+
},
19+
// vue-loader configurations
20+
vue: {
21+
// ... other vue options
22+
loaders: {
23+
// load all <script> without "lang" attribute with coffee-loader
24+
js: 'coffee',
25+
// load <template> directly as HTML string, without piping it
26+
// through vue-html-loader first
27+
html: 'raw'
28+
}
29+
}
30+
}
31+
```
32+
33+
A more practical usage of the advanced loader configuration is [extracting CSS inside components into a single file](./extract-css.md).

Diff for: docs/configurations/asset-url.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Asset URL Handling
2+
3+
By default, `vue-loader` automatically processes your style and template files with [css-loader](https://github.com/webpack/css-loader) and [vue-html-loader](https://github.com/vuejs/vue-html-loader) (which is just a Vue-specific fork of [html-loader](https://github.com/webpack/html-loader)). What this means is that all asset URLs such as `<img src="...">`, `background: url(...)` and CSS `@import` are **resolved as module dependencies**.
4+
5+
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 us some very powerful benefits by managing static assets this way:
6+
7+
1. `file-loader` allows you to designate where to copy and place the asset file, and how to name it using version hashes. The best part though, is that you can use relative URLs based on the folder structure of your source files, and Webpack will auto-rewrite them into different URLs in the bundled files based on the configuration.
8+
9+
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 fallbacks to `file-loader`.
10+
11+
Here's an example Webpack config that handles `.png`, `jpg` and `.gif` files, and inlining any file smaller than 10kb as base64 data URL:
12+
13+
``` bash
14+
npm install url-loader file-loader --save-dev
15+
```
16+
17+
``` js
18+
// webpack.config.js
19+
module.exports = {
20+
// ... other options
21+
module: {
22+
loaders: [
23+
// ... other loaders
24+
{
25+
test: /\.(png|jpg|gif)$/,
26+
loader: 'url',
27+
query: {
28+
// limit for base64 inlining in bytes
29+
limit: 10000,
30+
// custom naming format if file is larger than
31+
// the threshold
32+
name: '[name].[ext]?[hash]'
33+
}
34+
}
35+
]
36+
}
37+
}
38+
```

Diff for: docs/configurations/es2015.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Configuring JavaScript in Vue Components
2+
3+
### ES2015 by Default
4+
5+
`vue-loader` ships with ES2015 enabled by default in `<script>` tags. All scripts inside Vue components are compiled with [Babel](https://babeljs.io/) using `babel-loader`.
6+
7+
> **Compatibility Note**: `vue-loader` 7.0+ uses Babel 6. If you need to use Babel 5 for transpiling your code, use vue-loader 6.x.
8+
9+
### Using Custom Babel Configuration
10+
11+
The default Babel options used for scripts inside Vue components are:
12+
13+
``` json
14+
{
15+
"presets": ["es2015"],
16+
"plugins": ["transform-runtime"]
17+
}
18+
```
19+
20+
If you want to use custom presets or plugins, for example enable stage-0 language features, install the preset/plugin and then add a `babel` field in your Webpack config:
21+
22+
``` bash
23+
npm install babel-preset-stage-0 --save-dev
24+
```
25+
26+
``` js
27+
// webpack.config.js
28+
module.exports = {
29+
// other configs...
30+
babel: {
31+
// enable stage 0 babel transforms.
32+
presets: ['es2015', 'stage-0'],
33+
plugins: ['transform-runtime']
34+
}
35+
}
36+
```
37+
38+
### Compiling `.js` Files with Babel
39+
40+
Since we are already using Babel, most likely you'll want to compile your normal `.js` files too! Here's how to do it in your Webpack config:
41+
42+
``` js
43+
// webpack.config.js
44+
module.exports = {
45+
// other options ...
46+
module: {
47+
loaders: [
48+
{
49+
// use vue-loader for *.vue files
50+
test: /\.vue$/,
51+
loader: 'vue'
52+
},
53+
{
54+
// use babel-loader for *.js files
55+
test: /\.js$/,
56+
loader: 'babel',
57+
// important: exclude files in node_modules
58+
// otherwise it's going to be really slow!
59+
exclude: /node_modules/
60+
}
61+
]
62+
},
63+
// if you are using babel-loader directly then
64+
// the babel config block becomes required.
65+
babel: {
66+
presets: ['es2015'],
67+
plugins: ['transform-runtime']
68+
}
69+
}
70+
```

Diff for: docs/configurations/extract-css.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Extracting CSS into a Single File
2+
3+
Example config to extract all the processed CSS in all Vue components into a single CSS file:
4+
5+
``` bash
6+
npm install extract-text-webpack-plugin --save-dev
7+
```
8+
9+
``` js
10+
// webpack.config.js
11+
var ExtractTextPlugin = require("extract-text-webpack-plugin");
12+
13+
module.exports = {
14+
// other options...
15+
module: {
16+
loaders: [
17+
{
18+
test: /\.vue$/,
19+
loader: 'vue'
20+
},
21+
]
22+
},
23+
vue: {
24+
loaders: {
25+
css: ExtractTextPlugin.extract("css"),
26+
// you can also include <style lang="less"> or other langauges
27+
less: ExtractTextPlugin.extract("css!less")
28+
}
29+
},
30+
plugins: [
31+
new ExtractTextPlugin("style.css")
32+
]
33+
}
34+
```

Diff for: docs/configurations/inline-loaders.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Inline Loaders
2+
3+
`vue-loader` allows you to use other Webpack loaders to process a part of a Vue component. It will automatically infer the proper loaders to use from the `lang` attribute of a language block.
4+
5+
### CSS
6+
7+
For example, let's compile our `<style>` tag with SASS:
8+
9+
``` bash
10+
npm install sass-loader --save-dev
11+
```
12+
13+
``` html
14+
<style lang="sass">
15+
/* write sass here */
16+
</style>
17+
```
18+
19+
Under the hood, the text content inside the `<style>` tag will be first compiled by `sass-loader` before being passed on for further processing.
20+
21+
### JavaScript
22+
23+
All JavaScript inside Vue components are processed by `babel-loader` by default. But you can of course change it:
24+
25+
``` bash
26+
npm install coffee-loader --save-dev
27+
```
28+
29+
``` html
30+
<script lang="coffee">
31+
# Write coffeesciprt!
32+
</script>
33+
```
34+
35+
We will talk more about Babel/ES2015 configurations in another section.
36+
37+
### Templates
38+
39+
Processing templates is a little different, because most Webpack template loaders such as `jade-loader` returns a template function instead of compiled HTML string. So instead of using `jade-loader`, we will use `template-html-loader` plus the raw `jade` compiler:
40+
41+
``` bash
42+
npm install template-html-loader jade --save-dev
43+
```
44+
45+
``` html
46+
<template lang="jade">
47+
div
48+
h1 Hello world!
49+
</template>
50+
```
51+
52+
### Inline Loader Requests
53+
54+
You can use [Webpack loader requests](https://webpack.github.io/docs/loaders.html#introduction) in the `lang` attribute:
55+
56+
``` html
57+
<style lang="sass?outputStyle=expanded">
58+
/* use sass here with expanded output */
59+
</style>
60+
```
61+
62+
However, note this makes your Vue component Webpack-specific and not compatible with Browserify and [vueify](https://github.com/vuejs/vueify). **If you intend to ship your Vue component as a reusable 3rd-party component, avoid using this syntax.**

0 commit comments

Comments
 (0)