Webpack-使用CopyWebpackPlugin将文件从源文件复制到公共文件

Webpack-使用CopyWebpackPlugin将文件从源文件复制到公共文件,webpack,copy,Webpack,Copy,我有一个应用程序,其中我正在使用Webpack。在这个应用程序中,我需要将一些静态.html文件从我的源目录的不同目录复制到公共目录的相同层次结构中。为了做到这一点,我使用了。此时,我的webpack.config.js文件如下所示: /source /dir-1 /child-a index.html page.html /child-b index.html contact.html /dir-2 index.h

我有一个应用程序,其中我正在使用Webpack。在这个应用程序中,我需要将一些静态.html文件从我的
目录的不同目录复制到
公共
目录的相同层次结构中。为了做到这一点,我使用了。此时,我的webpack.config.js文件如下所示:

/source
  /dir-1
    /child-a
      index.html
      page.html
    /child-b
      index.html
      contact.html
  /dir-2
    index.html
webpack.config.js

const path = require('path');
const projectRoot = path.resolve(__dirname, '../');

const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  entry: {
    app: './source/index.html.js',
  },
  output: {
    path: path.resolve(__dirname, 'public'),
    filename: '[name].package.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        loaders: ['style-loader','css-loader']        
      },
    ]
  },
  plugins: [
    new CopyWebpackPlugin(
      [ { from: './source/**/*.html', to: './public', force:true } ],
      { copyUnmodified: true }
    )
  ]
};
当我从命令行运行
webpack
时,一切都按照我的要求运行。HTML文件(包括其目录)已成功复制到
public
目录中。但是,复制时,会包含源目录名。例如,如果我的目录结构如下所示:

/source
  /dir-1
    /child-a
      index.html
      page.html
    /child-b
      index.html
      contact.html
  /dir-2
    index.html
我希望
CopyWebpackPlugin
的结果输出到以下内容:

期望值:

/public
  /dir-1
    /child-a
      index.html
      page.html
    /child-b
      index.html
      contact.html
  /dir-2
    index.html
/public
  /source
    /dir-1
      /child-a
        index.html
        page.html
      /child-b
        index.html
        contact.html
    /dir-2
      index.html
然而,我实际上得到的是:

实际值:

/public
  /dir-1
    /child-a
      index.html
      page.html
    /child-b
      index.html
      contact.html
  /dir-2
    index.html
/public
  /source
    /dir-1
      /child-a
        index.html
        page.html
      /child-b
        index.html
        contact.html
    /dir-2
      index.html
注意
目录是如何包含在复制目标中的。我不明白为什么会包括这个。更重要的是,我需要删除它。如何从目标路径中删除
目录?

您可以使用param


我使用了name变量

模式:[
{从:“src/*.svg”到:'[name].svg'},
]

这对我不起作用,看看文档,在选项对象中应该有“上下文”选项(其中copy unmodified)并且适用于所有patrens@Tonio,这两种情况下都可以使用。我也喜欢数组元素。从文档中:
一个确定如何解释From路径的路径
一个确定如何解释From路径的路径,为所有模式共享
很好,这就是解决方案。是我很笨还是CopyWebpackPlugin文档不是很清楚?@migli文档不是很好。我想他们会很高兴合并文档改进PR。我发现问题很清楚。它声明ao:
如果from是一个glob,那么不管上下文选项如何,结果都将是from选项中指定的结构。结合
来确定找到的资源将从中复制到目标文件夹的结构,使用上下文选项。
这让我找到了与@StanislavMayorov相同的答案。