熟悉 eslint-loader 的同学一般如下配置:

设置一下几项:

  • test : A condition that must be met(一般是处理对应文件的正则)
  • exclude : A condition that must not be met(手动添加不需要处理的,一般比如 node_modules)
  • loader : An array of paths or files where the imported files will be transformed by the loader(对应 loader 的名字)
  • options(可选参数对象)

module.exports = {
// ...
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "eslint-loader",
options: {
// eslint options (if necessary)
}
},
],
},
// ...
}

当然还可以加上 enforce: "pre"

To be safe, you can use enforce: "pre" section to check source files, not modified by other loaders (like babel-loader)


module.exports = {
// ...
module: {
rules: [
{
enforce: "pre",
test: /\.js$/,
exclude: /node_modules/,
loader: "eslint-loader"
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader"
},
],
},
// ...
}

我们看一下 @vue/cli-plugin-eslint 的实现,先用 vue inspect --rule eslint 看一下最终生成的配置:


/* config.module.rule('eslint') */
{
enforce: 'pre',
test: /\.(vue|(j|t)sx?)$/,
exclude: [
/node_modules/,
'/Users/***/node_modules/@vue/cli-service/lib'
],
use: [
/* config.module.rule('eslint').use('eslint-loader') */
{
loader: 'eslint-loader',
options: {
extensions: [
'.js',
'.jsx',
'.vue'
],
cache: true,
cacheIdentifier: '65e8f1b4',
emitWarning: true,
emitError: false,
formatter: function () {
/* omitted long function */
}
}
}
]
}

我们看一下 cli-plugin-eslint/index.js


module.exports = (api, options) => {}

我们看一下 vue.config.js 的配置:lintOnSave

是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码。

我们看一下 @vue/cli-service/lib/options.js 的配置:

1、默认是:


lintOnSave: true

2、支持下面几个备选值:


lintOnSave: joi.any().valid([true, false, 'error'])

不然会报错:


ERROR Invalid options in vue.config.js: child "lintOnSave" fails because ["lintOnSave" must be one of [true, false, error]]

接下来就是通过 api.chainWebpack 来设置 webpackConfig


api.chainWebpack(webpackConfig => {
})

所以开始的设置 rule 为 eslint,然后设置:preexclude


webpackConfig.module
.rule('eslint')
.pre()
.exclude
.add(/node_modules/)
.add(require('path').dirname(require.resolve('@vue/cli-service')))
.end()

这里 add2 个:


.add(/node_modules/)
.add(require('path').dirname(require.resolve('@vue/cli-service')))

然后设置 test


.test(/\.(vue|(j|t)sx?)$/)

再设置 useloader


.use('eslint-loader')
.loader('eslint-loader')
.options({
})

这里的 options 分为如下几个:

1、extensions

An array of filename extensions that should be checked for code. The default is an array containing just ".js".

2、cache

Operate only on changed files (default: false).

3、cacheIdentifier

4、emitWarning

默认 false,Loader will always return warnings if option is set to true.

5、emitError

默认 false,Loader will always return errors if this option is set to true.

6、formatter

Loader accepts a function that will have one argument: an array of eslint messages (object). The function must return the output as a string. You can use official eslint formatters.

之前用的比较多的是:


require("eslint-friendly-formatter")

来源:https://segmentfault.com/a/1190000016236878

