Webpack.config如何将index.html复制到dist文件夹

标签 webpack

我正在尝试自动将 Assets 放入/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']
  }
}

我还想在运行 webpack 时将/lib 旁边的目录中的 main.html 包含到/dist 文件夹中。我怎样才能做到这一点?

2017 年更新 1_____________

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

最佳答案

选项 1

在您的 index.js 文件(即 webpack 条目)中,通过 file-loader 添加一个 require 到您的 index.html插件,例如:

require('file-loader?name=[name].[ext]!../index.html');

使用 webpack 构建项目后,index.html 将位于输出文件夹中。

选项 2

使用html-webpack-plugin完全避免有一个index.html。只需让 webpack 为您生成文件即可。

在这种情况下,如果您想保留自己的 index.html 文件作为模板,您可以使用以下配置:

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

module.exports = {
  // ...
  plugins: [
    new HtmlWebpackPlugin({
      template: 'src/index.html'
    })
  ]
}

请参阅docs了解更多信息。

关于Webpack.config如何将index.html复制到dist文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32155154/

相关文章:

html - 如何在使用 webpack 时在 css 中加载字体?

webpack - 如何将源目录结构保留在输出目录中?

javascript - 如何从 html-webpack-plugin 生成的 index.html 中排除 bundle.css?

javascript - 尝试运行我的 "webpack --config ./webpack.config.js"时出现错误

javascript - 热模块更换已启用但不起作用 http ://localhost:3000/__webpack_hmr

javascript - 如何将 Jquery/Bootstrap 文件包含到 Webpack/angular2/typescript 堆栈中

Webpack 5 开发服务器不会在文件更改时重新加载

angularjs - 通过 grunt webpack 运行的 Webpack 不会出错或完成,或做任何事情

angular - webpack 升级后找不到命名空间 NodeJS

gulp和webpack-dev-server,报错TypeError : this. sockWrite is not a function