React提供的脚手架creact-react-app创建的工程文件不像vue那种暴露出webpack来,所以添加依赖需要拐个弯。
为了配置sass需要按以下步骤进行:
一、安装sass-loader和node-sass依赖
npm install sass-loader node-sass --save-dev
二、打开react的webpack配置
node_modules/react-scripts/config/webpack.config.js
找到module下的rules,oneof数组中,然后找到最后一个配置,修改成如下的样子
1 { 2 loader: require.resolve('file-loader'), 3 // Exclude `js` files to keep "css" loader working as it injects 4 // its runtime that would otherwise be processed through "file" loader. 5 // Also exclude `html` and `json` extensions so they get processed 6 // by webpacks internal loaders. 7 exclude: [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/], 8 options: { 9 name: 'static/media/[name].[hash:8].[ext]', 10 }, 11 }, 12 // ** STOP ** Are you adding a new loader? 13 // Make sure to add the new loader(s) before the "file" loader. 14 { 15 test:/\.scss$/, 16 loaders: ['style-loader', 'css-loader', 'sass-loader'] 17 }

三、这样就可以愉快地使用scss文件了
