11

我想.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: [
      // 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';

我收到一个错误:

未捕获的错误:找不到模块“./assets/app.scss”

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

4

6 回答 6

9

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

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

所以添加

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

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

于 2016-12-15T02:24:54.403 回答
9

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

 {
        test: /\.scss$/,
        include: paths.appSrc,
        loaders: [require.resolve('style-loader'), require.resolve('css-loader'), require.resolve('sass-loader')]
  },
于 2017-07-05T13:09:41.027 回答
8

好的,我找到了解决方案 - 从此更改:

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

对此:

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

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

于 2016-11-08T13:05:52.580 回答
1

您可能必须使用

include: paths.appSrc,

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

{
    test: /\.scss$/,
    include: paths.appSrc,
    loaders: ['style', 'css', 'sass']
},
于 2016-11-22T20:54:20.910 回答
1

根据最新配置 [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
      ]
  },
于 2018-08-16T02:02:00.887 回答
0

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

{
     test: /\.scss$/,
     use: [
         "style-loader", // creates style nodes from JS strings
         "css-loader", // translates CSS into CommonJS
         "sass-loader" // compiles Sass to CSS
    ]
}
于 2018-06-04T14:20:20.420 回答