JetBrains / svg-sprite-loader

Webpack loader for creating SVG sprites.
MIT License
2.01k stars 271 forks source link

The plugin doesn-t see rules if config in oneOf #500

Open artemkliaus opened 1 year ago

artemkliaus commented 1 year ago

Do you want to request a feature, report a bug or ask a question? A bug

What is the current behavior? Then rules for the plugin set in webpack config webpack rule oneof Script doesn't get it. What is the expected behavior? The plugin correct read the 'oneof' rule. If the current behavior is a bug, please provide the steps to reproduce, at least part of webpack config with loader configuration and piece of your code.

...
    rules: [
      {
        test: /\.css$/,
        oneOf: [
          {
            resourceQuery: /inline/, // foo.css?inline
            use: 'url-loader',
          },
          {
            resourceQuery: /external/, // foo.css?external
            use: 'file-loader',
          },
        ],
      },
    ],
    ...

The best way is to create repo with minimal setup to demonstrate a problem (package.json, webpack config and your code). It you don't want to create a repository - create a gist with multiple files

If this is a feature request, what is motivation or use case for changing the behavior?

Please tell us about your environment:

Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow, gitter, etc)

The bug in this file - https://github.com/JetBrains/svg-sprite-loader/blob/master/lib/utils/get-matched-rule-5.js#L17

AdelFattakhova commented 1 year ago

@artemkliaus same thing! did you find any workaround, or should we fork and fix it on our own?

yunsii commented 1 year ago

i patched package with pnpm like below:

diff --git a/lib/utils/get-matched-rule-5.js b/lib/utils/get-matched-rule-5.js
index 8b3f27a64a0c2b1492daa750f6d91c6ed0ddb155..ccffadaed0014390c9f895faa9251c45a7a7585d 100644
--- a/lib/utils/get-matched-rule-5.js
+++ b/lib/utils/get-matched-rule-5.js
@@ -8,23 +8,33 @@ const isSpriteLoader = (rule) => {
   return /svg-sprite-loader/.test(rule.loader);
 };

-module.exports = (compiler) => {
-  const rawRules = compiler.options.module.rules;
-  let spriteLoader = null;
+const getTargetRule = (rawRules) => {
   for (const rawRule of rawRules) {
     if (isSpriteLoader(rawRule)) {
-      spriteLoader = rawRule;
-    } else if (Object.prototype.hasOwnProperty.call(rawRule, 'use')) {
+      return rawRule;
+    }
+    if (Object.prototype.hasOwnProperty.call(rawRule, 'oneOf')) {
+      let result = getTargetRule(rawRule.oneOf)
+      if (result) {
+        return result
+      }
+    }
+    if (Object.prototype.hasOwnProperty.call(rawRule, 'use')) {
       const rawRuleUse = Array.isArray(rawRule.use)
         ? rawRule.use
         : [rawRule.use];
       for (const subLoader of rawRuleUse) {
         if (isSpriteLoader(subLoader)) {
-          spriteLoader = subLoader;
+          return subLoader;
         }
       }
     }
-    if (spriteLoader !== null) break;
   }
+  return null
+}
+
+module.exports = (compiler) => {
+  const rawRules = compiler.options.module.rules;
+  const spriteLoader = getTargetRule(rawRules)
   return (spriteLoader !== null && spriteLoader.options !== undefined) ? spriteLoader.options : {};
 };