html-webpack-plugin 的使用

写在前面:
最近一直在研究webpack,按官方文档自己配置了一个小demo后,发现在实际工作中仅仅会这些基础的还不够,于是接着往下研究。

官方文档基础打包

首先安装基本的依赖项

$npm install webpack webpack-dev-server --save-dev

接下来可以看到:

//package.json

"devDependencies": {
    "webpack": "^2.2.1",
    "webpack-dev-server": "^2.4.1"
  }

此刻的项目目录:

demo
   –bundle.js
   –main.js
   –index.html
   –package.json
   –webpack.config.js

其他代码如下:

//webpack.config.js

module.exports = {
    entry:'./main.js',
    output:{
        path:__dirname,
        filename:'bundle.js'
    }
}
<!--index.html-->
<body>
<div> this is from index.html</div>
<script type="text/javascript" src="bundle.js"></script>  //手动引入
</body>
//main.js
document.write('hi all and this is from  main.js')  
$ webpack      //运行打包命令

打包完成,此刻项目目录:

demo
   –bundle.js
   –main.js
   –index.html
   –package.json
   –webpack.config.js

打开 index.html,页面出现:

hi all and this is from main.js
打包成功了。

借助 html-webpack-plugin 插件打包

借助这个插件,他可以创建一个新的 html 文件,类似于 index.html 。打包后自动生成 bundle.js 并被引入到 html 文件中,里面的很多配置项也很多,对于工作中的项目好处是不言而喻的(具体我也还暂时说不好)

这个插件可以帮助生成 HTML 文件,在 body 元素中,使用 script 来包含所有你的 webpack bundles(装载)

用法如下:

先要安装插件 html-webpack-plugin

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

然后package.json中多了个依赖

//package.json

"devDependencies": {
    "html-webpack-plugin": "^2.28.0",
    "webpack": "^2.2.1",
    "webpack-dev-server": "^2.4.1"
  }

此刻的项目目录:(没有什么变化)
demo
   –bundle.js
   –main.js
   –index.html
   –package.json
   –webpack.config.js

其他代码如下

<!--index.html  普通的 index.html  不需要再引入 bundle.js了 而且后面也用不上他了--> 

<body>
<div> this is from index.html</div>
<!-- <script type="text/javascript" src="bundle.js"></script> -->
</body>
// webpack.config.js

var HtmlWebpackPlugin = require('html-webpack-plugin') //引入插件
    module.exports = {
        entry:'./main.js', 
        output:{
            path:'dist', //生成一个叫 dist的新文件夹
            filename:'index_bundle.js'  //打包后的文件名
        },
        plugins:[new HtmlWebpackPlugin()]    //实例化一个插件
    }

运行webpack命令打包,
此刻会发现项目目录下多了个文件夹 dist及两个子文件:index.html 和 index_bundle.js

此刻的项目目录
demo
   |–dist
     index.html
     index_bundle.js
   main.js
   index.html
   package.json
   webpack.config.js
貌似 需要注意的是 这里生成的 index.html 就是一个单纯的 html文件 最普通的 里面什么内容都没有的,当然也可以用一个自定义的html模板来作为生成的文件。下面介绍的配置项 template就可以用来配置。

运行 dist中的index.html文件,页面显示:
hi all and this is from main.js
打包成功~~~

接下来说一说插件的配置项

即:

title: 用来生成页面的 title 元素

filename: 输出的 HTML 文件名,默认是 index.html, 也可以直接配置带有子目录。

template: 模板文件路径,支持加载器,比如 html!./index.html

inject: true | 'head' | 'body' | false  ,注入所有的资源到特定的 template 或者 templateContent 中,如果设置为 true 或者 body,所有的 javascript 资源将被放置到 body 元素的底部,'head' 将放置到 head 元素中。

favicon: 添加特定的 favicon 路径到输出的 HTML 文件中。 

minify: {} | false , 传递 html-minifier 选项给 minify 输出 

hash: true | false, 如果为 true, 将添加一个唯一的 webpack 编译 hash 到所有包含的脚本和 CSS 文件,对于解除 cache 很有用。 

cache: true | false,如果为 true, 这是默认值,仅仅在文件修改之后才会发布文件。 

showErrors: true | false, 如果为 true, 这是默认值,错误信息会写入到 HTML 页面中 

chunks: 允许只添加某些块 (比如,仅仅 unit test 块) 

chunksSortMode: 允许控制块在添加到页面之前的排序方式,支持的值:'none' | 'default' | {function}-default:'auto' 

excludeChunks: 允许跳过某些块,(比如,跳过单元测试的块) 

具体用法如下:

1.自定义html模板

plugins:[new HtmlWebpackPlugin({
    template: 'my-index.html'    //用 自定义的模板来当作后来生成的 html文件模板
})]

2.生成多个html文件,下面会生成两个html文件噢。

entry:'./main.js',
output:{
    path:'dist',
    filename:'index_bundle.js'
},
plugins:[
        new HtmlWebpackPlugin({
                                filename: 'test.html', 
                                //输出的html模板名称 如果不写,默认输出的html名称为 index.html,
                                template: 'my-index.html'  
                                //引用的模板 目录中需要新建一个 my-index.html文件模板
    }),
    new HtmlWebpackPlugin({
    })
]

以上代码运行之后 dist目录
|–dist
   index.html
   index_bundle.js
   test.html

关于配置项还没有写完,也是一边看别人写的一边自己实践的,后面会填完,写的不对欢迎指正。

当项目中的入口文件为多个的时候,也就是说会打包多次的时候,开发环境调试生成文件的时候可以通过配置chunks选项来 决定页面显示的是那个模块。。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值