webpack 插件html-webpack-plugin的具體使用
本文介紹了webpack 插件html-webpack-plugin的具體使用,分享給大家,具體如下:
插件地址:https://www.npmjs.com/package/html-webpack-plugin
這個(gè)插件用來(lái)簡(jiǎn)化創(chuàng)建服務(wù)于 webpack bundle 的 HTML 文件,尤其是對(duì)于在文件名中包含了 hash 值,而這個(gè)值在每次編譯的時(shí)候都發(fā)生變化的情況。你既可以讓這個(gè)插件來(lái)幫助你自動(dòng)生成 HTML 文件,也可以使用 lodash 模板加載生成的 bundles,或者自己加載這些 bundles。
Installation
使用 npm 安裝這個(gè)插件
$ npm install html-webpack-plugin@2 --save-dev
Basic Usage
這個(gè)插件可以幫助生成 HTML 文件,在 body 元素中,使用 script 來(lái)包含所有你的 webpack bundles,只需要在你的 webpack 配置文件中如下配置:
var HtmlWebpackPlugin = require('html-webpack-plugin')
var webpackConfig = {
entry: 'index.js',
output: {
path: 'dist',
filename: 'index_bundle.js'
},
plugins: [new HtmlWebpackPlugin()]
}
這將會(huì)自動(dòng)在 dist 目錄中生成一個(gè)名為 index.html 的文件,內(nèi)容如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Webpack App</title> </head> <body> <script src="index_bundle.js"></script> </body> </html>
如果你有多個(gè) webpack 入口點(diǎn),它們都會(huì)被包含在生成的 script 元素中。
如果有任何的 CSS 資源包含在 webpack 輸出中(例如,使用 ExtractTextPlugin 提煉出的 css ),這些將會(huì)使用 link 包含在 HTML 頁(yè)面的 head 元素中。
Configuration
可以進(jìn)行一系列的配置,支持如下的配置信息
- title: 用來(lái)生成頁(yè)面的 title 元素
- filename: 輸出的 HTML 文件名,默認(rèn)是 index.html, 也可以直接配置帶有子目錄。
- template: 模板文件路徑,支持加載器,比如 html!./index.html
- inject: true | 'head' | 'body' | false ,注入所有的資源到特定的 template 或者 templateContent 中,如果設(shè)置為 true 或者 body,所有的 javascript 資源將被放置到 body 元素的底部,'head' 將放置到 head 元素中。
- favicon: 添加特定的 favicon 路徑到輸出的 HTML 文件中。
- minify: {} | false , 傳遞 html-minifier 選項(xiàng)給 minify 輸出
- hash: true | false, 如果為 true, 將添加一個(gè)唯一的 webpack 編譯 hash 到所有包含的腳本和 CSS 文件,對(duì)于解除 cache 很有用。
- cache: true | false,如果為 true, 這是默認(rèn)值,僅僅在文件修改之后才會(huì)發(fā)布文件。
- showErrors: true | false, 如果為 true, 這是默認(rèn)值,錯(cuò)誤信息會(huì)寫(xiě)入到 HTML 頁(yè)面中
- chunks: 允許只添加某些塊 (比如,僅僅 unit test 塊)
- chunksSortMode: 允許控制塊在添加到頁(yè)面之前的排序方式,支持的值:'none' | 'default' | {function}-default:'auto'
- excludeChunks: 允許跳過(guò)某些塊,(比如,跳過(guò)單元測(cè)試的塊)
下面的示例演示了如何使用這些配置。
{
entry: 'index.js',
output: {
path: 'dist',
filename: 'index_bundle.js',
hash: true
},
plugins: [
new HtmlWebpackPlugin({
title: 'My App',
filename: 'assets/admin.html'
})
]
}
生成多個(gè) HTML 文件
通過(guò)在配置文件中添加多次這個(gè)插件,來(lái)生成多個(gè) HTML 文件。
{
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'
})
]
}
編寫(xiě)自定義模板
如果默認(rèn)生成的 HTML 文件不適合你的需要看,可以創(chuàng)建自己定義的模板。方便的方式是通過(guò) inject 選項(xiàng),然后傳遞給定制的 HTML 文件。html-webpack-plugin 將會(huì)自動(dòng)注入所有需要的 CSS, js, manifest 和 favicon 文件到標(biāo)記中。
plugins: [
new HtmlWebpackPlugin({
title: 'Custom template',
template: 'my-index.html', // Load a custom template
inject: 'body' // Inject all scripts into the body
})
]
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>
如果你有模板加載器,可以使用它來(lái)解析這個(gè)模板。
module: {
loaders: [
{ test: /\.hbs$/, loader: "handlebars" }
]
},
plugins: [
new HtmlWebpackPlugin({
title: 'Custom template using Handlebars',
template: 'my-index.hbs',
inject: 'body'
})
]
另外,如果你的模式是一個(gè)字符串,可以使用 templateContent 傳遞它。
plugins: [
new HtmlWebpackPlugin({
inject: true,
templateContent: templateContentString
})
]
如果 inject 特性不適合你的需要,你希望完全控制資源放置。 可以直接使用 lodash 語(yǔ)法,使用 default template 作為起點(diǎn)創(chuàng)建自己的模板。
templateContent 選項(xiàng)也可以是一個(gè)函數(shù),以便使用其它語(yǔ)言,比如 jade:
plugins: [
new HtmlWebpackPlugin({
templateContent: function(templateParams, compilation) {
// Return your template content synchronously here
return '..';
}
})
]
或者異步版本
plugins: [
new HtmlWebpackPlugin({
templateContent: function(templateParams, compilation, callback) {
// Return your template content asynchronously here
callback(null, '..');
}
})
]
注意,如果同時(shí)使用 template 和 templateContent ,插件會(huì)拋出錯(cuò)誤。
變量 o 在模板中是在渲染時(shí)傳遞進(jìn)來(lái)的數(shù)據(jù),這個(gè)變量有如下的屬性:
1、htmlWebpackPlugin: 這個(gè)插件的相關(guān)數(shù)據(jù) ◦
htmlWebpackPlugin.files: 資源的塊名,來(lái)自 webpack 的 stats 對(duì)象,包含來(lái)自 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": []
},
}
}
}
如果在 webpack 配置文件中,你配置了 publicPath,將會(huì)反射正確的資源
htmlWebpackPlugin.options: 傳遞給插件的配置。
2、webpack: webpack 的 stats 對(duì)象。
3、webpackConfig: webpack 配置信息。
過(guò)濾塊
可以使用 chunks 來(lái)限定特定的塊。
plugins: [
new HtmlWebpackPlugin({
chunks: ['app']
})
]
還可以使用 excludeChunks 來(lái)排除特定塊。
plugins: [
new HtmlWebpackPlugin({
excludeChunks: ['dev-helper']
})
]
事件
通過(guò)事件允許其它插件來(lái)擴(kuò)展 HTML。
- html-webpack-plugin-before-html-processing
- html-webpack-plugin-after-html-processing
- html-webpack-plugin-after-emit
使用方式:
compilation.plugin('html-webpack-plugin-before-html-processing', function(htmlPluginData, callback) {
htmlPluginData.html += 'The magic footer';
callback();
});
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS學(xué)習(xí)筆記之?dāng)?shù)組去重實(shí)現(xiàn)方法小結(jié)
這篇文章主要介紹了JS學(xué)習(xí)筆記之?dāng)?shù)組去重實(shí)現(xiàn)方法,結(jié)合實(shí)例形式總結(jié)分析了javascript數(shù)組去重的5種常見(jiàn)實(shí)現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
javascript 使用for循環(huán)時(shí)該注意的問(wèn)題-附問(wèn)題總結(jié)
所謂for循環(huán)就是重復(fù)的執(zhí)行一段代碼,for循環(huán)也是希望在創(chuàng)建循環(huán)時(shí)常會(huì)用到的工具,這篇內(nèi)容主要給大家介紹javascript 使用for循環(huán)時(shí)該注意的問(wèn)題-附問(wèn)題總結(jié),需要的朋友可以參考下2015-08-08
BootStrap modal實(shí)現(xiàn)拖拽功能
這篇文章主要為大家詳細(xì)介紹了BootStrap modal實(shí)現(xiàn)拖拽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Bootstrap導(dǎo)航條學(xué)習(xí)使用(一)
這篇文章主要為大家詳細(xì)介紹了Bootstrap導(dǎo)航條的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
JavaScript中偽協(xié)議 javascript:使用探討
javascript:這個(gè)特殊的協(xié)議類(lèi)型聲明了URL的主體是任意的javascript代碼,它由javascript的解釋器運(yùn)行2014-07-07
JavaScript實(shí)現(xiàn)事件總線(Event?Bus)的方法詳解
Event?Bus?事件總線,通常作為多個(gè)模塊間的通信機(jī)制,相當(dāng)于一個(gè)事件管理中心。本文將介紹如何在JavaScript中實(shí)現(xiàn)事件總線,需要的可以參考一下2022-05-05
javascript控制在光標(biāo)位置插入文字適合表情的插入
使用javascript控制在光標(biāo)位置插入文字,在實(shí)現(xiàn)表情的插入時(shí)會(huì)用到的,需要的朋友可以參考下2014-06-06
微信小程序?qū)崿F(xiàn)抖音播放效果的實(shí)例代碼
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)抖音播放效果的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04

