简体   繁体   English

如何使用 Create-React-App 为 sass-resources-loader 设置 Webpack?

[英]How to setup Webpack using Create-React-App for sass-resources-loader?

I'd like to build a project with CSS modules + SASS.我想用 CSS 模块 + SASS 构建一个项目。 I'd like to have some global styles (mixins, variables, functions etc), that will be available in every component.module.scss.我想要一些全局 styles(混合、变量、函数等),它们将在每个 component.module.scss 中可用。 For that I need to import my global styles to every single module style file.为此,我需要将我的全局 styles 导入到每个模块样式文件中。 As I expect to have a lot of components, I was looking for options to do this automatically.由于我希望有很多组件,我一直在寻找自动执行此操作的选项。 I've found sass-resourcess-loader ,might help with that.我找到了sass-resourcess-loader ,可能会有所帮助。

However, as I don't have any experiencies with Webpack , I have troubles to setting it up.但是,由于我对 Webpack 没有任何经验,因此在设置它时遇到了麻烦。

What I did so far:到目前为止我做了什么:

  1. Created my react app with create-react-app使用create-react-app创建了我的反应应用程序
  2. Installed node-sass and sass-resources-loader安装了 node-sasssass-resources-loader
  3. In my project-folder/node_modules/react-scripts/webpack.config.js on line 595 I've added在我的project-folder/node_modules/react-scripts/webpack.config.js的第595行中,我添加了

{ test: /.scss$/, use: [ { loader: "style-loader", }, { loader: "css-loader", }, { loader: "sass-loader", }, { loader: "sass-resources-loader", } ], } { 测试:/.scss$/,使用:[ { loader:“style-loader”,},{ loader:“css-loader”,},{ loader:“sass-loader”,},{ loader:“sass -resources-loader", } ], }

Sorry I cannot get that code snippet styled properly somehow.抱歉,我无法以某种方式正确设置该代码片段的样式。 Also I've tried options field:我也尝试过选项字段:

options: {
 resources: "./src/global.scss"
}

But nothing works, I still get undefined variable in my modular scss file.但是没有任何效果,我的模块化 scss 文件中仍然有未定义的变量。

What am I doing wrong?我究竟做错了什么? Or is there another way to resolve this problem?或者有其他方法可以解决这个问题吗?

Okay so I've find solution with CRACO (Create React App Configuration Override).好的,所以我找到了CRACO (创建 React 应用程序配置覆盖)的解决方案。

1) I've uninstalled sass-reasources-loader as is not needed anymore and reverted the changes in webpack.config.js 1)我已经卸载了不再需要的 sass-reasources-loader 并恢复了 webpack.config.js 中的更改

2) Installed CRACO and craco-sass-resources-loader 2)安装CRACOcraco-sass-resources-loader

3) Created craco.config.js file inside root directory of my project and added this: 3)在我的项目的根目录中创建了 craco.config.js 文件并添加了这个:

const sassResourcesLoader = require("craco-sass-resources-loader"); const sassResourcesLoader = require("craco-sass-resources-loader");

module.exports = {
    mode: "development",
    output: {
        path: __dirname,
    },
    plugins: [
        {
            plugin: sassResourcesLoader,
            options: {
                resources: "./src/global.scss",
            },
        },
    ],
};

4) In package.json I've changed the scripts to: 4)在 package.json 我将脚本更改为:

"start": "craco start",

"build": "craco build",

Now everything works.现在一切正常。 If you need more info, click on the links provided in step 2.如果您需要更多信息,请单击第 2 步中提供的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM