博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
html-webpack-plugin
阅读量:7252 次
发布时间:2019-06-29

本文共 1926 字,大约阅读时间需要 6 分钟。

开始之前,先看如下事例:

const HtmlWebpackPlugin = require('html-webpack-plugin')const path = require('path');module.exports = { entry: {  //  index: './src/index.js',  //  utils: './src/utils.js'   index: path.resolve(__dirname, 'src/index.js'),   utils: path.resolve(__dirname, 'src/utils.js') }, output: {   //path: __dirname + '/dist',   //filename: '[name]-[chunkhash].js'   path: path.resolve(__dirname, 'dist'),   filename: '[name]_bundle.js'  //配合下面配置中的hash选项使用,可不加[chunkhash] }, plugins: [   new HtmlWebpackPlugin({    title: 'My Test',    filename: 'test.html',    hash: true,    chunks: ['utils']   }),   new HtmlWebpackPlugin({    title: 'My App',    filename: 'index.html',    hash: true,    chunks: ['index']   }) ]}复制代码

常用的配置属性

template

指定你生成的文件所依赖哪一个html文件模板,模板类型可以是handlebars、html、jade、ejs等。但是要注意的是,如果想使用自定义的模板文件的时候,你需要安装对应的loader哦。

$ npm install handlebars-loader --save-dev复制代码
//webpack.config.jsrules: [      {        test: /\.hbs$/,        loader: 'handlebars-loader'      },      ...]plugins: [    new HtmlWebpackPlugin({        ...        template: './src/pages/index/index.hbs',    })]复制代码

minify

使用minify会对生成的html文件进行压缩。默认是false。html-webpack-plugin内部集成了 html-minifier,因此,还可以对minify进行配置:(注意,虽然minify支持BooleanObject,但是不能直接这样写:minify: true , 这样会报错 ERROR in TypeError: Cannot use 'in' operator to search for 'html5' in true , 使用时候必须给定一个 { } 对象 )

...plugins: [    new HtmlWebpackPlugin({        ...        minify: {            removeAttributeQuotes: true // 移除属性的引号        }    })]复制代码

chunks

chunks主要用于多入口文件,当你有多个入口文件,那就回编译后生成多个打包后的文件,那么chunks 就能选择你要使用那些js文件

entry: {    index: path.resolve(__dirname, './src/index.js'),    devor: path.resolve(__dirname, './src/devor.js'),    main: path.resolve(__dirname, './src/main.js')}plugins: [    new httpWebpackPlugin({        chunks: ['index','main']    })]复制代码

那么编译后:

复制代码
  • 如果你没有设置chunks选项,那么默认是全部显示

excludeChunks

排除掉一些js

//与上面写法等价excludeChunks: ['devor.js']复制代码

更多配置信息可参考:

转载地址:http://olebm.baihongyu.com/

你可能感兴趣的文章
利用SQL语句查询数据库中所有表
查看>>
虚拟机中的锁优化简介(适应性自旋/锁粗化/锁削除/轻量级锁/偏向锁)
查看>>
Golang的交互模式进阶-读取用户的输入
查看>>
mycat中间件--linux安装mycat1.6版本
查看>>
MySQL的用户管理
查看>>
linux配置java环境变量(详细)
查看>>
HTML CSS + DIV实现整体布局
查看>>
【开源项目】电视盒子好用又强大的APP: TVRemoteIME
查看>>
Java开发报表——Grid++Report 报表设计器
查看>>
三、加载公共语言运行时
查看>>
ABP框架系列之三十五:(MVC-Controllers-MVC控制器)
查看>>
SpringBoot(二)Web整合开发
查看>>
finalkeyword对JVM类载入器的影响
查看>>
洛谷P4051 [JSOI2007]字符加密
查看>>
用 label 控制 Pod 的位置 - 每天5分钟玩转 Docker 容器技术(128)
查看>>
人生苦短,我用Python(目录)
查看>>
NYOJ 589 糖果
查看>>
xorm
查看>>
算法知识目录整理
查看>>
Zookeeper原理架构
查看>>