cnpm i [email protected] [email protected] [email protected] --save -D
module.exports = {
root: true,
parser: 'vue-eslint-parser',
parserOptions: {
parser: 'babel-eslint',
ecmaVersion: 12,
sourceType: 'module',
},
env: {
node: true,
browser: true,
},
extends: [
'eslint:recommended',
'plugin:vue/essential',
],
// add your custom rules here
rules: {
// allow async-await
'generator-star-spacing': 'off',
// allow debugger during development
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'vue/no-parsing-error': [2, { 'x-invalid-end-tag': false }],
}
}
// 添加插件
const ESLintPlugin = require('eslint-webpack-plugin');
// 配置插件
plugins: [
new vueLoaderPlugin(),
new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery',
moment: 'moment'
})
// 新增插件配置
].concat(config.dev.useEslint ? (new ESLintPlugin()) : []),
// 移除module.rules中的eslint配置
rules:[
// 移除以下代码
...(config.dev.useEslint ? [createLintingRule()] : []),
]
cnpm install prettier@2.8.8 eslint-config-prettier@8.8.0 eslint-plugin-prettier@4.2.1 --save -D
module.exports = {
printWidth: 180,
tabWidth: 2,
useTabs: false,
semi: false,
singleQuote: true,
quoteProps: 'as-needed',
jsxSingleQuote: true,
bracketSameLine: false,
trailingComma: 'es5',
bracketSpacing: true,
jsxBracketSameLine: true,
arrowParens: 'avoid',
htmlWhitespaceSensitivity: 'ignore',
vueIndentScriptAndStyle: false,
embeddedLanguageFormatting: 'auto',
};
// 新增extends
extends: [
'plugin:prettier/recommended'
]
cnpm install husky@7.0.4 lint-staged@11.2.6 --save -D
npx husky install
执行完成后,会新增一个.husky文件夹
npx husky add .husky/pre-commit "npx lint-staged"
"lint-staged": {
"*.{js,vue}": "eslint --fix"
}
配置完成后,当我们commit的时候就会自动校验我们的代码
editorconfig的作用是用来规范我们的编辑习惯的
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
备注:以上的所有配置,各项目根据自己的实际情况做微调,有则改之无则加勉,eslint规则切忌随意off
遇到eslint问题,我们可以用上面的操作格式化当前文件,或者save的时候校验
env: {
browser:
true
,
jquery:
true
},
2、The template root requires exactly one element
'vue/no-multiple-template-root': 'off',