React中配置Sass引入.scss文件无效

在React项目中配置Sass时遇到.scss文件引入无效的问题。尝试多种方法未果,解决方案是修改node_modules下的react-scripts/config文件夹内的设置,添加相应loader。详细步骤见来源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

React中配置Sass引入.scss文件无效

在react中使用sass时,引入.scss文件失效
尝试很多方法没法解决,最终找到解决方法,希望能帮助正在坑里挣扎的筒子~

在node_modules文件夹下,找到react-scripts ==> config文件夹, 在该配置文件夹下找到 webpack.config.dev.jswebpack.config.prod.js
file-loader之前添加style-loadercss-loadersass-loader
代码如下:

          {
            test: /\.scss$/,
            loaders: ['style-loader', 'css-loader', 'sass-loader'],
          },
          // "file" loader makes sure those assets get served by WebpackDevServer.
          // When you `import` an asset, you get its (virtual) filename.
          // In production, they would get copied to the `build` folder.
          // This loader doesn't use a "test" so it will catch all modules
          // that fall through the other loaders.
          {
            // Exclude `js` files to keep "css" loader working as it injects
            // its runtime that would otherwise processed through "file" loader.
            // Also exclude `html` and `json` extensions so they get processed
            // by webpacks internal loaders.
            exclude: [/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/],
            loader: require.resolve('file-loader'),
            options: {
              name: 'static/media/[name].[hash:8].[ext]',
            },
          }

转载于:https://www.cnblogs.com/ygunoil/p/11250395.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值