Skip to content

Add webpack 5 support #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions lib/plugin-webpack4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
const RuleSet = require('webpack/lib/RuleSet')
const progressiveLoaderModule = require('../progressive-loader/module')
let vueLoaderPath
try {
vueLoaderPath = require.resolve('vue-loader')
} catch (err) {}

function isVueLoader (use) {
return use.ident === 'vue-loader-options' ||
use.loader === 'vue-loader' ||
(vueLoaderPath && use.loader === vueLoaderPath)
}

class VuetifyLoaderPlugin {
constructor (options) {
this.options = options || {}
}

apply (compiler) {
// 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 rules that apply to vue files
const vueRules = rules.filter(rule => rule.use && rule.use.find(isVueLoader))

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 = rule.use.find(isVueLoader).options
vueLoaderOptions.compilerOptions = vueLoaderOptions.compilerOptions || {}
vueLoaderOptions.compilerOptions.modules = vueLoaderOptions.compilerOptions.modules || []
vueLoaderOptions.compilerOptions.modules.push(progressiveLoaderModule)

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

const options = typeof this.options.progressiveImages === 'boolean'
? undefined
: this.options.progressiveImages

if (!imageRule) {
imageRule = {
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)(\?.*)?$/,
oneOf: [
{
test: /\.(png|jpe?g|gif)$/,
resourceQuery: options ? options.resourceQuery : /vuetify-preload/,
use: [
{
loader: 'vuetify-loader/progressive-loader',
options
},
{
loader: 'url-loader',
options: { limit: 8000 }
}
]
},
{
loader: 'url-loader',
options: { limit: 8000 }
}
]
}
rules.push(imageRule)
} else {
if (Array.isArray(imageRule.use)) {
imageRule.oneOf = [
{
test: /\.(png|jpe?g|gif)$/,
resourceQuery: options ? options.resourceQuery : /vuetify-preload/,
use: [
{
loader: 'vuetify-loader/progressive-loader',
options
},
...imageRule.use
]
},
...imageRule.use
]
} else if (imageRule.loader) {
imageRule.oneOf = [
{
test: /\.(png|jpe?g|gif)$/,
resourceQuery: options ? options.resourceQuery : /vuetify-preload/,
use: [
{
loader: 'vuetify-loader/progressive-loader',
options
},
{
loader: imageRule.loader,
options: imageRule.options
}
]
},
{
loader: imageRule.loader,
options: imageRule.options
}
]
}
delete imageRule.use
delete imageRule.loader
delete imageRule.options
}
}

rule.oneOf = [
{
resourceQuery: '?',
use: rule.use
},
{
use: [
{
loader: require.resolve('./loader'),
options: {
match: this.options.match || [],
attrsMatch: this.options.attrsMatch || []
}
},
...rule.use
]
},
]
delete rule.use
}
}

module.exports = VuetifyLoaderPlugin
186 changes: 186 additions & 0 deletions lib/plugin-webpack5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
const BasicEffectRulePlugin = require('webpack/lib/rules/BasicEffectRulePlugin')
const BasicMatcherRulePlugin = require('webpack/lib/rules/BasicMatcherRulePlugin')
const RuleSetCompiler = require('webpack/lib/rules/RuleSetCompiler')
const UseEffectRulePlugin = require('webpack/lib/rules/UseEffectRulePlugin')

const ruleSetCompiler = new RuleSetCompiler([
new BasicMatcherRulePlugin('test', 'resource'),
new BasicMatcherRulePlugin('include', 'resource'),
new BasicMatcherRulePlugin('exclude', 'resource', true),
new BasicMatcherRulePlugin('resource'),
new BasicMatcherRulePlugin('conditions'),
new BasicMatcherRulePlugin('resourceQuery'),
new BasicMatcherRulePlugin('realResource'),
new BasicMatcherRulePlugin('issuer'),
new BasicMatcherRulePlugin('compiler'),
new BasicEffectRulePlugin('type'),
new BasicEffectRulePlugin('sideEffects'),
new BasicEffectRulePlugin('parser'),
new BasicEffectRulePlugin('resolve'),
new UseEffectRulePlugin()
])

const progressiveLoaderModule = require('../progressive-loader/module')
let vueLoaderPath
try {
vueLoaderPath = require.resolve('vue-loader')
} catch (err) { }

function isVueLoader(use) {
return use.ident === 'vue-loader-options' ||
use.loader === 'vue-loader' ||
(vueLoaderPath && use.loader === vueLoaderPath)
}

class VuetifyLoaderPlugin {
constructor(options) {
this.options = options || {}
}

apply(compiler) {
// use webpack's RuleSet utility to normalize user rules
const { rules } = compiler.options.module;
let vueRules;
for (const rawRule of rules) {
// skip rules with 'enforce'. eg. rule for eslint-loader
if (rawRule.enforce) {
continue
}
// skip the `include` check when locating the vue rule
const clonedRawRule = Object.assign({}, rawRule)
delete clonedRawRule.include

const ruleSet = ruleSetCompiler.compile([{
rules: [clonedRawRule]
}])
vueRules = ruleSet.exec({
resource: 'foo.vue'
})
if (vueRules.length > 0) {
break;
}
}

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.`
)
}

// find the rules that apply to vue files
vueRules = rules.filter(rule => rule.use && rule.use.find(isVueLoader))
vueRules.forEach(this.updateRule.bind(this))
compiler.options.module.rules = rules;
}

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

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

const options = typeof this.options.progressiveImages === 'boolean'
? undefined
: this.options.progressiveImages

if (!imageRule) {
imageRule = {
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)(\?.*)?$/,
oneOf: [
{
test: /\.(png|jpe?g|gif)$/,
resourceQuery: options ? options.resourceQuery : /vuetify-preload/,
use: [
{
loader: 'vuetify-loader/progressive-loader',
options
},
{
loader: 'url-loader',
Copy link

@merceyz merceyz Dec 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these loaders needs to be resolved up front using require.resolve

Ref https://yarnpkg.com/advanced/rulebook#packages-should-only-ever-require-what-they-formally-list-in-their-dependencies

options: { limit: 8000 }
}
]
},
{
loader: 'url-loader',
options: { limit: 8000 }
}
]
}
rules.push(imageRule)
} else {
if (Array.isArray(imageRule.use)) {
imageRule.oneOf = [
{
test: /\.(png|jpe?g|gif)$/,
resourceQuery: options ? options.resourceQuery : /vuetify-preload/,
use: [
{
loader: 'vuetify-loader/progressive-loader',
options
},
...imageRule.use
]
},
...imageRule.use
]
} else if (imageRule.loader) {
imageRule.oneOf = [
{
test: /\.(png|jpe?g|gif)$/,
resourceQuery: options ? options.resourceQuery : /vuetify-preload/,
use: [
{
loader: 'vuetify-loader/progressive-loader',
options
},
{
loader: imageRule.loader,
options: imageRule.options
}
]
},
{
loader: imageRule.loader,
options: imageRule.options
}
]
}
delete imageRule.use
delete imageRule.loader
delete imageRule.options
}
}

rule.oneOf = [
{
resourceQuery: '?',
use: rule.use
},
{
use: [
{
loader: require.resolve('./loader'),
options: {
match: this.options.match || [],
attrsMatch: this.options.attrsMatch || []
}
},
...rule.use
]
},
]
delete rule.use
}
}

module.exports = VuetifyLoaderPlugin
Loading