Skip to content

Commit

Permalink
fix: should not remove eslint-loader on src import blocks (close #1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 17, 2018
1 parent be2384c commit 6f1d404
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions lib/loaders/pitcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ const selfPath = require.resolve('../index')
const templateLoaderPath = require.resolve('./templateLoader')
const stylePostLoaderPath = require.resolve('./stylePostLoader')

const isESLintLoader = l => /(\/|\\|@)eslint-loader/.test(l.path)
const isNullLoader = l => /(\/|\\|@)null-loader/.test(l.path)
const isCSSLoader = l => /(\/|\\|@)css-loader/.test(l.path)
const isPitcher = l => l.path !== __filename

const dedupeESLintLoader = loaders => {
const res = []
let seen = false
loaders.forEach(l => {
if (!isESLintLoader(l)) {
res.push(l)
} else if (!seen) {
seen = true
res.push(l)
}
})
return res
}

module.exports = code => code

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

let loaders = this.loaders

// if this is a language block request, remove eslint-loader to avoid
// duplicate linting.
// if this is a language block request, eslint-loader may get matched
// multiple times
if (query.type) {
loaders = loaders.filter(l => !/(\/|\\|@)eslint-loader/.test(l.path))
// if this is an inline block, since the whole file itself is being linted,
// remove eslint-loader to avoid duplicate linting.
if (/\.vue$/.test(this.resourcePath)) {
loaders = loaders.filter(l => !isESLintLoader(l))
} else {
// This is a src import. Just make sure there's not more than 1 instance
// of eslint present.
loaders = dedupeESLintLoader(loaders)
}
}

// remove self
loaders = loaders.filter(l => l.path !== __filename)
loaders = loaders.filter(isPitcher)

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

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

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

0 comments on commit 6f1d404

Please sign in to comment.