插件 html-webpack-plugin 的详解

最近在学习webpack,接触到的第一个插件就是html-webpack-plugin,那么今天就来详解一下它的用法吧。

先来上一个例子:

var htmlWebpackPlugin = require('html-webpack-plugin')

const path = require('path')
module.exports = {
    entry: './src/script/main.js',
    output: {
        filename: 'js/bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    plugins: [
        new htmlWebpackPlugin({
            filename: 'index.html',
            template: 'index.html',
            inject: 'head'
        })
    ]
}

配置属性

title

生成html文件的标题

filename

就是html文件的文件名,默认是index.html

template

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

举例子:

$ npm install jade-loader --save-dev

// webpack.config.js
...
loaders: {
    ...
    {
        test: /\.jade$/,
        loader: 'jade'
    }
}
plugins: [
    new HtmlWebpackPlugin({
        ...
        jade: 'path/to/yourfile.jade'
    })
]

如果你设置的 title 和 filename于模板中发生了冲突,那么以你的title 和 filename 的配置值为准。

inject

inject有四个值: true body head false

true 默认值,script标签位于html文件的 body 底部
body script标签位于html文件的 body 底部
head script标签位于html文件的 head中
false 不插入生成的js文件,这个几乎不会用到的

favicon

给你生成的html文件生成一个 favicon ,值是一个路径

plugins: [
    new HtmlWebpackPlugin({
        ...
        favicon: 'path/to/my_favicon.ico'
    }) 

然后再生成的html中就有了一个 link 标签

<link rel="shortcut icon" href="example.ico">

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 // 移除属性的引号
        }
    })
]

cache

默认是true的,表示内容变化的时候生成一个新的文件。

showErrors

当webpack报错的时候,会把错误信息包裹再一个pre中,默认是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']
    })
]

那么编译后:

<script type=text/javascript src="index.js"></script>
<script type=text/javascript src="main.js"></script>

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

excludeChunks

排除掉一些js

excludeChunks: ['devor.js']
// 等价于上面的

xhtml

一个布尔值,默认值是 false ,如果为 true ,则以兼容 xhtml 的模式引用文件。

chunksSortMode

script的顺序,默认四个选项: none auto dependency {function}

‘dependency’ 不用说,按照不同文件的依赖关系来排序。

‘auto’ 默认值,插件的内置的排序方式,具体顺序…

‘none’ 无序?

{function} 提供一个函数?

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 Webpack 构建项目时,我们可以使用 `html-webpack-plugin` 件来自动生成 HTML 文件,并且将打包后的 JS/CSS 文件注入到 HTML 文件中。其中,`html-webpack-plugin` 也支持一些配置项来满足不同的需求,其中就包括设置生成的 HTML 文件中的版本号。 在生成的 HTML 文件中,我们可以通过以下方式给所有的 JS/CSS 文件添加版本号: ```html <link rel="stylesheet" href="styles.css?v=<%= htmlWebpackPlugin.options.version %>"> <script src="bundle.js?v=<%= htmlWebpackPlugin.options.version %>"></script> ``` 其中,`htmlWebpackPlugin.options.version` 是 `html-webpack-plugin` 的配置项,我们需要将其在 webpack 的配置文件中进行设置。具体操作如下: 1. 安装 `html-webpack-plugin` 件: ```bash npm install --save-dev html-webpack-plugin ``` 2. 在 webpack 的配置文件中引入 `html-webpack-plugin` 件: ```javascript const HtmlWebpackPlugin = require('html-webpack-plugin'); ``` 3. 在 `plugins` 数组中添加 `html-webpack-plugin` 的实例,并设置 `version` 配置项: ```javascript plugins: [ new HtmlWebpackPlugin({ template: './src/index.html', version: '1.0.0' }) ] ``` 其中,`template` 指定了模板文件,`version` 指定了版本号。 4. 在模板文件中使用 `<%= htmlWebpackPlugin.options.version %>` 来引用版本号: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>My App</title> <link rel="stylesheet" href="styles.css?v=<%= htmlWebpackPlugin.options.version %>"> </head> <body> <div id="app"></div> <script src="bundle.js?v=<%= htmlWebpackPlugin.options.version %>"></script> </body> </html> ``` 这样,就可以在生成的 HTML 文件中给所有的 JS/CSS 文件添加版本号了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值