6、插件使用

下面演示2个插件的使用。官方文档。

1、安装插件。

npm install --save-dev html-webpack-plugin
npm install --save-dev clean-webpack-plugin

2、在webpack.config.js中配置插件。

使用导入插件

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

173310_eeNK_2601303.png

plugins属性中配置插件。这里表示

plugins: [
    new HtmlWebpackPlugin({template : './index.html'}),
    new Cleanwebpackplugin(['dist'])
]

 

3、创建index.html

根目录下创建index.html

171341_6lVd_2601303.png

index.html的内容如下:注意里面没有任何的js。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
</head>
<body>
<div id="box1"></div>

<div id="box2"></div>

</body>
</html>

 

4、使用webpack命令看效果。

运行webpack命令。可以看到,index.html被复制到目标目录下面,并且index.html中引入了我们打包好的js文件。

这就是插件HtmlWebpackPlugin的作用。new HtmlWebpackPlugin({trmplete : './index.html'})表示使用当前目录下的index.html作为模板。复制到打包的目标目录,并且把打包好的js导入。

clean-webpack-plugin 作用是删除指定目录下的所有资源。可以删除多个目录。

配置好 new Cleanwebpackplugin(['dist'])  当运行webpack命令时,就会删除根目录下的dist文件夹。

 

转载于:https://my.oschina.net/kunBlog/blog/1634113

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值