如何在 webpack 5 copy-webpack-plugin 中使用展平设置

How to use flatten setting in webpack 5 copy-webpack-plugin

我使用 copy-webpack-plugin 10.2.0 和 webpack 5.65.0。我想将 public/js 文件夹中的 js 文件复制到 dist/js.

plugins: [
    new CopyWebpackPlugin({
        patterns:[
            {
                from:'public/js/*.js',
                to:path.resolve(__dirname, 'dist','js'),
            
            }
        ]
    })
],

但是设置也把路径复制到dist里面,变成了dist/js/public/js。我试着加上flatten:true,但是报错

Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
 - options.patterns[0] has an unknown property 'flatten'. These properties are valid:
   object { from, to?, context?, globOptions?, filter?, transformAll?, toType?, force?, priority?, info?, transform?, noErrorOnMissing? }

那怎么制作呢?

您可以使用 [name][ext] 组件在 to 参数中设置文件名,并简单地省略 path 部分。

plugins: [
    new CopyWebpackPlugin({
        patterns:[
            {
                from:'public/js/*.js',
                to:path.resolve(__dirname, 'dist','js', '[name][ext]'),
            
            }
        ]
    })
],