【问题标题】:copy-webpack-plugin error: Invalid host defined optionscopy-webpack-plugin 错误:主机定义的选项无效
【发布时间】:2021-11-23 12:09:58
【问题描述】:

使用 copy-webpack-plugin 后构建失败。 webpack 和插件版本已经过兼容性测试(webpack5 cwp10) 我的想法不多了:( 有没有人碰到过熟悉的东西? 这可能是跨模块兼容性问题吗?

进一步提供错误日志和配置代码 提前致谢

错误是

 HookWebpackError: Invalid host defined options
    at makeWebpackError (/home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/HookWebpackError.js:49:9)
    at /home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/Compilation.js:2495:12
    at eval (eval at create (/home/egeo/Source/coreon-chrome-plugin/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:38:1)
    at /home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/Compilation.js:457:26
    at /home/egeo/Source/coreon-chrome-plugin/node_modules/copy-webpack-plugin/dist/index.js:485:13
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
-- inner error --
TypeError: Invalid host defined options
    at /home/egeo/Source/coreon-chrome-plugin/node_modules/copy-webpack-plugin/dist/index.js:481:13
    at fn (/home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/Compilation.js:456:9)
    at Hook.eval [as callAsync] (eval at create (/home/egeo/Source/coreon-chrome-plugin/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:36:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/home/egeo/Source/coreon-chrome-plugin/node_modules/tapable/lib/Hook.js:18:14)
    at cont (/home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/Compilation.js:2492:34)
    at /home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/Compilation.js:2538:10
    at /home/egeo/Source/coreon-chrome-plugin/node_modules/neo-async/async.js:2830:7
    at Object.each (/home/egeo/Source/coreon-chrome-plugin/node_modules/neo-async/async.js:2850:39)
    at Compilation.createChunkAssets (/home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/Compilation.js:3769:12)
    at /home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/Compilation.js:2533:14
caused by plugins in Compilation.hooks.processAssets
TypeError: Invalid host defined options
    at /home/egeo/Source/coreon-chrome-plugin/node_modules/copy-webpack-plugin/dist/index.js:481:13
    at fn (/home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/Compilation.js:456:9)
    at Hook.eval [as callAsync] (eval at create (/home/egeo/Source/coreon-chrome-plugin/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:36:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/home/egeo/Source/coreon-chrome-plugin/node_modules/tapable/lib/Hook.js:18:14)
    at cont (/home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/Compilation.js:2492:34)
    at /home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/Compilation.js:2538:10
    at /home/egeo/Source/coreon-chrome-plugin/node_modules/neo-async/async.js:2830:7
    at Object.each (/home/egeo/Source/coreon-chrome-plugin/node_modules/neo-async/async.js:2850:39)
    at Compilation.createChunkAssets (/home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/Compilation.js:3769:12)
    at /home/egeo/Source/coreon-chrome-plugin/node_modules/webpack/lib/Compilation.js:2533:14

使用的配置是(webpack.config.js)

const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const path = require('path');

module.exports = (env) => [
  {
    mode: 'production',
    entry: './js/background.js',
    output: {
      path: path.resolve(__dirname, 'dist'),
      publicPath: '/',
      filename: 'background.js',
    },
  },
  {
    mode: 'production',
    entry: {
      './js/login': './js/login.js',
      './js/search': './js/search.js',
      './js/options': './js/options.js',
    },
    output: {
      path: path.resolve(__dirname, 'dist'),
      filename: '[name].js',
      publicPath: '/',
    },
    module: {
      rules: [
        {
          test: /\.(scss|css)$/,
          use: [MiniCssExtractPlugin.loader, "css-loader",
            {
              loader: "sass-loader",
            }
          ]
        },
        {
          test: /\.(png|svg|jpg|jpeg|gif)$/i,
          type: 'asset/resource',
          generator: {
            filename: 'styles/images/[hash][ext][query]'
          }
        },
        {
          test: /\.(woff|woff2|eot|ttf|otf)$/i,
          type: 'asset/resource',
          generator: {
            filename: 'styles/[hash][ext][query]'
          }
        },
      ]
    },
    plugins: [
      new CopyPlugin({
        patterns: [
          {
            from: '/extension-icons/*'
          }
        ],
      }),
      new webpack.DefinePlugin({
        BUILD_FOR: JSON.stringify(env.custom)
      }),
      new MiniCssExtractPlugin({
        filename: ({ chunk }) => `${chunk.name.replace('/js/', '/styles/')}.css`,
      }),
    ],
  }
];

【问题讨论】:

  • 由于插件的使用版本(10.0.0)只有5天,我继续使用旧版本。即 9.0.1 我很欣慰地说,现在就像一个魅力(@webpack 5)
  • FWIW 在版本 10 中遇到了同样的问题,降级到 ^9.1.0 也对我有用
  • 这里也一样,也不得不降级到 9。我想你应该如何使用 v10 定义路径发生了一些变化,但我想不出正确的方法。仅供参考,这是我目前的定义:{ from: path.resolve(__dirname, 'public'), to: path.resolve(__dirname, 'dist/public') }

标签: webpack copy-webpack-plugin


【解决方案1】:

插件版本 10 开始使用动态导入的 ES 模块。这仍然在各种工具中得到了一些低于标准的支持。例如。 Yarn 似乎不能很好地处理它(但基于 npm 的设置也可能会损坏)

github 上的相关错误报告:

官方回应:问题出在其他工具上,而不是插件本身。

看起来最好的方法是降级到 v9,正如上面一些人已经提到的那样。

【讨论】:

  • 非常感谢您的帮助。拯救了我的一天。请注意:在Invalid host defined options 之前,我只收到/c/Program Files/nodejs/npm: line 44: 2184 Segmentation fault "$NODE_EXE" "$NPM_CLI_JS" "$@" 错误行。我不知道为什么我开始出现正确的错误(没有触及任何东西,包括 npm 缓存和node_modules),但这可能对其他开发人员有用。
猜你喜欢
  • 2022-11-11
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
  • 2017-11-29
  • 2016-10-20
  • 2019-04-18
相关资源
最近更新 更多