Skip to content

Commit 5e23e55

Browse files
committed
update lint docs
1 parent 3ac4d6b commit 5e23e55

File tree

1 file changed

+48
-15
lines changed

1 file changed

+48
-15
lines changed

docs/workflow/linting.md

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,23 @@
22

33
You may have been wondering how do you lint your code inside `*.vue` files, since they are not JavaScript. We will assume you are using [ESLint](http://eslint.org/) (if you are not, you should!).
44

5-
When using ESLint directly, you can use it with the [eslint-html-plugin](https://github.com/BenoitZugmeyer/eslint-plugin-html) with also supports extracting and linting the JavaScript inside `*.vue` files.
5+
You will also need the [eslint-html-plugin](https://github.com/BenoitZugmeyer/eslint-plugin-html) with supports extracting and linting the JavaScript inside `*.vue` files.
66

7-
Another option is using the [eslint-loader](https://github.com/MoOx/eslint-loader), and configure it to lint the JavaScript code in `*.vue` files directly:
7+
Make sure to include the plugin in your ESLint config:
8+
9+
``` json
10+
"plugins": [
11+
"html"
12+
]
13+
```
14+
15+
Then from the command line:
16+
17+
``` bash
18+
eslint --ext js,vue MyComponent.vue
19+
```
20+
21+
Another option is using [eslint-loader](https://github.com/MoOx/eslint-loader) so that your `*.vue` files are automatically linted on save during development:
822

923
``` bash
1024
npm install eslint eslint-loader --save-dev
@@ -14,22 +28,41 @@ npm install eslint eslint-loader --save-dev
1428
// webpack.config.js
1529
module.exports = {
1630
// ... other options
17-
vue: {
18-
loaders: {
19-
js: 'babel!eslint'
20-
}
21-
},
22-
babel: {
23-
presets: ['es2015'],
24-
plugins: ['transform-runtime']
31+
module: {
32+
loaders: [
33+
{
34+
test: /.vue$/,
35+
loader: 'vue!eslint'
36+
}
37+
]
2538
}
2639
}
2740
```
2841

29-
Two things to note here:
30-
31-
1. We are overwriting the default loader string for all JavaScript in `*.vue` files - if we still want ES2015, we need to explicitly apply `babel-loader` and related configurations.
42+
Note that Webpack loader chains are applied **right-first**. Make sure to apply `eslint` before `vue` so we are linting the pre-compile source code.
3243

33-
2. Webpack loader chains are applied right-first. Make sure to apply `eslint` before `babel` so we are linting the pre-compile source code.
44+
One thing we need to consider is using third party `*.vue` components shipped in NPM packages. In such case, we want to use `vue-loader` to process the third party component, but do not want to lint it. We can separate the linting into Webpack's [preLoaders](https://webpack.github.io/docs/loaders.html#loader-order):
3445

35-
And don't forget to create your `.eslintrc` config. Now when you run `webpack-dev-server`, you'll get instant lint warnings as you save the file!
46+
``` js
47+
// webpack.config.js
48+
module.exports = {
49+
// ... other options
50+
module: {
51+
// only lint local *.vue files
52+
preLoaders: [
53+
{
54+
test: /.vue$/,
55+
loader: 'eslint',
56+
exclude: /node_modules/
57+
}
58+
],
59+
// but use vue-loader for all *.vue files
60+
loaders: [
61+
{
62+
test: /.vue$/,
63+
loader: 'vue'
64+
}
65+
]
66+
}
67+
}
68+
```

0 commit comments

Comments
 (0)