css - 使用 webpack 配置 CSS 模块时出错

标签 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
                    }
                }
            ]
        }

最佳答案

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]'
                }
            }
        }
    ]
}

关于css - 使用 webpack 配置 CSS 模块时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57899750/

相关文章:

javascript - DynamoDB 'createSet' 返回 'constructor' 而不是 'Set'

javascript - CLI 移至单独的包 : webpack-cli

html - Bootstrap 4 中如何使卡头无边框半径?

javascript - 如何更新多张图片的 'src'属性?

javascript - CSS\JS\JQuery : Customize color of clicked (not chosen) option in multiselect box

html - 等高 flex 行不起作用

node.js - 启动失败 'npm' : Ensure that 'npm' is installed and can be found

reactjs - 避免 react-router goBack() 退出应用程序

javascript - React 组件在状态更改时重新渲染

reactjs - React模块解析失败: Unexpected character '@'