Webpack dev服务器初始化问题

3htmauhk  于 8个月前  发布在  Webpack
关注(0)|答案(1)|浏览(106)

当我在cmd中编写npm run dev时,它给了我以下错误:
配置对象无效。已使用与API方案不匹配的配置对象初始化Webpack。
完整跟踪:

- configuration.module.rules[1].use should be one of these:
   non-empty string | function | object { ident?, loader?, options?, query? } | function | [non-empty string | function |
 object { ident?, loader?, options?, query? }]
   -> Modifiers applied to the module when rule is matched
   Details:
    * configuration.module.rules[1].use[0] should be a string.
    * configuration.module.rules[1].use[0] should be an instance of function
    * configuration.module.rules[1].use[0] should be an object.
    * configuration.module.rules[1].use[0] should be one of these:
      non-empty string | function | object { ident?, loader?, options?, query? }
    * configuration.module.rules[1].use[0] should be one of these:
      non-empty string | function | object { ident?, loader?, options?, query? }
      -> An use item

我的webpack配置文件:

const path = require("path");
const extractCssPlugin = require("mini-css-extract-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

const JS_DIR = path.resolve(__dirname, "js");
const BUILD = path.resolve(__dirname, "build");

// Entry point of our JS
entry = {
    main: JS_DIR + "main.js",
};

// Entry point of the output
output = {
    path: BUILD,
    filename: 'js/[name].js'
};

// Rules for the webpack

const rules = [

    // Rule for the JavaScript
    {
        test: /\.js$/,
        include: [ JS_DIR ],
        exclude: /node_modules/,
        use: 'babel-loader' 
    },

    // Rule for the scss
    {
        test: /\.scss$/,
        exclude: /node_modules/,
        use: [
            extractCssPlugin.load,
            "css-loader"
        ] 
    },

    // Rule for the images
    {
        test: /\.(png|jpg|jpeg)$/,
        use: [
            {
                loader: "file-loader",
                options: {
                    name: "[path][name].[ext]",
                    publicPath: ( process.env.NODE_ENV === "production" ) ? "../" : "../../"
                }
            }
        ] 
    }
]

// Defining the plugins 

const plugins = argv => {
    return [
        new CleanWebpackPlugin({
            cleanStaleWebpackAssets: argv.mode === "production"
        }),
        new extractCssPlugin({
            filename: "css/[name].css"
        })
    ]
}

module.exports = ( env, argv ) => {
    return {
        entry: entry,
        output: output,
        module: {
            rules: rules
        },
        plugins: plugins( argv )
    }
}

奇怪的是,开发服务器在我的笔记本电脑上运行得很好,但在我的电脑上却没有,即使我安装了所有必需的软件包。我的代码有什么问题?

z2acfund

z2acfund1#

看看你的webpack配置,我可以看到问题在这一部分:

// Rule for the scss
{
    test: /\.scss$/,
    exclude: /node_modules/,
    use: [
        extractCssPlugin.load,
        "css-loader"
    ] 
},

这里的问题是,您正在使用extractCssPlugin.load作为use数组中的值,这导致了错误。相反,根据mini-css-extract-plugin的官方文档,您应该使用来自extractCssPlugin对象的MiniCssExtractPlugin.loader引用。
以下是修改SCSS规则的方法:

// Rule for the scss
{
    test: /\.scss$/,
    exclude: /node_modules/,
    use: [
        MiniCssExtractPlugin.loader,
        "css-loader"
    ] 
},

确保在webpack配置文件的顶部也有必要的导入:

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

我希望这可以解决SCSS规则的问题,并帮助您在计算机上运行webpack dev服务器。

相关问题