[英]eslint on dynamically changing file list
I'd like to run eslint
on modified files only. 我
eslint
对修改后的文件运行eslint
。 I've created a new run target in package.json
to run eslint
from command line ( git diff --name-only --relative | grep -E '.*\\\\.(vue|js)$' | xargs eslint --ext .js,.vue
). 我已经在
package.json
创建了一个新的运行目标,以便从命令行运行eslint
( git diff --name-only --relative | grep -E '.*\\\\.(vue|js)$' | xargs eslint --ext .js,.vue
eslint
git diff --name-only --relative | grep -E '.*\\\\.(vue|js)$' | xargs eslint --ext .js,.vue
)。 In theory, this should work fine, but there's a little transformation step happening in my project (a string replacement) when bundling the files with webpack that will throw off eslint
(some non-standard markup will be expanded to JS). 从理论上讲,这应该可以正常工作,但是当使用webpack捆绑文件时,我的项目中发生了一些转换步骤(字符串替换),这将使
eslint
(某些非标准标记将扩展为JS)。
What are my options and how would I go about implementing them? 我有什么选择,我将如何实施它们? For instance, could I execute a particular webpack rule/loader and pipe the result to
eslint
? 例如,我可以执行特定的webpack规则/加载程序并将结果传递给
eslint
吗? Another option I see is to include eslint
into the webpack rule/loader process (instead of executing it from the command line), but how would I then filter on files that are currently modified (could this be handled by a temporary file that contains the git diff...
result?) 我看到的另一个选择是将
eslint
包含在webpack规则/加载器进程中(而不是从命令行执行),但是我将如何过滤当前已修改的文件(可以由包含以下内容的临时文件处理) git diff...
结果?)
I've got a somewhat working approach. 我有一种可行的方法。 I chose to modify
webpack.base.conf.js
instead of going for the command line solution to make use of the already existing string replacement loader. 我选择修改
webpack.base.conf.js
而不是使用命令行解决方案来利用已经存在的字符串替换加载程序。
The files are collected in the WebpackBeforeBuildPlugin
callback function and instead of a regex based test variable, a function is used which checks against the previously collected files. 这些文件收集在
WebpackBeforeBuildPlugin
回调函数中,而不是使用基于正则表达式的测试变量,而是使用一个函数检查先前收集的文件。
const exec = require('child_process').exec;
const WebpackBeforeBuildPlugin = require('before-build-webpack');
var modFilesList = new Set([]);
const srcPath = resolve('.');
...
rules: [{
test: function(filename) {
let relFilename = path.relative(srcPath, filename);
let lint = modFilesList.has(relFilename);
return lint
},
loader: 'eslint-loader',
include: resolve('src'),
exclude: /node_modules/,
options: {
formatter: require('eslint-friendly-formatter'),
cache: false
}
}, {
... other string replacement loader ...
}
plugins: [
...
new WebpackBeforeBuildPlugin(function(stats, callback) {
// Collect changed files before building.
let gitCmd = 'git diff --name-only --relative | grep -E ".*\\.(vue|js)$"';
const proc = exec(gitCmd, (error, stdout, stderr) => {
if (stdout) {
let files = stdout.split('\n');
modFilesList = new Set(files);
}
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
callback();
})
]
The only problem at the moment is that when git file changes occur, they don't trigger a re-linting based on these file changes (ie new file is changed, or previously (before starting webpack-dev-server) changed file changes are discarded). 当前唯一的问题是,当git文件发生更改时,它们不会基于这些文件更改(即,新文件已更改,或之前(在启动webpack-dev-server之前)更改的文件更改)触发重新lint丢弃)。 I checked everything I could.
我检查了一切。 The change is registered and stored in
modFilesList
, the test function is executed and returns true (for a new change in a previously unchanged file) or false in case the change was discarded. 更改将被注册并存储在
modFilesList
,执行测试功能并返回true(对于以前未更改的文件中的新更改)或false(如果更改被放弃)。 I also played with the cache
option to no avail. 我也玩过
cache
选项无济于事。 It seems that at initial load, eslint-loader
caches the files it will lint in future (don't know if that's a result of using a test function instead of a regex or also the case with the regex). 似乎在初始加载时,
eslint-loader
缓存了将来将要掉毛的文件(不知道这是使用测试功能而不是regex还是regex的结果)。 Is anyone having an idea or has seen this before (eslint-loader not updating the file list)? 是否有人有想法或曾经见过此想法(eslint-loader未更新文件列表)?
Update 更新资料
This seems to be a problem with webpack (or one of the other loaders) as the eslint-loader isn't even executed when the file changed. 这似乎是webpack(或其他加载程序之一)的问题,因为文件更改后甚至没有执行eslint加载程序。 The
test
function however is executed which is a bit weird. 但是执行了
test
功能,这有点奇怪。 I don't fully understand how loaders work or how they play together, so there might be some other loader that is causing this... 我不完全了解装载机的工作方式或它们如何共同发挥作用,因此可能还有其他装载机导致了此情况...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.