记录 webpack 打包html+js

导入 html-webpack-plugin 包,打包html+JS

配置 webpack-config.js

文件目录
在这里插入图片描述

const { resolve } = require("path");
// 应用打包html插件
const htmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  entry: {
    vendor: ["./src/js/jquery.js", "./src/js/common.js"],
    index: "./src/js/index.js",
    cart: "./src/js/cart.js",
  },

  //  entry为对象时,配合以下output 会成成多个打包后的文件
  output: {
    filename: "[name].js",
    path: resolve(__dirname, "build"),
  },
  
  plugins: [
    // 默认会创建一个空的,目的就是自动引入打包的资源(js/css)
    new htmlWebpackPlugin({
      // 复制'./src/index.html'文件,并自动打包引入打包输出的所有资源(js/css)
      template: "./src/index.html",
      // 默认时index.html名称,通过filename设置输出文件名称
      filename: "index.html",
      chunks: ["index", "vendor"], //写入指定打包的文件名
      minify: {
        // 移除打包后的空格
        collapseWhitespace: true,
        // 移除注释
        removeComments: true,
      },
    }),

    new htmlWebpackPlugin({
      // 复制'./src/index.html'文件,并自动打包引入打包输出的所有资源(js/css)
      template: "./src/cart.html",
      // 默认时index.html名称,通过filename设置输出文件名称
      filename: "cart.html",
      chunks: ["cart", "vendor"],
    }),
  ],
  // production 打包后的代码用于上线,不会有多余的注释,性能达到对号
  // development 打包后的代码会有注释,本地开发调试方便。
    mode: "production", //生产模式 production,开发模式development
}


  • 生成后的文件 vendor.js内引入common.js jquery.js
    在这里插入图片描述
  • index.js 打包 index.js
  • 在这里插入图片描述
  • cart.js 打包 cart.js
  • 在这里插入图片描述
  • index.html 打包vendor.js, index.js
  • 在这里插入图片描述
    -cart.html 打包 cart.js ,vendor.js
    -在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码の搬运工

记录学习,记录认知,记录。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值