Skip to content

Commit 6f1d404

Browse files
committed
fix: should not remove eslint-loader on src import blocks (close #1359)
1 parent be2384c commit 6f1d404

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

Diff for: lib/loaders/pitcher.js

+33-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ const selfPath = require.resolve('../index')
55
const templateLoaderPath = require.resolve('./templateLoader')
66
const stylePostLoaderPath = require.resolve('./stylePostLoader')
77

8+
const isESLintLoader = l => /(\/|\\|@)eslint-loader/.test(l.path)
9+
const isNullLoader = l => /(\/|\\|@)null-loader/.test(l.path)
10+
const isCSSLoader = l => /(\/|\\|@)css-loader/.test(l.path)
11+
const isPitcher = l => l.path !== __filename
12+
13+
const dedupeESLintLoader = loaders => {
14+
const res = []
15+
let seen = false
16+
loaders.forEach(l => {
17+
if (!isESLintLoader(l)) {
18+
res.push(l)
19+
} else if (!seen) {
20+
seen = true
21+
res.push(l)
22+
}
23+
})
24+
return res
25+
}
26+
827
module.exports = code => code
928

1029
// This pitching loader is responsible for intercepting all vue block requests
@@ -16,17 +35,25 @@ module.exports.pitch = function (remainingRequest) {
1635

1736
let loaders = this.loaders
1837

19-
// if this is a language block request, remove eslint-loader to avoid
20-
// duplicate linting.
38+
// if this is a language block request, eslint-loader may get matched
39+
// multiple times
2140
if (query.type) {
22-
loaders = loaders.filter(l => !/(\/|\\|@)eslint-loader/.test(l.path))
41+
// if this is an inline block, since the whole file itself is being linted,
42+
// remove eslint-loader to avoid duplicate linting.
43+
if (/\.vue$/.test(this.resourcePath)) {
44+
loaders = loaders.filter(l => !isESLintLoader(l))
45+
} else {
46+
// This is a src import. Just make sure there's not more than 1 instance
47+
// of eslint present.
48+
loaders = dedupeESLintLoader(loaders)
49+
}
2350
}
2451

2552
// remove self
26-
loaders = loaders.filter(l => l.path !== __filename)
53+
loaders = loaders.filter(isPitcher)
2754

2855
// do not inject if user uses null-loader to void the type (#1239)
29-
if (loaders.some(l => /(\/|\\|@)null-loader/.test(l.path))) {
56+
if (loaders.some(isNullLoader)) {
3057
return
3158
}
3259

@@ -58,7 +85,7 @@ module.exports.pitch = function (remainingRequest) {
5885

5986
// Inject style-post-loader before css-loader for scoped CSS and trimming
6087
if (query.type === `style`) {
61-
const cssLoaderIndex = loaders.findIndex(l => /(\/|\\|@)css-loader/.test(l.path))
88+
const cssLoaderIndex = loaders.findIndex(isCSSLoader)
6289
if (cssLoaderIndex > -1) {
6390
const afterLoaders = loaders.slice(0, cssLoaderIndex + 1)
6491
const beforeLoaders = loaders.slice(cssLoaderIndex + 1)

0 commit comments

Comments
 (0)