深刻解析webpack 插件html-webpack-plugin

这个插件用来简化建立服务于 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 的文件,内容以下:vue

<!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 能够进行一系列的配置,支持以下的配置信息java

  • title: 用来生成页面的 title 元素
  • filename: 输出的 HTML 文件名,默认是 index.html, 也能够直接配置带有子目录。
  • template: 模板文件路径,支持加载器,好比 html!./index.html
  • inject: true | 'head' | 'body' | false ,注入全部的资源到特定的 template 或者 templateContent 中,* * 若是设置为 true 或者 body,全部的 javascript 资源将被放置到 body 元素的底部,'head' 将放置到 head 元素中。
  • favicon: 添加特定的 favicon 路径到输出的 HTML 文件中。
  • minify: {} | false , 传递 html-minifier 选项给 minify 输出
  • hash: true | false, 若是为 true, 将添加一个惟一的 webpack 编译 hash 到全部包含的脚本和 CSS 文件,对于解除 cache 颇有用。
  • cache: true | false,若是为 true, 这是默认值,仅仅在文件修改以后才会发布文件。
  • showErrors: true | false, 若是为 true, 这是默认值,错误信息会写入到 HTML 页面中
  • chunks: 容许只添加某些块 (好比,仅仅 unit test 块)
  • chunksSortMode: 容许控制块在添加到页面以前的排序方式,支持的值:'none' | 'default' | {function}-default:'auto'
  • excludeChunks: 容许跳过某些块,(好比,跳过单元测试的块)

下面的示例演示了如何使用这些配置。node

{
 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 文件。webpack

{
 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 文件到标记中。web

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>
复制代码

若是你有模板加载器,可使用它来解析这个模板。npm

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。

  • 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();
});
复制代码

结语

感谢您的观看,若有不足之处,欢迎批评指正。

本次给你们推荐一个免费的学习群,里面归纳移动应用网站开发,css,html,webpack,vue node angular以及面试资源等。 对web开发技术感兴趣的同窗,欢迎加入Q群:864305860,无论你是小白仍是大牛我都欢迎,还有大牛整理的一套高效率学习路线和教程与您免费分享,同时天天更新视频资料。 最后,祝你们早日学有所成,拿到满意offer,快速升职加薪,走上人生巅峰。

相关文章
相关标签/搜索