Skip to content

Commit

Permalink
Merge pull request #333 from zaycker/bugfix/oneof-and-rules-composito…
Browse files Browse the repository at this point in the history
…ns-of-rule

`oneOf` and `rules` compositions of rule support
  • Loading branch information
kisenka authored Apr 27, 2019
2 parents 48b1921 + 5ee8417 commit 5528c3d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/utils/get-matched-rule.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// eslint-disable-next-line import/no-extraneous-dependencies
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.use || []);
}, []);

module.exports = (compiler) => {
const rawRules = compiler.options.module.rules;
const { rules } = new RuleSet(rawRules);
const rule = rules
.reduce((pre, cur) => pre.concat(cur.use || []), [])
const rule = flattenAndExtractUse(rules)
.find((item) => {
return /svg-sprite-loader/.test(item.loader);
});
}) || {};

return rule.options || {};
};
26 changes: 26 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const {
rules,
multiRule,
svgRule,
svgInsideOneOfRule,
svgInsideRulesRule,
compile,
extractPlugin,
extractCSSRule,
Expand Down Expand Up @@ -357,6 +359,30 @@ describe('loader and plugin', () => {
Object.keys(assets).should.be.lengthOf(1);
});

it('should support `oneOf` composition of rule', async () => {
const { assets } = await compile({
entry: './entry',
module: rules(
svgInsideOneOfRule()
),
plugins: [new SpritePlugin()]
});

Object.keys(assets).should.be.lengthOf(1);
});

it('should support `rules` composition of rule', async () => {
const { assets } = await compile({
entry: './entry',
module: rules(
svgInsideRulesRule()
),
plugins: [new SpritePlugin()]
});

Object.keys(assets).should.be.lengthOf(1);
});

it('should allow to specify custom sprite filename', async () => {
const spriteFilename = 'qwe.svg';

Expand Down
26 changes: 26 additions & 0 deletions test/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ function svgRule(opts) {
});
}

function svgInsideOneOfRule(opts) {
const options = merge({}, opts || {});

return rule({
oneOf: [{
test: /\.svg$/,
loader: loaderPath,
options
}]
});
}

function svgInsideRulesRule(opts) {
const options = merge({}, opts || {});

return rule({
rules: [{
test: /\.svg$/,
loader: loaderPath,
options
}]
});
}

/**
* @see for webpack 1 - https://github.com/webpack-contrib/extract-text-webpack-plugin/blob/webpack-1/README.md#api
* @see for webpack 2 - https://github.com/webpack-contrib/extract-text-webpack-plugin#options
Expand Down Expand Up @@ -109,6 +133,8 @@ module.exports.rule = rule;
module.exports.rules = rules;
module.exports.multiRule = multiRule;
module.exports.svgRule = svgRule;
module.exports.svgInsideOneOfRule = svgInsideOneOfRule;
module.exports.svgInsideRulesRule = svgInsideRulesRule;
module.exports.compile = compile;
module.exports.compileAndNotReject = compileAndNotReject;
module.exports.createCompiler = createCompiler;
Expand Down

0 comments on commit 5528c3d

Please sign in to comment.