webpack 的插件 html-webpack-plugin

webpack 的插件 html-webpack-plugin

  文件名的动态生成,防止了缓存的问题,但是也带来了另外一个问题。那就是每次构建结果的文件名都是不同的。我们必须手动修改 html 中的引用才可以继续使用。这是不可以接受的。那我们就要引入 webpack 插件 html-webpack-plugin 来处理这个问题了。由它来更新 html 中对 js 文件的引用。

  1. 安装 html-webpack-plugin

    npm i html-webpack-plugin -D

  2. 更新 webpack.config.js

    更新 webpack 的配置文件 webpack.config.js 增加 html-webpack-plugin。

    // webpack.config.js
    const HtmlWebpackPlugin = require("html-webpack-plugin");
    
    module.exports = {
      mode: "development",
      entry: {
        main: "./src/index.js"
      },
      output: {
        filename: "[name].[hash].js"
      },
      plugins: [
        new HtmlWebpackPlugin({
          filename: "index.html",
          title: "webpack"
        })
      ]
    };
    
  3. 使用插件
    此时执行 npx webpack。可以看到输出文件夹 dist 中,会生成一个 index.html 文件,会将构建生成的 js 文件插入到其中。此时 html 使用的是 html-webpack-plugin 自带默认的模板。

    <!-- dist文件夹中自动生成的index.html -->
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <title>webpack</title>
      </head>
      <body>
        <script
          type="text/javascript"
          src="main.9ac3aa7e8d8de2ef34b1.js"
        ></script>
      </body>
    </html>
    
  4. 使用 template

    根目录下创建 index.html 作为 template。将 index.html 修改为:

    <!-- 根目录下作为template的 index.html -->
    <!DOCTYPE html>
    <html lang="zh-CN">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title><%= htmlWebpackPlugin.options.title %></title>
      </head>
    
      <body>
        <div id="app"></div>
      </body>
    </html>
    

    修改 webpack 中 html-webpack-plugin 的配置,增加 template。

    // webpack.config.js
    ......
      plugins: [
        new HtmlWebpackPlugin({
          filename: "index.html",
          title: "webpack",
          template: './index.html'
        })
      ],
    ......
    

运行 webpack 查看 dist 文件夹中生成的结果。

  在模板中我们可以使用<%= htmlWebpackPlugin.options.title %>来访问 html-webpack-plugin 配置中的参数,除了默认一些属性之外,可以自定义属性也是可以在模板中通过这种方法访问的,但是尽量不要和官方已有的冲突就好。

  多页面使用 html-webpack-plugin 的时候,每个页面都需要使用 new HtmlWebpackPlugin()

html-webpack-plugin 一些其他配置

html-webpack-plugin 相关文档

html-webpack-plugin 配置相关文档(github)

  1. inject
    注入选项,即指定 js 文件插入到 html 中的位置。有四个选项值 true, body, head, false.

    value描述
    true默认值 html 文件的 body 底部
    body同 true
    headhead 标签内
    false不插入生成的 js 文件
  2. favicon

    给 html 文件指定 favicon 文件所在的路径

  3. minify
    minify 的作用是对 html 文件进行压缩,minify 的属性值是一个压缩选项或者 false 。默认值为 false, 不对生成的 html 文件进行压缩。

    webpack mode 为 production 的时候默认值为 true。也可以使用对象对其进行具体配置。

  4. hash (Boolean)

    如果为 true 会在插入的文件名末尾增加一串 hash 值。

  5. cache

    内容变化的时候重新生成目标文件。

  6. showErrors

    把 webpack 的报错插入到当前的 HTML 中,展示到当前的页面。

  7. chunks

    指定要引入的块,值为 webpack 配置 entry 属性中的模块名。默认全部引入。

  8. excludeChunks

    排除一些块

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值