[Vue CLI 3] @vue/cli-plugin-eslint 源码分析的更多相关文章

  1. Vue.js 源码分析(二) 基础篇 全局配置

    Vue.config是一个对象,包含Vue的全局配置,可以在启动应用之前修改下列属性,如下: ptionMergeStrategies        ;自定义合并策略的选项silent         ...

  2. MyBatis源码分析(2)—— Plugin原理

    @(MyBatis)[Plugin] MyBatis源码分析--Plugin原理 Plugin原理 Plugin的实现采用了Java的动态代理,应用了责任链设计模式 InterceptorChain ...

  3. Vue.js 源码分析(三十二) 总结

    第一次写博客,坚持了一个多月时间,Vue源码分析基本分析完了,回过头也看也漏了一些地方,比如双向绑定里的观察者模式,也可以说是订阅者模式,也就是Vue里的Dep.Watcher等这些函数的作用,网上搜 ...

  4. Vue源码分析(一) : new Vue() 做了什么

    Vue源码分析(一) : new Vue() 做了什么 author: @TiffanysBear 在了解new Vue做了什么之前,我们先对Vue源码做一些基础的了解,如果你已经对基础的源码目录设计 ...

  5. vue双向绑定的原理及实现双向绑定MVVM源码分析

    vue双向绑定的原理及实现双向绑定MVVM源码分析 双向数据绑定的原理是:可以将对象的属性绑定到UI,具体的说,我们有一个对象,该对象有一个name属性,当我们给这个对象name属性赋新值的时候,新值 ...

  6. 前端Vue 源码分析-逻辑层

    Vue 源码分析-逻辑层 预期的效果: 监听input的输入,input在输入的时候,会触发 watch与computed函数,并且会更新原始的input的数值.所以直接跟input相关的处理就有3处 ...

  7. 基于vue实现一个简单的MVVM框架(源码分析)

    不知不觉接触前端的时间已经过去半年了,越来越发觉对知识的学习不应该只停留在会用的层面,这在我学jQuery的一段时间后便有这样的体会. 虽然jQuery只是一个JS的代码库,只要会一些JS的基本操作学 ...

  8. [Vue源码分析] v-model实现原理

    最近小组有个关于vue源码分析的分享会,提前准备一下… 前言:我们都知道使用v-model可以实现数据的双向绑定,及实现数据的变化驱动dom的更新,dom的更新影响数据的变化.那么v-model是怎么 ...

  9. Vue系列---理解Vue.nextTick使用及源码分析(五)

    _ 阅读目录 一. 什么是Vue.nextTick()? 二. Vue.nextTick()方法的应用场景有哪些? 2.1 更改数据后,进行节点DOM操作. 2.2 在created生命周期中进行DO ...

  10. Vue.js 源码分析(六) 基础篇 计算属性 computed 属性详解

    模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护,比如: <div id="example">{{ messag ...

随机推荐

  1. Linux-c glib库hash表GHashTable介绍

    百度云glib  链接:https://pan.baidu.com/s/1W9qdlMKWRKIFykenTVuWNQ 密码:ol6y hash表是一种提供key-value访问的数据结构,通过指定的 ...

  2. golang Linux下编译环境搭建

    1.下载golang1.4和1.10源码(1.4以后的版本都用1.4go编译安装,所以先安装1.4) 2.解压后我的目录结构是: /opt/xxx/golang |-------gopath     ...

  3. ajaxStart 和 ajaxSend 不执行

    我们一般会在loading 效果的时候会用上这两个全局事件 ajaxStart 和 ajaxSend 但是要注意的是 在同时有多个ajax 执行的时候ajaxStart 只会执行一次 所以一般情况下 ...

  4. pyinstaller 打包python3.6文件成exe 运行

    1.安装pyinstaller  切换到安装目录下script 运行  如我的目录:F:\Program Files\Python36\Scripts pip  install pyinstaller ...

  5. Django项目:CRM(客户关系管理系统)--74--64PerfectCRM实现CRM课程排名详情

    #urls.py """PerfectCRM URL Configuration The `urlpatterns` list routes URLs to views. ...

  6. vue.js_07_vue-resource的请求方式

    1.vue-resource 实现 get, post, jsonp请求 <body> <div id="app"> <input type=&quo ...

  7. mysql基础教程(二)-----分组函数、多表查询、常见函数

    分组函数 什么是分组函数 分组函数作用于一组数据,并对一组数据返回一个值. 组函数类型 • AVG() • COUNT() • MAX() • MIN() • SUM() 组函数语法 AVG(平均值) ...

  8. leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径

    设计最短路径 用bfs 天然带最短路径 每一个状态是 当前的阶段 和已经访问过的节点 下面是正确但是超时的代码 class Solution: def shortestPathLength(self, ...

  9. css3之属性选择器

    总体来看一下都有哪些选择器 1.属性选择器  1)[att*=val] 2)[att^=val] 3)[att$=val] 2.结构伪类选择器 3.UI伪类选择器 其中E:read-only伪类选择器 ...

  10. LeetCode389Find the Difference找不同

    给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = "abcd&quo ...