這個插件用來簡化建立服務於 webpack bundle 的 HTML 文件,尤爲是對於在文件名中包含了 hash 值,而這個值在每次編譯的時候都發生變化的狀況。你既可讓這個插件來幫助你自動生成 HTML 文件,也可使用 lodash 模板加載生成的 bundles,或者本身加載這些 bundles。javascript
Installation 使用 npm 安裝這個插件css
$ npm install html-webpack-plugin@2 --save-dev
Basic Usage 這個插件能夠幫助生成 HTML 文件,在 body 元素中,使用 script 來包含全部你的 webpack bundles,只須要在你的 webpack 配置文件中以下配置:html
var HtmlWebpackPlugin = require('html-webpack-plugin') var webpackConfig = { entry: 'index.js', output: { path: 'dist', filename: 'index_bundle.js' }, plugins: [new HtmlWebpackPlugin()] }
這將會自動在 dist 目錄中生成一個名爲 index.html 的文件,內容以下:java
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Webpack App</title> </head> <body> <script src="index_bundle.js"></script> </body> </html>
若是你有多個 webpack 入口點,它們都會被包含在生成的 script 元素中。 若是有任何的 CSS 資源包含在 webpack 輸出中(例如,使用 ExtractTextPlugin 提煉出的 css ),這些將會使用 link 包含在 HTML 頁面的 head 元素中。 Configuration 能夠進行一系列的配置,支持以下的配置信息webpack
下面的示例演示瞭如何使用這些配置。web
{ entry: 'index.js', output: { path: 'dist', filename: 'index_bundle.js', hash: true }, plugins: [ new HtmlWebpackPlugin({ title: 'My App', filename: 'assets/admin.html' }) ] }//歡迎加入全棧開發交流圈一塊兒學習交流:864305860
生成多個 HTML 文件 經過在配置文件中添加屢次這個插件,來生成多個 HTML 文件。npm
{ entry: 'index.js', output: { path: 'dist', filename: 'index_bundle.js' }, plugins: [ new HtmlWebpackPlugin(), // Generates default index.html new HtmlWebpackPlugin({ // Also generate a test.html filename: 'test.html', template: 'src/assets/test.html' }) ] }
編寫自定義模板 若是默認生成的 HTML 文件不適合你的須要看,能夠建立本身定義的模板。方便的方式是經過 inject 選項,而後傳遞給定製的 HTML 文件。html-webpack-plugin 將會自動注入全部須要的 CSS, js, manifest 和 favicon 文件到標記中。app
plugins: [ new HtmlWebpackPlugin({ title: 'Custom template', template: 'my-index.html', // Load a custom template inject: 'body' // Inject all scripts into the body })//歡迎加入全棧開發交流圈一塊兒學習交流:864305860 ]
my-index.html 文件異步
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"/> <title><%= htmlWebpackPlugin.options.title %></title> </head> <body> </body> </html>
若是你有模板加載器,可使用它來解析這個模板。async
module: { loaders: [ { test: /\.hbs$/, loader: "handlebars" } ]//歡迎加入全棧開發交流圈一塊兒學習交流:864305860 }, plugins: [ new HtmlWebpackPlugin({ title: 'Custom template using Handlebars', template: 'my-index.hbs', inject: 'body' }) ]
另外,若是你的模式是一個字符串,可使用 templateContent 傳遞它。
plugins: [ new HtmlWebpackPlugin({ inject: true, templateContent: templateContentString }) ]
若是 inject 特性不適合你的須要,你但願徹底控制資源放置。 能夠直接使用 lodash 語法,使用 default template 做爲起點建立本身的模板。 templateContent 選項也能夠是一個函數,以便使用其它語言,好比 jade:
plugins: [ new HtmlWebpackPlugin({ templateContent: function(templateParams, compilation) { // Return your template content synchronously here return '..'; }//歡迎加入全棧開發交流圈一塊兒學習交流:864305860 }) ]
或者異步版本
plugins: [ new HtmlWebpackPlugin({ templateContent: function(templateParams, compilation, callback) { // Return your template content asynchronously here callback(null, '..'); }//歡迎加入全棧開發交流圈一塊兒學習交流:864305860 }) ]
注意,若是同時使用 template 和 templateContent ,插件會拋出錯誤。 變量 o 在模板中是在渲染時傳遞進來的數據,這個變量有以下的屬性: 一、htmlWebpackPlugin: 這個插件的相關數據 ◦ htmlWebpackPlugin.files: 資源的塊名,來自 webpack 的 stats 對象,包含來自 entry 的從 entry point name 到 bundle 文件名映射。
"htmlWebpackPlugin": { "files": { "css": [ "main.css" ], "js": [ "assets/head_bundle.js", "assets/main_bundle.js"], "chunks": { "head": { "entry": "assets/head_bundle.js", "css": [ "main.css" ] }, "main": { "entry": "assets/main_bundle.js", "css": [] },//歡迎加入全棧開發交流圈一塊兒學習交流:864305860 } } }
若是在 webpack 配置文件中,你配置了 publicPath,將會反射正確的資源 htmlWebpackPlugin.options: 傳遞給插件的配置。 二、webpack: webpack 的 stats 對象。 三、webpackConfig: webpack 配置信息。 過濾塊 可使用 chunks 來限定特定的塊。
plugins: [ new HtmlWebpackPlugin({ chunks: ['app'] })//歡迎加入全棧開發交流圈一塊兒學習交流:864305860 ]
還可使用 excludeChunks 來排除特定塊。
plugins: [ new HtmlWebpackPlugin({ excludeChunks: ['dev-helper'] })//歡迎加入全棧開發交流圈一塊兒學習交流:864305860 ]
事件 經過事件容許其它插件來擴展 HTML。
使用方式:
compilation.plugin('html-webpack-plugin-before-html-processing', function(htmlPluginData, callback) { htmlPluginData.html += 'The magic footer'; callback(); });
結語
感謝您的觀看,若有不足之處,歡迎批評指正。