来源:未知 时间:2020-02-19 18:14 作者:小飞侠 阅读:次
[导读] 这个插件用来简化创建服务于 webpack bundle 的 HTML 文件,尤其是对于在文件名中包含了 hash 值,而这个值在每次编译的时候都发生变化的情况。你既可以让这个插件来帮助你自动生成 H...
| 这个插件用来简化创建服务于 webpack bundle 的 HTML 文件,尤其是对于在文件名中包含了 hash 值,而这个值在每次编译的时候都发生变化的情况。你既可以让这个插件来帮助你自动生成 HTML 文件,也可以使用 lodash 模板加载生成的 bundles,或者自己加载这些 bundles。 Installation $ npm install html-webpack-plugin@2 --save-dev Basic Usage 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 元素中。 
 下面的示例演示了如何使用这些配置。 {
 entry: 'index.js',
 output: {
  path: 'dist',
  filename: 'index_bundle.js',
  hash: true
 },
 plugins: [
  new HtmlWebpackPlugin({
   title: 'My App',
   filename: 'assets/admin.html'
  })
 ]}//欢迎加入全栈开发交流圈一起学习交流:864305860生成多个 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'
  })
 ]}编写自定义模板 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> 如果你有模板加载器,可以使用它来解析这个模板。 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 作为起点创建自己的模板。 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 ,插件会抛出错误。 "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,将会反射正确的资源 plugins: [
 new HtmlWebpackPlugin({
  chunks: ['app']
 })//欢迎加入全栈开发交流圈一起学习交流:864305860]还可以使用 excludeChunks 来排除特定块。 plugins: [
 new HtmlWebpackPlugin({
  excludeChunks: ['dev-helper']
 })//欢迎加入全栈开发交流圈一起学习交流:864305860]事件 
 使用方式: compilation.plugin('html-webpack-plugin-before-html-processing', function(htmlPluginData, callback) {
 htmlPluginData.html += 'The magic footer';
 callback();}); | 
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com