我想在使用create-react-app创建的应用中添加.scss支持。
我已经通过运行npm eject命令弹出了应用,并安装了必要的依赖项:npm install sass-loader node-sass --save-dev
在config / webpack.config.dev.js文件中,我将以下代码片段添加到loaders中:
{
test: /\.scss$/,
include: paths.appSrc,
loaders: ["style", "css", "scss"]
},
所以现在加载器数组的开头看起来像这样:
loaders: [
// Process JS with Babel.
{
test: /\.(js|jsx)$/,
include: paths.appSrc,
loader: 'babel',
query: require('./babel.dev')
},
// "postcss" loader applies autoprefixer to our CSS.
// "css" loader resolves paths in CSS and adds assets as dependencies.
// "style" loader turns CSS into JS modules that inject <style> tags.
// In production, we use a plugin to extract that CSS to a file, but
// in development "style" loader enables hot editing of CSS.
{
test: /\.css$/,
loader: 'style!css!postcss'
},
// LOAD & COMPILE SCSS
{
test: /\.scss$/,
include: paths.appSrc,
loaders: ["style", "css", "scss"]
},
现在在我的jsx文件中,当我尝试导入scss文件时:import './assets/app.scss';
我遇到了错误:
Uncaught Error: Cannot find module "./assets/app.scss"
因此,我的配置可能有问题,因为我无法加载.scss
文件。如何调整配置以在create-react-app
中引入.scss
文件?
{ test: /\.scss$/, include: path.appSrc, loaders: [ 'style-loader', 'css-loader', 'sass-loader'] },
对我有用。 - t3__rry