webpack-demo002,使用html-webpack-plugin

在demo001的基础上增加 html-webpack-plugin

https://blog.csdn.net/qq_26264237/article/details/131280125

安装html-webpack-plugin

npm i -D html-webpack-plugin

新增src目录内容

修改/src/index.html
主要去掉脚本引入部分,由html-webpack-plugin完成脚本引入

<script src="../dist/main.js"></script>
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test</title>
</head>

<body>

</body>

</html>

修改webpack配置文件

新增插件方面配置

const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    // 入口文件 默认值是 ./src/index.js
    entry: "./src/index.js",
    // 打包后的出口文件 默认值是 ./dist/main.js
    output: {
        // 输出的文件名称
        filename: 'main.js',
    },
    // 使用开发模式打包 development, production 或 none
    mode: "development",
    plugins: [
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: './src/index.html',
            // inject:是否在template文件中插入打包的js文件
            inject: true,
            title: 'webpack is good',
            chunks: ['main']
        })
    ]
}

修改运行脚本

package.json文件变化,多出了html-webpack-plugin依赖

{
  "name": "webpack-demo002-html-webpack-plugin",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --mode development",
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "html-webpack-plugin": "^5.5.3",
    "webpack": "^5.87.0",
    "webpack-cli": "^5.1.4"
  }
}

运行

执行命令进行打包,生成/dist/main.js文件
npm run dev

右键 /src/index.html open with live server(需要安装插件)
http://127.0.0.1:5501/src/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qq_26264237

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值