webpack之html-webpack-plugin简单配置

本文介绍了如何简单配置webpack插件html-webpack-plugin,通过配置,html-webpack-plugin能自动生成index.html文件,并自动引入打包后的app.js。详细配置及更多功能可参考官方文档。

前言:

本文简单配置了webpack的一个插件:html-webpack-plugin;

演示了html-webpack-plugin帮我们做的工作。


正文:

plugin是webpack一个比较重要的概念。

当前webpack.config.js配置:

const path = require('path');  
  
module.exports={  
    entry :{  
        path: path.resolve(__dirname,'src/app.js')  
    },  
    output:{  
        path:path.resolve(__dirname,'dist'), //这里必须是绝对路径  
        filename:'app.hundle.js'  
    },  
    mode:'development'  
} 

现在,我们安装并配置一个插件html-webpack-plugin。

$ npm i -D html-webpack-plugin


plugins 接收的是一个数组,数组的每一项都是一个构造函数的实例。

...
plugins: [
	new HtmlWebpackPlugin({
		template: './src/index.html', //生成的新的html文件所依赖的模板
		filename: 'index.html' //生成的新的html文件的名字
	})
],
...

执行npm run build,打开dist目录下的index.html文件查看源码。

html-webpack-plugin做的工作:

  • 自动生成一个index.html文件
  • 将webapck打包好的app.js文件动态的引入dist目录下新生成的index.html中。


更多html-webpack-plugin配置详见 :https://www.npmjs.com/package/html-webpack-plugin


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值