插件地址:https://www.npmjs.com/package/html-webpack-plugin

这个插件用来简化创建服务于 webpack bundle 的 HTML 文件,尤其是对于在文件名中包含了 hash 值,而这个值在每次编译的时候都发生变化的情况。你既可以让这个插件来帮助你自动生成 HTML 文件,也可以使用 lodash 模板加载生成的 bundles,或者自己加载这些 bundles。

Installation

使用 npm 安装这个插件

$ npm install html-webpack-plugin@2 --save-dev

Basic Usage

这个插件可以帮助生成 HTML 文件,在 body 元素中,使用 script 来包含所有你的 webpack bundles,只需要在你的 webpack 配置文件中如下配置:

var HtmlWebpackPlugin = require('html-webpack-plugin')
var webpackConfig = {
entry: 'index.js',
output: {
path: 'dist',
filename: 'index_bundle.js'
},
plugins: [new HtmlWebpackPlugin()]
}

这将会自动在 dist 目录中生成一个名为 index.html 的文件,内容如下:

<!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

可以进行一系列的配置,支持如下的配置信息

  • 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: 允许跳过某些块,(比如,跳过单元测试的块)

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

{
entry: 'index.js',
output: {
path: 'dist',
filename: 'index_bundle.js',
hash: true
},
plugins: [
new HtmlWebpackPlugin({
title: 'My App',
filename: 'assets/admin.html'
})
]
}

生成多个 HTML 文件

通过在配置文件中添加多次这个插件,来生成多个 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'
})
]
}

编写自定义模板

如果默认生成的 HTML 文件不适合你的需要看,可以创建自己定义的模板。方便的方式是通过 inject 选项,然后传递给定制的 HTML 文件。html-webpack-plugin 将会自动注入所有需要的 CSS, js, manifest 和 favicon 文件到标记中。

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>

如果你有模板加载器,可以使用它来解析这个模板。

module: {
loaders: [
{ test: /\.hbs$/, loader: "handlebars" }
]
},
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 '..';
}
})
]

或者异步版本

plugins: [
new HtmlWebpackPlugin({
templateContent: function(templateParams, compilation, callback) {
// Return your template content asynchronously here
callback(null, '..');
}
})
]

注意,如果同时使用 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": []
      },
      }
      }
      }

      如果在 webpack 配置文件中,你配置了 publicPath,将会反射正确的资源

    • htmlWebpackPlugin.options: 传递给插件的配置。
  • webpack: webpack 的 stats 对象。
  • webpackConfig: webpack 配置信息。

过滤块

可以使用 chunks 来限定特定的块。

plugins: [
new HtmlWebpackPlugin({
chunks: ['app']
})
]

还可以使用 excludeChunks 来排除特定块。

plugins: [
new HtmlWebpackPlugin({
excludeChunks: ['dev-helper']
})
]

事件

通过事件允许其它插件来扩展 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();
});

