无法解析styleName

时间:2019-12-20 23:31:31

标签: webpack sass babel-plugin-react-css-modules

我在我的应用程序中使用scss模块,但是每次更改styleName时,在热重载期间都会收到错误Could not resolve styleName。如果styleName没有相应的样式,我也会收到错误消息。我必须每次重新启动服务器才能重新编译。我的配置有什么问题吗?

webpack.dev.js

const webpack = require('webpack')
const BrowserSyncPlugin = require('browser-sync-webpack-plugin')

module.exports = require('./webpack.base')({
    mode: 'development',
    devServer: {
        hot: true,
        port: 3000,
        historyApiFallback: true
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new BrowserSyncPlugin(
            { proxy: 'http://localhost:3000/', open: false },
            { reload: false }
        )
    ],
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        plugins: [
                            [
                                'react-css-modules',
                                {
                                    "filetypes": {
                                        ".scss": { "syntax": "postcss-scss" }
                                    },
                                    "generateScopedName": '[name]_[local]_[hash:base64:5]'
                                }
                            ],
                        ],
                    },
                },
                resolve: {
                        extensions: ['.js', '.jsx']
                }
            },
            {
                test: /\.(sa|sc)ss$/,
                use: [
                    { loader: 'style-loader' },
                    {
                        loader: 'css-loader',
                        options: {
                            modules: true,
                            sourceMap: false,
                            localIdentName: '[name]_[local]_[hash:base64:5]'
                        }
                    },
                    {
                        loader: 'sass-loader'
                    }
                ]
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader', 'postcss-loader']
            },
        ]
    }
})

2 个答案:

答案 0 :(得分:0)

您是否已安装postcss-scss并放置类似此插件选项的配置?

['react-css-modules', {
        filetypes: {
           '.scss': {
              syntax: 'postcss-scss'
           }
         }
      }
]

答案 1 :(得分:0)

您必须安装postcss-import-sync2并将其作为第一个插件导入,如下所示(配置 babel-plugin-react-css-modules )。

也许更新的 postcss-import 在这里也可以使用,但是它没有同步模式,并且在README.md中也使用了 postcss-import-sync2

>

如果您想支持导入sass部分_my-partial.scss,例如@import'./my-partial';,则需要自定义解析功能。

const path = require('path');

// Later configuring plugin...
{
  filetypes: {
    ".scss": {
      syntax: "postcss-scss",
      plugins: [
        [
          "postcss-import-sync2",
          {
            resolve: function(id, basedir, importOptions) {
              let nextId = id;

              if (id.substr(0, 2) === "./") {
                nextId = id.replace("./", "");
              }

              if (nextId[0] !== "_") {
                nextId = `_${nextId}`;
              }

              if (nextId.indexOf(".scss") === -1) {
                nextId = `${nextId}.scss`;
              }

              return path.resolve(basedir, nextId);
            }
          }
        ],
        [
          "postcss-nested",
          {
            bubble: ["@include"],
            preserveEmpty: true
          }
        ]
      ]
    }
  },
  generateScopedName: "[path]___[name]__[local]___[hash:base64:5]",
  webpackHotModuleReloading: true,
  exclude: "node_modules",
  handleMissingStyleName: "warn"
}

styles.scss

@import './example';