【问题标题】:Webpack 5.47.1 : Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schemaWebpack 5.47.1:配置对象无效。 Webpack 已使用与 API 架构不匹配的配置对象进行初始化
【发布时间】:2021-07-30 08:23:20
【问题描述】:

我正在使用 React 应用程序。下面是我的 5.47.1 的 Webpack 配置文件。

const path = require('path');
const webpack = require('webpack');

process.noDeprecation = true;

module.exports = (options) => ({
  entry: options.entry,
  output: Object.assign({ // Compile into js/build.js
    path: path.resolve(process.cwd(), 'build'),
    publicPath: '/',
  }, options.output), // Merge with env dependent settings
  module: {
    mode: 'development',
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: options.babelQuery,
        },
      },
      {
        test: /\.scss$/,
        exclude: /node_modules/,
        use: ['style-loader', 'css-loader', 'sass-loader'],
      },
      {
        test: /\.css$/,
        exclude: /node_modules/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.css$/,
        include: /node_modules/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.(eot|svg|otf|ttf|woff|woff2)$/,
        use: 'file-loader',
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        use: [
          'file-loader',
          {
            loader: 'image-webpack-loader',
            options: {
              progressive: true,
              optimizationLevel: 7,
              interlaced: false,
              pngquant: {
                quality: '65-90',
                speed: 4,
              },
            },
          },
        ],
      },
      {
        test: /\.html$/,
        use: 'html-loader',
      },
      {
        test: /\.json$/,
        use: 'json-loader',
      },
      {
        test: /\.(mp4|webm)$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 10000,
          },
        },
      },
    ],
  },
  plugins: options.plugins.concat([
    new webpack.ProvidePlugin({
      // make fetch available
      fetch: 'exports-loader?self.fetch!whatwg-fetch',
    }),

    new webpack.DefinePlugin({
      'process.env.NODE_ENV' : JSON.stringify('development'),
    }),
    // new webpack.NamedModulesPlugin(),
  ]),
  resolve: {
    modules: ['app', 'node_modules'],
    extensions: [
      '.js',
      '.jsx',
      '.react.js',
    ],
    mainFields: [
      'browser',
      'jsnext:main',
      'main',
    ],
  },
  devtool: options.devtool,
  target: 'web', // Make web variables accessible to webpack, e.g. window
  performance: options.performance || {},
});

我已更新mode。无法找出这里的配置问题

【问题讨论】:

    标签: javascript reactjs webpack webpack-dev-server


    【解决方案1】:

    mode 应该在根对象中,而不是在模块对象中:

    module.exports = (options) => ({
        mode: 'development',
        entry: options.entry,
        output: Object.assign({ // Compile into js/build.js
            path: path.resolve(process.cwd(), 'build'),
            publicPath: '/',
        }, options.output), // Merge with env dependent settings
        module: {
    
            rules: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: {
                        loader: 'babel-loader',
                        options: options.babelQuery,
                    },
                },
                {
                    test: /\.scss$/,
                    exclude: /node_modules/,
                    use: ['style-loader', 'css-loader', 'sass-loader'],
                },
                {
                    test: /\.css$/,
                    exclude: /node_modules/,
                    use: ['style-loader', 'css-loader'],
                },
                {
                    test: /\.css$/,
                    include: /node_modules/,
                    use: ['style-loader', 'css-loader'],
                },
                {
                    test: /\.(eot|svg|otf|ttf|woff|woff2)$/,
                    use: 'file-loader',
                },
                {
                    test: /\.(jpe?g|png|gif|svg)$/i,
                    use: [
                        'file-loader',
                        {
                            loader: 'image-webpack-loader',
                            options: {
                                progressive: true,
                                optimizationLevel: 7,
                                interlaced: false,
                                pngquant: {
                                    quality: '65-90',
                                    speed: 4,
                                },
                            },
                        },
                    ],
                },
                {
                    test: /\.html$/,
                    use: 'html-loader',
                },
                {
                    test: /\.json$/,
                    use: 'json-loader',
                },
                {
                    test: /\.(mp4|webm)$/,
                    use: {
                        loader: 'url-loader',
                        options: {
                            limit: 10000,
                        },
                    },
                },
            ],
        },
        plugins: options.plugins.concat([
            new webpack.ProvidePlugin({
                // make fetch available
                fetch: 'exports-loader?self.fetch!whatwg-fetch',
            }),
    
            new webpack.DefinePlugin({
                'process.env.NODE_ENV': JSON.stringify('development'),
            }),
            // new webpack.NamedModulesPlugin(),
        ]),
        resolve: {
            modules: ['app', 'node_modules'],
            extensions: [
                '.js',
                '.jsx',
                '.react.js',
            ],
            mainFields: [
                'browser',
                'jsnext:main',
                'main',
            ],
        },
        devtool: options.devtool,
        target: 'web', // Make web variables accessible to webpack, e.g. window
        performance: options.performance || {},
    });
    
    
    

    【讨论】:

    • 感谢此问题已解决。但是当我进行上述更改时,我收到TypeError: compiler.plugin is not a function 错误。你知道吗?
    • 当你使用 webpack 4 插件和 webpack 5 时有时会出现这个错误,也许尝试更新一些东西。您也可以尝试删除插件以找出导致问题的插件...
    猜你喜欢
    • 1970-01-01
    • 2017-06-22
    • 2017-10-21
    • 2018-09-03
    • 2019-10-12
    • 2021-09-03
    • 2022-07-30
    • 2018-08-16
    相关资源
    最近更新 更多