Skip to content

Commit

Permalink
fix(utils): fix: some options had no effect with webpack5
Browse files Browse the repository at this point in the history
ruleSet.exec({resource: '.svg'})` was used to find `svg-sprite-loader` options object
But this approach does not work if we define a rule with something like `test: /svg-sprites.*?\.svg/`

ISSUES CLOSED: JetBrains#446
  • Loading branch information
KycKyc committed Jun 5, 2021
1 parent ade0adf commit a89fb92
Showing 1 changed file with 16 additions and 52 deletions.
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.options !== undefined ? spriteLoader.options : {};
};

0 comments on commit a89fb92

Please sign in to comment.