獲取全套webpack 4.x教程,請訪問瓦力博客
ESLint 是一個ECMAScript/JavaScript 語法規(guī)則和代碼風格的檢查工具,它的目標是保證代碼的一致性和避免錯誤。個人認為ESlint 在團隊開發(fā)中很重要翘狱,它能有效的規(guī)范團隊代碼風格,為代碼后期維護提供便利。
1.了解eslint規(guī)則
小菜在這里列舉一下規(guī)則蔽挠,關(guān)于更多規(guī)則請閱讀官方網(wǎng)站eslint 規(guī)則{:target="_blank"}
"no-bitwise": 0,//禁止使用按位運算符
"no-catch-shadow": 2,//禁止catch子句參數(shù)與外部作用域變量同名
"no-class-assign": 2,//禁止給類賦值
"no-cond-assign": 2,//禁止在條件表達式中使用賦值語句
"no-console": 2,//禁止使用console
"no-const-assign": 2,//禁止修改const聲明的變量
"no-constant-condition": 2,//禁止在條件中使用常量表達式 if(true) if(1)
"no-continue": 0,//禁止使用continue
"no-control-regex": 2,//禁止在正則表達式中使用控制字符
"no-debugger": 2,//禁止使用debugger
"no-delete-var": 2,//不能對var聲明的變量使用delete操作符
"no-div-regex": 1,//不能使用看起來像除法的正則表達式/=foo/
"no-dupe-keys": 2,//在創(chuàng)建對象字面量時不允許鍵重復 {a:1,a:1}
"no-dupe-args": 2,//函數(shù)參數(shù)不能重復
"no-duplicate-case": 2,//switch中的case標簽不能重復
"no-else-return": 2,//如果if語句里面有return,后面不能跟else語句
"no-empty": 2,//塊語句中的內(nèi)容不能為空
"no-empty-character-class": 2,//正則表達式中的[]內(nèi)容不能為空
"no-empty-label": 2,//禁止使用空label
"no-eq-null": 2,//禁止對null使用==或!=運算符
"no-eval": 1,//禁止使用eval
"no-ex-assign": 2,//禁止給catch語句中的異常參數(shù)賦值
"no-extend-native": 2,//禁止擴展native對象
"no-extra-bind": 2,//禁止不必要的函數(shù)綁定
"no-extra-boolean-cast": 2,//禁止不必要的bool轉(zhuǎn)換
"no-extra-parens": 2,//禁止非必要的括號
"no-extra-semi": 2,//禁止多余的冒號
"no-fallthrough": 1,//禁止switch穿透
"no-func-assign": 2,//禁止重復的函數(shù)聲明
"no-implicit-coercion": 1,//禁止隱式轉(zhuǎn)換
"no-implied-eval": 2,//禁止使用隱式eval
"no-inline-comments": 0,//禁止行內(nèi)備注
"no-invalid-regexp": 2,//禁止無效的正則表達式
"no-label-var": 2,//label名不能與var聲明的變量名相同
"no-labels": 2,//禁止標簽聲明
"no-lone-blocks": 2,//禁止不必要的嵌套塊
"no-lonely-if": 2,//禁止else語句內(nèi)只有if語句
"no-loop-func": 1,//禁止在循環(huán)中使用函數(shù)(如果沒有引用外部變量不形成閉包就可以)
"no-multi-spaces": 1,//不能用多余的空格
"no-multi-str": 2,//字符串不能用\換行
"no-multiple-empty-lines": [1, {"max": 2}],//空行最多不能超過2行
"no-native-reassign": 2,//不能重寫native對象
"no-negated-in-lhs": 2,//in 操作符的左邊不能有!
"no-nested-ternary": 0,//禁止使用嵌套的三目運算
"no-new": 1,//禁止在使用new構(gòu)造一個實例后不賦值
"no-new-func": 1,//禁止使用new Function
"no-new-object": 2,//禁止使用new Object()
"no-new-require": 2,//禁止使用new require
"no-new-wrappers": 2,//禁止使用new創(chuàng)建包裝實例,new String new Boolean new Number
"no-obj-calls": 2,//不能調(diào)用內(nèi)置的全局對象瓜浸,比如Math() JSON()
"no-octal": 2,//禁止使用八進制數(shù)字
"no-octal-escape": 2,//禁止使用八進制轉(zhuǎn)義序列
"no-param-reassign": 2,//禁止給參數(shù)重新賦值
"no-path-concat": 0,//node中不能使用__dirname或__filename做路徑拼接
"no-plusplus": 0,//禁止使用++澳淑,--
"no-process-env": 0,//禁止使用process.env
"no-process-exit": 0,//禁止使用process.exit()
"no-proto": 2,//禁止使用__proto__屬性
"no-redeclare": 2,//禁止重復聲明變量
"no-regex-spaces": 2,//禁止在正則表達式字面量中使用多個空格 /foo bar/
"no-restricted-modules": 0,//如果禁用了指定模塊,使用就會報錯
"no-return-assign": 1,//return 語句中不能有賦值表達式
"no-script-url": 0,//禁止使用javascript:void(0)
"no-self-compare": 2,//不能比較自身
"no-sequences": 0,//禁止使用逗號運算符
"no-shadow": 2,//外部作用域中的變量不能與它所包含的作用域中的變量或參數(shù)同名
"no-shadow-restricted-names": 2,//嚴格模式中規(guī)定的限制標識符不能作為聲明時的變量名使用
"no-spaced-func": 2,//函數(shù)調(diào)用時 函數(shù)名與()之間不能有空格
"no-sync": 0,//nodejs 禁止同步方法
"no-ternary": 0,//禁止使用三目運算符
"no-trailing-spaces": 1,//一行結(jié)束后面不要有空格
2.cli中運行eslint
安裝Eslint
yarn add eslint
初始化話eslint配置文件
這里小菜提醒一下插佛,就是如果用gitbash運行這個命令杠巡,不能選擇的話建議用cmd
執(zhí)行這條命令
npx eslint --init
這里的配置是自定義的,大家根據(jù)自己的喜好隨意選雇寇,小菜也是隨意配的氢拥。
eslint 語法檢測
npx eslint src
3.開發(fā)工具檢測
上面介紹的方法效率實在太低了,大家可以在自己的開發(fā)工具中安裝eslint
插件锨侯,如webstrom
,vsCode
等編輯中安裝插件嫩海。插件安裝完成后,vscode編輯器會查找本地.eslintrc.js
文件囚痴,根據(jù)該文件來進行檢查叁怪。.eslintrc.js
文件上一步在命令行中生成的。
大家快去按照eslint
規(guī)則去消除錯誤
4.webpack檢測
安裝eslint-loader
yarn add eslint-loader
build/module.js
const dirPath = require('./base/path');
const config = require('./base/config');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
let _module = {
rules:[
{
test:/\.css$/,
use:[
config.NODE_ENV == 'development'?'style-loader': MiniCssExtractPlugin.loader,
{
loader:'css-loader',
options:{
importLoaders:1
}
},
'postcss-loader'
]
},
{
test:/\.scss$/,
use:[
config.NODE_ENV == 'development'?'style-loader': MiniCssExtractPlugin.loader,
{
loader:'css-loader',
options:{
importLoaders:2
}
},
'sass-loader',
'postcss-loader'
]
},
{
test: /\.less$/,
use: [
config.NODE_ENV == 'development'?'style-loader': MiniCssExtractPlugin.loader,
{
loader:'css-loader',
options:{
importLoaders:2
}
},
'less-loader',
'postcss-loader'
]
},
{
test:/\.(png|svg|jpeg|jpg|gif)$/,
use:[
{
loader:'file-loader',
options:{
name:'[name][sha512:hash:base64:7].[ext]', //[path] 上下文環(huán)境路徑
outputPath: dirPath.images, //輸出路徑
publicPath: config.NODE_ENV === 'development'?dirPath.images:dirPath.images //公共路徑
}
},
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true, // webpack@1.x
disable: true, // webpack@2.x and newer
},
},
]
},
{
test: /\.html$/,
use:[
{
loader:'html-loader',
options:{
arrts:['img:src','img:data-src'],
minimize: config.NODE_ENV === 'development'? false:true //是否壓縮html
}
}
]
},
{
test: /(iconfont.svg)|\.(woff|woff2|eot|ttf|otf)$/,
use:[
{
loader:'file-loader',
options:{
name:'[name].[ext]', //[path] 上下文環(huán)境路徑
outputPath: dirPath.iconfont, //輸出路徑
publicPath: config.NODE_ENV === 'development'? dirPath.iconfont: dirPath.iconfont, //公共路徑
}
}
]
},
{
test: /\.js$/,
exclude: /(node_modules)/,
+ use:[
+ 'babel-loader',
+ {
+ loader:'eslint-loader'
+ }
+ ]
}
]
}
module.exports = _module;
build/devServer.js
const dirPath = require('./base/path');
const config = require('./base/config');
let devServer = {
+ overlay:true,
contentBase: dirPath.dist,
clientLogLevel: 'info',
open:true, //啟動時默認打開瀏覽器
host:'localhost', //域名 0.0.0.0局域網(wǎng)可訪問
port:config.port || '9999',
inline:true, //實時更新
hot:true, //熱替換
hotOnly:false, //禁止瀏覽器自動刷新
proxy:{
'/':{
target: config.apiUrl
},
'/upload':{
target: config.apiUrl
}
}
}
module.exports = devServer;
index.js
let a = 1;
console.log(a);
運行webpack
yarn run dev
可以看到我們在index.js
報了一個錯誤深滚,我們修復一下這個錯誤
const a = 1;
console.log(a);
刷新下瀏覽器奕谭,eslint
提示錯誤就不存在了
5.指定某個文件不使用eslint
當 ESLint 運行時,在確定哪些文件要檢測之前痴荐,它會在當前工作目錄中查找一個 .eslintignore 文件血柳。如果發(fā)現(xiàn)了這個文件,當遍歷目錄時生兆,將會應用這些偏好設置难捌。一次只有一個 .eslintignore 文件會被使用,所以,不是當前工作目錄下的 .eslintignore 文件將不會被用到根吁。
myProject
|-build
|-base
|-path.js
|-config.js
|-mode.js
|-entry.js
|-devtool.js
|-module.js
|-plugins.js
|-devServer.js
|-optimization.js
|-output.js
|-dist
|-node_modules
|-src
|-api
|-apiPath.js
|-util
|-math.js
|-assets
|-css
|-index.css
|-less
|-index.less
|-sass
|-index.scss
|-images
|-wali_logo.png
|-index.html
|-index.js
|-package.json
|-webpack.config.js
|-postcss.config.js
|-.babelrc
+|-.eslintignore
|-.eslintrc.js
|-package-lock.json
|-stats.json
創(chuàng)建一個.eslintignore
文件昆汹。將不想被eslint檢測的文件或目錄寫入到.eslintignore
配置中
.eslintignore
build/*
dist/*
node_modules/*