这一篇,咱们将接着上篇来完成配置eslint、babel、postcss。css
咱们采用eslint --init
的方式来建立eslintrc.js。 对了,前提咱们须要全局安装eslint:npm i -g eslint
。 安装彻底局eslint之后,咱们在项目根目录使用eslint --init
,我选择自定义的方式来规定eslint规则:html
➜ vue-construct git:(master) ✗ eslint --init
? How would you like to configure ESLint? Answer questions about your style
? Are you using ECMAScript 6 features? Yes
? Are you using ES6 modules? Yes
? Where will your code run? Browser, Node
? Do you use CommonJS? Yes
? Do you use JSX? No
? What style of indentation do you use? Spaces
? What quotes do you use for strings? Single
? What line endings do you use? Unix
? Do you require semicolons? No
? What format do you want your config file to be in? (Use arrow keys)
❯ JavaScript
复制代码
固然,你能够按照本身喜欢,选择本身想要的方式,好比How would you like to configure ESLint? 这个问题的时候,能够选择popular的规则,有Google、standard等规则,选择你想要的就好。前端
我po下个人配置吧:vue
// 建立这个文件的话,本王推荐用eslint --init建立
module.exports = {
"env": {
"browser": true,
"node": true
},
// https://stackoverflow.com/questions/38296761/how-to-support-es7-in-eslint
// 为了让eslint支持es7或更高的语法
"parser": 'babel-eslint',
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"plugins": [
// https://github.com/BenoitZugmeyer/eslint-plugin-html
// 支持 *.vue lint
"html"
],
// https://eslint.org/docs/rules/
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
],
// https://eslint.org/docs/user-guide/configuring#using-configuration-files
// "off" or 0 - turn the rule off
// "warn" or 1 - turn the rule on as a warning (doesn’t affect exit code)
// "error" or 2 - turn the rule on as an error (exit code is 1 when triggered)
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
'no-console': 0,
}
};
复制代码
建立.babelrc
文件,直接上配置:node
{
"presets": [
[
"env",
{
"targets": {
"browsers": [
"> 1%",
"last 2 versions",
"ie >= 10"
]
},
"modules": false,
"useBuiltIns": true
}
]
],
"plugins": [
"transform-object-rest-spread",
"syntax-dynamic-import"
]
}
复制代码
配合webpack配置:webpack
{
test: /\.js$/,
include: [resolve('app')],
use: [
'babel-loader',
'eslint-loader'
]
},
复制代码
咱们使用的是babel-preset-env,咱们知道,babel只是转译了高级语法,好比lambda,class,async等,并不会支持高级的api,因此须要babel-polyfill的帮忙。方便的是,咱们只须要"useBuiltIns": true
,而后npm安装babel-polyfill,再在webpack配置中的entry带上babel-polyfill就行了。git
babel-preset-env的优势:github
targets
来决定支持到那个哪些版本的语法就够了,不会过渡转译,可控性强useBuiltIns
来支持babel-polyfill的按需加载,而不是一口气把整个包打入,由于其实咱们只用到了很小一部分transform-object-rest-spread是为了支持const a = {name: kitty, age: 7}; const b = { ...a }
这种es7语法。web
syntax-dynamic-import是为了支持const Home = () => import('../views/home')
这种语法,达到按需分割、加载的目的。shell
建立postcss.config.js
文件,上配置:
module.exports = {
plugins: [
require('autoprefixer')
],
// 配置autoprefix
browsers: [
"> 1%",
"last 2 versions",
"ie >= 10"
]
}
复制代码
这篇很少,就作了三件事,eslint、babel、postcss。
下一篇咱们将建立项目文件、目录架构 - 从零开始作Vue前端架构(3)