Skip to content

Commit

Permalink
fix: handle multiple vue-loader rules
Browse files Browse the repository at this point in the history
fixes #106
  • Loading branch information
KaelWD committed Jul 11, 2020
1 parent 86751a1 commit 9f0e669
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,36 @@ class VuetifyLoaderPlugin {
// use webpack's RuleSet utility to normalize user rules
const rawRules = compiler.options.module.rules
const { rules } = new RuleSet(rawRules)
this.rules = rules

// find the rule that applies to vue files
const vueRuleIndex = rules.findIndex(rule => rule.use && rule.use.find(isVueLoader))
const vueRule = rules[vueRuleIndex]
// find the rules that apply to vue files
const vueRules = rules.filter(rule => rule.use && rule.use.find(isVueLoader))

if (!vueRule) {
if (!vueRules.length) {
throw new Error(
`[VuetifyLoaderPlugin Error] No matching rule for vue-loader found.\n` +
`Make sure there is at least one root-level rule that uses vue-loader.`
)
}

vueRules.forEach(this.updateRule.bind(this))

compiler.options.module.rules = rules
}

updateRule (rule) {
if (this.options.progressiveImages) {
const vueLoaderOptions = vueRule.use.find(isVueLoader).options
const vueLoaderOptions = rule.use.find(isVueLoader).options
vueLoaderOptions.compilerOptions = vueLoaderOptions.compilerOptions || {}
vueLoaderOptions.compilerOptions.modules = vueLoaderOptions.compilerOptions.modules || []
vueLoaderOptions.compilerOptions.modules.push(progressiveLoaderModule)

const imageRuleIndex = rules.findIndex(rule => {
const imageRuleIndex = this.rules.findIndex(rule => {
return rule.resource &&
!rule.resourceQuery &&
['.png', '.jpg', '.jpeg', '.gif'].some(ext => rule.resource(ext))
})
let imageRule = rules[imageRuleIndex]
let imageRule = this.rules[imageRuleIndex]

const options = typeof this.options.progressiveImages === 'boolean'
? undefined
Expand Down Expand Up @@ -118,10 +124,10 @@ class VuetifyLoaderPlugin {
}
}

vueRule.oneOf = [
rule.oneOf = [
{
resourceQuery: '?',
use: vueRule.use
use: rule.use
},
{
use: [
Expand All @@ -132,13 +138,11 @@ class VuetifyLoaderPlugin {
attrsMatch: this.options.attrsMatch || []
}
},
...vueRule.use
...rule.use
]
},
]
delete vueRule.use

compiler.options.module.rules = rules
delete rule.use
}
}

Expand Down

0 comments on commit 9f0e669

Please sign in to comment.