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

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

Open
artemkliaus opened this issue Jan 25, 2023 · 2 comments
Open

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

artemkliaus opened this issue Jan 25, 2023 · 2 comments

Comments

@artemkliaus
Copy link

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:

  • Node.js version: 16
  • webpack version: 5.55.1
  • svg-sprite-loader version: 6.0.11
  • OS type & version: ?

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
Copy link

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

@yunsii
Copy link

yunsii commented Jul 19, 2023

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 : {};
 };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants