webpack的使用(插件HtmlWebpackPlugin)

与前两篇想结合

1.安装HtmlWebpackPlugin插件

2.在webpack.config.js中配置插件(此处实例是多页面应用)

//多页面应用
plugins: [new HtmlWebpackPlugin({
    title: "页面一", //指定页面名称
    filename: "index.html",//指定生成的页面名称 默认生成在output指定的路径下
    template: "src/html/index.html", //以当前指定的页面为模板去生成
    inject: "head",
    chunks:["main","main2"] //指定index页面中引入的js文件为main.js与main2.js
}), new HtmlWebpackPlugin({
    title: "页面二",
    filename: "index2.html",
    template: "src/html/index2.html",
    inject: true, //相当"body"
    chunks:["main2"] //指定index页面中引入的js文件为main2.js
    // minify:{ //配置html的压缩
    //   removeComments:true, //设置去除注释
    //   collapseWhitespace:true //设置去除空格
    // }
    //excludeChunks: [""] 除了一些指定的js之排除在外,其余的js全部加载进来
})]

3.demo目录结构图

webpack的使用(插件HtmlWebpackPlugin)