NX WebPack CSS-Loader问题

发布于 2025-02-09 04:10:06 字数 1281 浏览 2 评论 0原文

const { merge } = require('webpack-merge');
const sass = require('sass');

module.exports = (config, context) => {
    return merge(config, {
        module: {
            rules: [
                {
                    test: /\.sass$/i,
                    use: [
                        'style-loader',
                        {
                            loader: 'css-loader',
                            options: {
                                modules: {
                                    localIdentName: '[local]__[hash:base64:5]'
                                }
                            }
                        },
                        {
                            loader: 'sass-loader',
                            options: {
                                implementation: sass
                            }
                        }
                    ]
                }
            ]
        }
    });
};

在其中有React Project的新鲜生成的NX工作区。 试图制作自定义的webpack.config以通过SASS文件生成哈希,

但是有很多错误掉落了。

我也尝试配置巴别尔。添加了插件babel-plugin-reactct-css模型,这无济于事

const { merge } = require('webpack-merge');
const sass = require('sass');

module.exports = (config, context) => {
    return merge(config, {
        module: {
            rules: [
                {
                    test: /\.sass$/i,
                    use: [
                        'style-loader',
                        {
                            loader: 'css-loader',
                            options: {
                                modules: {
                                    localIdentName: '[local]__[hash:base64:5]'
                                }
                            }
                        },
                        {
                            loader: 'sass-loader',
                            options: {
                                implementation: sass
                            }
                        }
                    ]
                }
            ]
        }
    });
};

enter image description here

freshly generated nx workspace with react project in it.
trying to make a custom webpack.config to generate the hashes via sass files

but there is a lot of errors falling down.

Also i tried configure the babel. Added the plugin babel-plugin-react-css-modules and that's doesn't help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

养猫人 2025-02-16 04:10:06

NX具有自己的样式加载程序,它是您合并的配置的一部分。

要解决此问题,您必须从NX Config删除该加载程序。我通过循环浏览所有module.rules并忽略我不想要的任何一个。因此,要解决您的问题,您将这样做:

module.exports = (config, context) => {
  const conf = merge(config, {
    module: {
      rules: [
        {
          test: /\.sass$/i,
          use: [
            'style-loader',
            {
              loader: 'css-loader',
              options: {
                modules: {
                  localIdentName: '[local]__[hash:base64:5]'
                }
              }
            },
            {
              loader: 'sass-loader',
              options: {
                implementation: sass
              }
            }
          ]
        }
      ]
    }
  });

  // Remove unwanted NX rules
  const mods = [];
  conf.module.rules.forEach((rule) => {
    if (rule.test != '/\\.css$|\\.scss$|\\.sass$|\\.less$|\\.styl$/') {
      mods.push(rule);
    }
  });
  conf.module.rules = mods;

  return conf;
};

这花了我很长时间来弄清楚。根据您的NX版本,您的CSS规则可能有所不同。只需console.log(rule.test)检查您要忽略的规则是什么。

NX has it's own style-loader which is part of the config which You are merging.

To resolve this, You will have to remove that loader from NX config. I did it manually by looping through all of the module.rules and ignoring whichever I don't want. So to fix Your issue, You would do it this way:

module.exports = (config, context) => {
  const conf = merge(config, {
    module: {
      rules: [
        {
          test: /\.sass$/i,
          use: [
            'style-loader',
            {
              loader: 'css-loader',
              options: {
                modules: {
                  localIdentName: '[local]__[hash:base64:5]'
                }
              }
            },
            {
              loader: 'sass-loader',
              options: {
                implementation: sass
              }
            }
          ]
        }
      ]
    }
  });

  // Remove unwanted NX rules
  const mods = [];
  conf.module.rules.forEach((rule) => {
    if (rule.test != '/\\.css$|\\.scss$|\\.sass$|\\.less$|\\.styl$/') {
      mods.push(rule);
    }
  });
  conf.module.rules = mods;

  return conf;
};

This took me a long time to figure out. Your CSS rule might be different, depending on Your NX version. Just console.log(rule.test) to check what is the rule which You want to ignore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文