簡體   English   中英

HtmlWebpackPlugin無法解析模板中的自定義變量

[英]Custom variables in template are not resolved by HtmlWebpackPlugin

我需要在模板中添加隨機變量。 這是使用HtmlWebpackPlugin彈出的Angular項目。 我的HtmlWebpackPlugin配置如下所示:

new HtmlWebpackPlugin({
  "filename": "./index.html",
  "hash": false,
  "inject": false,
  "compile": true,
  "favicon": false,
  "minify": false,
  "template": "./src/index.html",
  "cache": true,
  "showErrors": true,
  "chunks": "all",
  "excludeChunks": [],
  "myHash": Math.random().toString(36).slice(2),
  "xhtml": true,
  "chunksSortMode": function sort(left, right) {
    let leftIndex = entryPoints.indexOf(left.names[0]);
    let rightindex = entryPoints.indexOf(right.names[0]);
    if (leftIndex > rightindex) {
      return 1;
    }
    else if (leftIndex < rightindex) {
      return -1;
    }
    else {
      return 0;
    }
  }
})

myHash是我需要添加到模板的變量。

由於某些原因,這不起作用:

<p><%= htmlWebpackPlugin.options.myHash %></p>

生成的HTML看起來像一樣: <p><%= htmlWebpackPlugin.options.myHash %></p>

當我在webpack 1中遇到非常相似的問題(推斷<%=htmlWebpackPlugin.files.webpackManifest%> )時,我通過從html-loader中排除html-file來解決了這個問題:

{
                test: /\.html$/,
                use: [{
                    loader: 'html-loader',
                    options: minifyHtmlOpts
                }],
                // excluding, so ejs loader will be used for these pages
                exclude: /index.html/
            }

它仍然適用於webpack 3。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM