使用webpack配置CSS模块时出错

时间:2019-09-12 04:29:04

标签: css reactjs webpack

我正在尝试使用webpack配置CSS模块,但出现错误。

我已经检查了关于stackoverflow的其他答案,但是到目前为止,没有一种解决方案对我有用。

我已经按照文档建议添加了加载程序,但仍然显示错误。

这是我的webpack.configuration.js文件。

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');


module.exports = {
    entry: './src/index.js',
    output: {
        path: path.join(__dirname, '/dist'),
        filename: 'index_bundle.js',
        publicPath: '/'
    },
    module: {
        rules: [

            {
                test: /\.css$/,
                loader: 'style-loader!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'
            },

            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            }
        ]
    },
    devServer: {
        historyApiFallback: true,
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        })
    ]
}

我得到的错误是这个。

ERROR in ./src/index.css (./node_modules/css-loader/dist/cjs.js?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./src/index.css)
    Module build failed (from ./node_modules/css-loader/dist/cjs.js):
    ValidationError: Invalid options object. CSS Loader has been initialised using an options object that does not match the API schema.
     - options has an unknown property 'localIdentName'. These properties are valid:
       object { url?, import?, modules?, sourceMap?, importLoaders?, localsConvention?, onlyLocals? }
     - options.importLoaders should be one of these:
       boolean | number
       -> Enables/Disables or setups number of loaders applied before CSS loader (https://github.com/webpack-contrib/css-loader#importloaders).
       Details:
        * options.importLoaders should be a boolean.
        * options.importLoaders should be a number.
        at validate (C:\Users\Arjun\Desktop\manpro\node_modules\css-loader\node_modules\schema-utils\dist\validate.js:49:11)
        at Object.loader (C:\Users\Arjun\Desktop\manpro\node_modules\css-loader\dist\index.js:34:28)
     @ ./src/index.css 1:14-150 20:4-31:5 23:25-161
     @ ./src/index.js

    ERROR in ./src/components/layout/Navbar.css (./node_modules/css-loader/dist/cjs.js?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./src/components/layout/Navbar.css)
    Module build failed (from ./node_modules/css-loader/dist/cjs.js):
    ValidationError: Invalid options object. CSS Loader has been initialised using an options object that does not match the API schema.
     - options has an unknown property 'localIdentName'. These properties are valid

更新:我可以通过以下方式对其进行修复:

{
            test: /\.css$/,
            use: [
                'style-loader',
                {
                    loader: 'css-loader',
                    options: {
                        importLoaders: 1,
                        modules: true
                    }
                }
            ]
        }

4 个答案:

答案 0 :(得分:7)

我遇到了这个问题,并使用以下解决方案解决了该问题

ValidationError: Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.<br>

 - options has an unknown property 'localIndentName'. These properties are valid:
   object { url?, import?, modules?, sourceMap?, importLoaders?, localsConvention?, onlyLocals?, esModule? }
  • 在最新版本的react中,webpack.config.js具有 localIdentName嵌套在模块中。
  • 通过将localIdentName: '[name]__[local]___[hash:base64:5]'替换为以下格式,将modules : true移至模块
{
              test: cssRegex,
              exclude: cssModuleRegex,
              use: getStyleLoaders({
                importLoaders: 1,
                sourceMap: isEnvProduction && shouldUseSourceMap,
              modules: {
                    localIdentName: '[name]__[local]___[hash:base64:5]'
                }
}

这应该可以解决问题

答案 1 :(得分:2)

css-loader选项的语法在3.0.0版中已更改。 localIdentName已移至modules选项下。

我不知道为什么选项importLoaders如果以内联语法指定,则返回错误。但是非内联语法仍在起作用,请尝试使用以下方法替换Webpack配置中的CSS加载程序配置:

{
    test: /\.css$/,
    use: [
        {
            loader: 'style-loader'
        },
        {
            loader: 'css-loader',
            options: {
                importLoaders: 1,
                modules: {
                    localIdentName: '[name]__[local]___[hash:base64:5]'
                }
            }
        }
    ]
}

答案 2 :(得分:0)

test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
    importLoaders: 1,
    // modules:true,
    sourceMap: isEnvProduction && shouldUseSourceMap,
    modules:{
        localIdentName:'[name]__[local]__[hash:base64:5]',
    }
}),

使用此代码对我有用。

答案 3 :(得分:0)

我用这段代码解决了这个问题...`

在 webpack.config.js 文件中编写这段代码

{
test: cssRegex, exclude: cssModuleRegex, use: getStyleLoaders({ importLoaders: 1, sourceMap: isEnvProduction && shouldUseSourceMap, modules: true,
}