webpack 插件: html-webpack-plugin的更多相关文章

  1. webpack(9)plugin插件功能的使用

    plugin 插件是 webpack 的支柱功能.webpack 自身也是构建于你在 webpack 配置中用到的相同的插件系统之上! 插件目的在于解决 loader 无法实现的其他事. 常用的插件 ...

  2. 探寻 webpack 插件机制

    webpack 可谓是让人欣喜又让人忧,功能强大但需要一定的学习成本.在探寻 webpack 插件机制前,首先需要了解一件有意思的事情,webpack 插件机制是整个 webpack 工具的骨架,而 ...

  3. 编写webpack 插件

    Webpack插件为第三方开发者释放了Webpack的最大可能性.利用多级回调开发者可以把他们自己的需要的功能引入到Webpack里面来.Build插件比Build loader 更进一步.因为你需要 ...

  4. 从0开始编写webpack插件

    1. 前言 插件(plugins)是webpack中的一等功臣.正是由于有了诸多插件的存在,才使得webpack无所不能.在webpack源码中也是使用了大量的内部插件,插件要是用的好,可以让你的工作 ...

  5. 80行代码教你写一个Webpack插件并发布到npm

    1. 前言 最近在学习 Webpack 相关的原理,以前只知道 Webpack 的配置方法,但并不知道其内部流程,经过一轮的学习,感觉获益良多,为了巩固学习的内容,我决定尝试自己动手写一个插件. 这个 ...

  6. 【前端必会】不知道webpack插件? webpack插件源码分析BannerPlugin

    背景 不知道webpack插件是怎么回事,除了官方的文档外,还有一个很直观的方式,就是看源码. 看源码是一个挖宝的行动,也是一次冒险,我们可以找一些代码量不是很大的源码 比如webpack插件,我们就 ...

  7. webpack 插件拾趣 (1) —— webpack-dev-server

    结束了一季的忙碌,我这封笔已久的博客也终究该从春困的咒印中复苏,想来写些实用易读的作为开篇,自然是最好不过. 新开个 webpack 插件/工具介绍的文章系列,约莫每周更新一篇篇幅适中的文章聊以共勉, ...

  8. webpack之loader和plugin简介

    webpack之loader和plugin简介 webpack入门和实战(二):全面理解和运用loader和plugins webpack入门(四)——webpack loader 和plugin w ...

  9. 【vue】webpack插件svg-sprite-loader---实现自己的icon组件

    引言:最近开始写vue的项目,借鉴了一下vue-element-admin源码,针对vue有一个关于icon图标的处理,最近也找了很多关于vue的icon处理的解决方案,大部分都是按照之前小程序的方式 ...

  10. webpack插件html-webpack-plugin

    1.插件安装 npm install html-webpack-plugin --save-dev 2.插件使用 webpack.config.js配置文件为: var htmlWebpackPlug ...

随机推荐

  1. Java里面的转义字符

    转义字符是指,用一些普通字符的组合来代替一些特殊字符,由于其组合改变了原来字符表示的含义,因此称为“转义”. 常见的转义字符: \n 回车(\u000a) \t 水平制表符(\u0009) \b 空格 ...

  2. CPU和GPU性能对比

    计算20000次10000点的fft,分别使用CPU和GPU,得 the running time of cpu is : 2.3696s the running time of gpu is : 0 ...

  3. POJ 2540 Hotter Colder --半平面交

    题意: 一个(0,0)到(10,10)的矩形,目标点不定,从(0,0)开始走,如果走到新一点是"Hotter",那么意思是离目标点近了,如果是"Colder“,那么就是远 ...

  4. TopCoder SRM 639 Div.2 500 AliceGameEasy --乱搞

    题意: 一个游戏有n轮,有A和B比赛,谁在第 i 轮得胜,就获得 i 分,给出x,y,问A得x分,B得y分有没有可能,如果有,输出A最少赢的盘数. 解法: 这题是我傻逼了,处理上各种不优越,要使n*( ...

  5. POJ1523 SPF[无向图割点]

    SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8139   Accepted: 3723 Description C ...

  6. 微软极品工具箱-Sysinternals Suite

    工具包由来 Sysinternals Suite是微软发布的一套非常强大的免费工具程序集,一共包括74个windows工具.Sysinternals是Winternals公司提供的免费工具,Winte ...

  7. code blocks 如何实现一键代码格式化

    问题:code blocks 如何实现一键代码格式化 解答:直接右键,选择format use ASstyle

  8. C#几个经常用到的字符串截取

    C#几个经常用到的字符串截取 一. 1.取字符串的前i个字符 (1)string str1=str.Substring(0,i); (2)string str1=str.Remove(i,str.Le ...

  9. Java设计模式之-----策略模式

    首先,我们来看下策略模式的概念.一般的解释如下:     策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化.(原文:The St ...

  10. codevs 1227 方格取数 2

    Description 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每达到一格,把该格子的数取出来, ...