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

use*_*554 6 copy webpack

我有一个应用程序,我正在使用Webpack.在这个应用程序中,我需要将目录中各种目录中的一些静态.html文件复制到目录中source的同一层次结构中public.为了做到这一点,我正在使用CopyWebpackPlugin.这时,我的webpack.config.js文件如下所示:

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 }
    )
  ]
};
Run Code Online (Sandbox Code Playgroud)

当我从命令行运行时webpack,一切都按我的意愿运行.HTML文件已成功复制到public目录中,包括其目录.但是,复制时,将包含源目录名称.例如,如果我的目录结构是这样的:

/source
  /dir-1
    /child-a
      index.html
      page.html
    /child-b
      index.html
      contact.html
  /dir-2
    index.html
Run Code Online (Sandbox Code Playgroud)

我期待结果CopyWebpackPlugin输出到以下内容:

预期:

/public
  /dir-1
    /child-a
      index.html
      page.html
    /child-b
      index.html
      contact.html
  /dir-2
    index.html
Run Code Online (Sandbox Code Playgroud)

但是,我实际得到的是:

实际:

/public
  /source
    /dir-1
      /child-a
        index.html
        page.html
      /child-b
        index.html
        contact.html
    /dir-2
      index.html
Run Code Online (Sandbox Code Playgroud)

请注意source目录是如何包含在复制目标中的.我不明白为什么包括这个.更重要的是,我需要删除它.如何source从目标中的路径中删除目录?

Sta*_*rov 13

您可以使用上下文参数.

new CopyWebpackPlugin(
    [{
        context: './source/',
        from: '**/*.html',
        to: './public',
        force: true
    }], {
        copyUnmodified: true
    }
)
Run Code Online (Sandbox Code Playgroud)

  • 太好了,这就是解决方案。我是愚蠢的还是 CopyWebpackPlugin 文档不太清楚? (3认同)