添加SASS以弹出create-react-app配置

时间:2016-11-08 12:16:50

标签: reactjs sass webpack create-react-app

我想在我的应用中添加.scss支持,该支持是使用create-react-app创建的。

我确实弹出了npm run eject并安装了必要的依赖项:npm install sass-loader node-sass --save-dev

config/webpack.config.dev.js内,我在加载器中添加了这个代码段:

{
   test: /\.scss$/,
   include: paths.appSrc,
   loaders: ["style", "css", "scss"]
},

所以loaders数组的开头现在看起来像这样:

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"]
      },

当我尝试导入scss文件时,现在在我的jsx中:

import './assets/app.scss';

我收到错误:

Uncaught Error: Cannot find module "./assets/app.scss"

所以我的配置一定是错的,因为我无法加载.scss个文件。如何调整配置以在弹出的.scss中加载create-react-app个文件?

6 个答案:

答案 0 :(得分:9)

检查第一个加载器,首先获取所有文件,并排除其他加载器文件

loaders: [
        {
            exclude: [
                /\.html$/,
                /\.(js|jsx)$/,
                /\.css$/,
                /\.json$/,
                /\.svg$/
            ],
            loader: 'url',
            query: {
                limit: 10000,
                name: 'static/media/[name].[hash:8].[ext]'
            }
        },

所以添加

/\.sass$/,
/\.scss$/,

排除,似乎解决了我遇到的同样问题:D

答案 1 :(得分:9)

当尝试将sass集成到空反应项目时,我使用了这个article。在那里提出的解决方案不起作用并且收到与主题中类似的错误。 在我的情况下,用style-loader替换require.resolve('style-loader')帮助:

 {
        test: /\.scss$/,
        include: paths.appSrc,
        loaders: [require.resolve('style-loader'), require.resolve('css-loader'), require.resolve('sass-loader')]
  },

答案 2 :(得分:8)

好的,我找到了解决方案 - 改变了这个:

{
    test: /\.scss$/,
    include: paths.appSrc,
    loaders: ["style", "css", "scss"]
},

到此:

{
    test: /\.scss$/,
    loaders: ['style', 'css', 'sass']
},

现在我的.scss文件正在加载!!

答案 3 :(得分:1)

您可能必须使用

include: paths.appSrc,

启用webpack热重载的选项。所以你的配置片段看起来像

{
    test: /\.scss$/,
    include: paths.appSrc,
    loaders: ['style', 'css', 'sass']
},

答案 4 :(得分:0)

检查此加载程序设置以获取最新版本。

{
     test: /\.scss$/,
     use: [
         "style-loader", // creates style nodes from JS strings
         "css-loader", // translates CSS into CommonJS
         "sass-loader" // compiles Sass to CSS
    ]
}

答案 5 :(得分:0)

根据最新配置[CRA]

:: webpack.config.dev.js ::

{
    test: /\.css$/,
    use: [
      require.resolve('style-loader'),
      {
        loader: require.resolve('css-loader'),
        options: {
          importLoaders: 1,
        },
      },
      {
        loader: require.resolve('postcss-loader'),
        options: {
          // Necessary for external CSS imports to work
          // https://github.com/facebookincubator/create-react-app/issues/2677
          ident: 'postcss',
          plugins: () => [
            require('postcss-flexbugs-fixes'),
            autoprefixer({
              browsers: [
                '>1%',
                'last 4 versions',
                'Firefox ESR',
                'not ie < 9', // React doesn't support IE8 anyway
              ],
              flexbox: 'no-2009',
            }),
          ],
        },
      },
    ],
  },
  {
    test: /\.scss$/,
    loaders: [
        require.resolve("style-loader"), // creates style nodes from JS strings
        require.resolve("css-loader"), // transl ates CSS into CommonJS
        require.resolve("sass-loader") // compiles Sass to CSS
      ]
  },