Skip to content
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

fix(utils): fix: some options had no effect with webpack5 #460

Merged
merged 1 commit into from Jun 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 16 additions & 52 deletions lib/utils/get-matched-rule-5.js
Expand Up @@ -2,62 +2,26 @@
/* eslint-disable import/no-unresolved */
/* eslint-disable no-restricted-syntax */
// eslint-disable-next-line import/no-extraneous-dependencies
const BasicEffectRulePlugin = require('webpack/lib/rules/BasicEffectRulePlugin');
const BasicMatcherRulePlugin = require('webpack/lib/rules/BasicMatcherRulePlugin');
const RuleSetCompiler = require('webpack/lib/rules/RuleSetCompiler');
const DescriptionDataMatcherRulePlugin = require('webpack/lib/rules/DescriptionDataMatcherRulePlugin');
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 BasicEffectRulePlugin('generator'),
new DescriptionDataMatcherRulePlugin(),
new UseEffectRulePlugin()
]);

// const RuleSet = require('webpack/lib/RuleSet');

const flattenAndExtractUse = rules => rules.reduce((pre, rule) => {
// if ('rules' in rule || 'oneOf' in rule) {
// return pre.concat(flattenAndExtractUse(rule.rules || rule.oneOf));
// }
return pre.concat(rule || []);
}, []);
const isSpriteLoader = (rule) => {
if (!Object.prototype.hasOwnProperty.call(rule, 'loader')) return false;
return /svg-sprite-loader/.test(rule.loader);
};

module.exports = (compiler) => {
const rawRules = compiler.options.module.rules;
const rulesUse = [];
let spriteLoader = null;
for (const rawRule of rawRules) {
const clonedRawRule = Object.assign({}, rawRule);
delete clonedRawRule.include;
const ruleSet = ruleSetCompiler.compile([{
rules: [clonedRawRule]
}]);
rulesUse.push(ruleSet.exec({
resource: '.svg'
}));
if (isSpriteLoader(rawRule)) {
spriteLoader = rawRule;
} else if (Object.prototype.hasOwnProperty.call(rawRule, 'use')) {
for (const subLoader of rawRule.use) {
if (isSpriteLoader(subLoader)) {
spriteLoader = subLoader;
}
}
}
if (spriteLoader !== null) break;
}

// const {
// rules
// } = ruleSet;

const rule = flattenAndExtractUse(rulesUse)
.find((item) => {
return /svg-sprite-loader/.test(item.value.loader);
}) || {};

return rule.value ? rule.value.options : {};
return (spriteLoader !== null && spriteLoader.options !== undefined) ? spriteLoader.options : {};
};