在react中使用sass時,引入.scss文件失效
嘗試很多方法沒法解決按价,最終找到解決方法餐茵,希望能幫助正在坑里掙扎的筒子~
在node_modules文件夾下,找到react-scripts ==> config文件夾辆苔, 在該配置文件夾下找到 webpack.config.dev.js
和webpack.config.prod.js
算灸。
在file-loader
之前添加style-loader
, css-loader
, sass-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]',
},
}