我试图自动化资产进入/dist。我有以下config.js:

module.exports = {
  context: __dirname + "/lib",
  entry: {
    main: [
      "./baa.ts"
    ]
  },
  output: {
    path: __dirname + "/dist",
    filename: "foo.js"
  },
  devtool: "source-map",
  module: {
    loaders: [
      {
        test: /\.ts$/,
        loader: 'awesome-typescript-loader'
      },
      { test: /\.css$/, loader: "style-loader!css-loader" }
    ]
  },
  resolve: {
    // you can now require('file') instead of require('file.js')
    extensions: ['', '.js', '.json']
  }
}

我还想包括main.html从目录,坐在旁边/lib,到/dist文件夹时运行webpack。我该怎么做呢?

更新1 2017_____________

现在我最喜欢的方法是使用带有模板文件的html-webpack-plugin。也感谢公认的答案!这种方法的优点是,索引文件也将有cachbusted js链接添加开箱即用!


当前回答

这在Windows上运行良好:

NPM install——save-dev copyfiles 在包中。我有一个复制任务:“复制”:“copyfiles -u 1 ./app/index.html ./deploy”

这将我的index.html从app文件夹移动到deploy文件夹。

其他回答

要复制一个已经存在的index.html文件到dist目录,你可以简单地使用HtmlWebpackPlugin,指定源index.html作为模板。

const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  // ...
  plugins: [    
    new HtmlWebpackPlugin({
      template: './path/to/index.html',
    })
  ],
  // ...
};

创建的dist/index.html文件将基本上与您的源文件相同,不同的是。js文件等捆绑资源被webpack注入了<script>标记。缩小和进一步的选项可以配置,并记录在github上。

我还发现把我的index.html文件放在dist/目录下,并将<script src='main.js'></script>添加到index.html中,以包括我绑定的webpack文件,这很简单,也很通用。如果在webpack的conf文件中没有指定其他的话,Main.js似乎是我们的bundle的默认输出名称。我想这不是一个好的和长期的解决方案,但我希望它能帮助理解webpack是如何工作的。

您可以直接将索引添加到条目配置中,并使用文件加载器来加载它

module.exports = {

  entry: [
    __dirname + "/index.html",
    .. other js files here
  ],

  module: {
    rules: [
      {
        test: /\.html/, 
        loader: 'file-loader?name=[name].[ext]', 
      },
      .. other loaders
    ]
  }

}

这在Windows上运行良好:

NPM install——save-dev copyfiles 在包中。我有一个复制任务:“复制”:“copyfiles -u 1 ./app/index.html ./deploy”

这将我的index.html从app文件夹移动到deploy文件夹。

你可以使用CopyWebpackPlugin。它是这样工作的:

module.exports = {
  plugins: [
    new CopyWebpackPlugin([{
      from: './*.html'
    }])
  ]
}