2

我的目标是替换一个字符串

<!--configjs-->

<script src="/api/config?bundle-hash"></script>

在我的 index.html 文件中

我正在使用https://github.com/AngularClass/NG6-starter

plugins: [
    // Injects bundles in your index.html instead of wiring all manually.
    // It also adds hash to all injected assets so we don't have problems
    // with cache purging during deployment.
    new HtmlWebpackPlugin({
      template: 'client/index.html',
      inject: 'body',
      hash: true
    })
]
4

1 回答 1

6

你想使用html-webpack-plugin

配置插件,(我认为您想将 inject 设置为 false 以提供更大的控制权)。

// webpack.config.js
plugins: [new HtmlWebpackPlugin(
    {
       inject : false,
       template : 'index.html'
    }
)]

并在您的 html 模板中按名称调用块。

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <script src="<%= htmlWebpackPlugin.files['bundle-hash'].entry %>"></script>
  </head>
  <body>
  </body>
</html>

我不完全确定您的确切需求,但您可以只使用插件默认值并获得您正在寻找的东西。该插件可以将现有的 html 文件作为模板,并在头部注入 CSS,在正文中注入脚本。如果您想要更多控制权,您可以按照我上面的建议进行操作。

于 2016-09-25T15:52:27.903 回答