1、下载
cnpm install -D eslint-config-airbnb-base eslint eslint-plugin-import eslint-loader
使用airbnb-base库来进行代码检查
2、配置
module:{
rules:[
{
test: /\.js$/,
exclude:/node_modules/, 不检查node_modules
loader: 'eslint-loader',
enforce:'pre', 优先执行该loader,避免其他loader对js文件处理后(如兼容性处理)报错
options: {
fix:true 检查出错误后自动修复
}
}
]
},
修改package.json,添加eslintConfig,与devDependencies同级设置
"eslintConfig": {
"extends": "airbnb-base" 使用airbnb-base库进行代码检查
}
3、避开代码检查
在需要避开检查的代码上一行添加如下注释
//eslint-disable-next-line
代码示例:
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin= require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
process.env.NODE_ENV='development'
module.exports = {
entry: './src/js/index.js',
output: {
filename: 'src/js/build.js',
path:path.resolve(__dirname,'build')
},
module: {
rules: [//创建模块时,匹配请求的规则数组。这些规则能够修改模块的创建方式。这些规则能够对模块(module)应用 loader,或者修改解析器(parser)。
{
test: /\.css$/, //匹配文件名
use: [//使用哪些loader
// 'style-loader', //创建style标签,将js中的css样式资源添加到style标签中生效
MiniCssExtractPlugin.loader, //取代style-loader,提取js中的css为单独文件
'css-loader', //css文件变成commonjs模块,里面内容是样式字符串
{
loader: 'postcss-loader',
ident: 'postcss',
options: {
postcssOptions: {
plugins: [['postcss-preset-env',{
}]]
}
}
}
]
},
{
test: /\.less$/,
use: [
'style-loader',
'css-loader',
'less-loader', //将less文件编译成css文件
]
},
{
test: /\.(jpg|png|gif|jfif)$/,
loader:'url-loader', //依赖于file-loader
options: {
//图片小于8kb,会处理成base64,即减少请求数量,增大图片体积(请求速度会变慢cn)
limit: 8 * 1024,
esModule: false, //不采用es6模式解析,否则会和html-loader采用的commonjs模式冲突
name: '[name].[ext]',
outputPath:'src/images'
}
},
{
test: /\.html$/,
loader:'html-loader', //处理html文档中的img,负责引入图片,交给url-loader处理
},
{
// exclude:/\.xx$/
test: /\.(ttf|eot|svg|woff|woff2)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath:'src/icon'
},
},
{
//不检查node_modules
test: /\.js$/,
exclude:/node_modules/,
loader: 'eslint-loader',
enforce:'pre',
options: {
fix:true
}
}
]
},
//插件
plugins: [
new HtmlWebpackPlugin({
template:'./src/index.html' //将模板复制成指定文件
}), //默认传键html文件,并引入打包输出的资源,默认为基本结构
new MiniCssExtractPlugin({
filename: 'src/css/build.css'
}),
new OptimizeCssAssetsPlugin()
],
mode: 'development',
//自动化,自动编译、打开浏览器和自动刷新等
devServer: {
// contentBase:['xx','xx',...]
contentBase: path.resolve(__dirname, 'build'), //提供内容的目录
//启动gzip压缩
compress: true,
port: 3000, //监听端口号
open:true, //自动打开浏览器
}
}
package.json:
{
"name": "demo1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^5.0.1",
"eslint": "^7.17.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-loader": "^4.0.2",
"eslint-plugin-import": "^2.22.1",
"file-loader": "^6.2.0",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^4.5.1",
"less": "^3.13.1",
"less-loader": "^7.2.1",
"mini-css-extract-plugin": "^1.3.3",
"optimize-css-assets-webpack-plugin": "^5.0.4",
"postcss-loader": "^4.1.0",
"postcss-preset-env": "^6.7.0",
"style-loader": "^2.0.0",
"url-loader": "^4.1.1",
"webpack": "^4.41.6",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.11.1"
},
"browserslist": {
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
],
"production": [
">0.2%",
"not dead",
"not op_mini all"
]
},
"eslintConfig": {
"extends": "airbnb-base"
}
}