博客
关于我
webpack配置【一】webpack-cli中加入eslint规范,自动化测试,浮层错误抛出
阅读量:531 次
发布时间:2019-03-08

本文共 2519 字,大约阅读时间需要 8 分钟。

eslint检测和修复

为了确保项目代码的质量,eslint是一个强大的代码检测工具。本文将介绍如何在项目中集成eslint进行自动检测和修复。

1.下载依赖

通过cnpm安装eslint及相关依赖:
cnpm i eslint --save-dev  
cnpm i eslint-loading --save-dev

2.配置eslint

在项目根目录中创建一个新文件
.eslintrc.js
module.exports={    env: {        browser: true,        commonjs: true,        es6: true,        node: true,    },    extends: 'eslint:recommended',    parserOptions: {        sourceType: 'module',    },    rules: {        'comma-dangle': ['error', 'always-multiline'],        indent: ['error',2],        'linebreak-style': ['error','unix'],        quotes: ['error', 'single'],        semi: ['error','always'],        'no-unused-vars': ['warn'],        'no-console': 0,    },};

3.在pakage.json中配置脚本

添加以下脚本:
"scripts": {    "test": "echo \"Error: no test specified\" && exit 1",    "start": "webpack-dev-server --env development",    "build": "webpack --env production",    "lintjs": "eslint app/ webpack.*.js --cache"  },

4.运行eslint

执行以下命令进行检测:
npm run lintjs
若需要自动修复错误可执行:
npm run lintjs -- --fix

5.在webpack.config.js中集成eslint

在module中添加eslint-loader规则:
const path=require('path');//webpack生成htmlconst HtmlWebpackPlugin=require('html-webpack-plugin');const PATHS={  app: path.join(__dirname, 'app'),  build: path.join(__dirname, 'build'),};module.exports={  devServer:{     host: process.env.HOST, //Defaults to 'localhost    port: 80,  //Defalut to 8080  },  entry:{    app:PATHS.app,  },  output: {    path: PATHS.build,    filename: '[name].js',  },  module:{    //运行监测    rules:[      {        test: /\.js$/,        enforce: 'pre',                loader:'eslint-loader',        options:{          emitWarning: true,        },      },    ],  },  plugins:[    //生成html    new HtmlWebpackPlugin({      title: 'Webpack demo',    }),  ],};

6.运行项目

启动开发服务器:
npm run start

7.错误浮层显示

在devServer中配置显示错误浮层:
const path=require('path');const HtmlWebpackPlugin=require('html-webpack-plugin');const PATHS={  app: path.join(__dirname, 'app'),  build: path.join(__dirname, 'build'),};module.exports={  devServer:{     host: process.env.HOST, //Defaults to 'localhost    port: 80,  //Defalut to 8080    // 浮层错误抛出    overlay: {      errors: true,      warnings: true,    },  },  entry:{    app:PATHS.app,  },  output: {    path: PATHS.build,    filename: '[name].js',  },  module:{    rules:[      {        test: /\.js$/,        enforce: 'pre',                loader:'eslint-loader',        options:{          emitWarning: true,        },      },    ],  },  plugins:[    new HtmlWebpackPlugin({      title: 'Webpack demo',    }),  ],};

转载地址:http://xrmnz.baihongyu.com/

你可能感兴趣的文章
Nacos Derby 远程命令执行漏洞(QVD-2024-26473)
查看>>
Nacos 与 Eureka、Zookeeper 和 Consul 等其他注册中心的区别
查看>>
Nacos 单机集群搭建及常用生产环境配置 | Spring Cloud 3
查看>>
Nacos 启动报错[db-load-error]load jdbc.properties error
查看>>
Nacos 报Statement cancelled due to timeout or client request
查看>>
Nacos 注册服务源码分析
查看>>
Nacos 融合 Spring Cloud,成为注册配置中心
查看>>
Nacos-注册中心
查看>>
Nacos-配置中心
查看>>
Nacos2.X 源码分析:为订阅方推送、服务健康检查、集群数据同步、grpc客户端服务端初始化
查看>>
Nacos2.X 配置中心源码分析:客户端如何拉取配置、服务端配置发布客户端监听机制
查看>>
Nacos2.X源码分析:服务注册、服务发现流程
查看>>
NacosClient客户端搭建,微服务注册进nacos
查看>>
Nacos中使用ribbon
查看>>
Nacos使用OpenFeign
查看>>
Nacos使用Ribbon
查看>>
Nacos做注册中心使用
查看>>
Nacos做配置中心使用
查看>>
Nacos入门过程的坑--获取不到配置的值
查看>>
Nacos原理
查看>>