1. 自动化生成项目中HTML页面(上)

    使用场景:webpack可以处理js文件,那html中引入的JS文件,如果使用hash值命名,每次打包不一致,可以使用html-webpack-plugin去动态生成html页面,起到每次更新JS导入的作用
    安装npm i html-webpack-plugin@3.0.7 —save-dev
    在webpack中使用插件,代码如下:

    1. var htmlWebpackPlugin = require("html-webpack-plugin");
    2. // 存在一个上下文就是根目录
    3. module.exports = {
    4. entry: {
    5. main: "./src/script/main.js",
    6. a: "./src/script/a.js"
    7. },//打包入口
    8. output: {
    9. path: "./dist",//打包之后文件的存放路径
    10. filename: "js/[name]-[chunkhash].js"//打包之后的文件名
    11. },
    12. plugins:[
    13. new htmlWebpackPlugin({
    14. filename:"index-[hash].html",//html处理之后的文件名
    15. template:'index.html',//以根目录下的index.html为模板
    16. inject:"head" //js文件在<head></head>引入,还是在<body></body>引入
    17. })
    18. ]
    19. }
    1. 自动化生成项目中HTML页面(中)
    • 调用htmlWebpackPlugin插件时,传入的模板(index.html)中,可以获取htmlWebpackPlugin的属性参数(备注:直接取值<%= %> 执行表达式:<% %>) ```javascript var htmlWebpackPlugin = require(“html-webpack-plugin”); // 存在一个上下文就是根目录 module.exports = { entry: { main: “./src/script/main.js”, a: “./src/script/a.js” },//打包入口 output: { path: “./dist”,//打包之后文件的存放路径 filename: “js/[name]-[chunkhash].js”//打包之后的文件名 }, plugins: [ new htmlWebpackPlugin({
      1. filename: "index.html",//html处理之后的文件名 index-[hash].html 此处的名字可以使用hash
      2. template: 'index.html',//以根目录下的index.html为模板
      3. inject: true, //js文件在<head></head>引入,还是在<body></body>引入
      4. title: "webpack is good",
      5. date: new Date()
      }) ] }
    1. ```html
    2. <!DOCTYPE html>
    3. <html lang="en">
    4. <head>
    5. <meta charset="UTF-8">
    6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    8. <title>
    9. <%= htmlWebpackPlugin.options.title%>
    10. </title>
    11. <script src="bundle.js" type="text/javascript"></script>
    12. </head>
    13. <!-- 直接取值<%= %> 执行表达式:<% %> -->
    14. <body>
    15. <% for(var key in htmlWebpackPlugin){%>
    16. <%= key +":"+ JSON.stringify(htmlWebpackPlugin[key]) %>
    17. <% } %>
    18. </body>
    19. </html>
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>
    8. webpack is good
    9. </title>
    10. <script src="bundle.js" type="text/javascript"></script>
    11. <script type="text/javascript" src="js/main-2f0460e5ccb816026595.js"></script><script type="text/javascript" src="js/a-1ceb4b79e006a83c7f37.js"></script></head>
    12. <body>
    13. files:{
    14. "publicPath": "",
    15. "chunks": {
    16. "main": {
    17. "size": 211,
    18. "entry": "js/main-2f0460e5ccb816026595.js",
    19. "hash": "2f0460e5ccb816026595",
    20. "css": []
    21. },
    22. "a": {
    23. "size": 177,
    24. "entry": "js/a-1ceb4b79e006a83c7f37.js",
    25. "hash": "1ceb4b79e006a83c7f37",
    26. "css": []
    27. }
    28. },
    29. "js": [
    30. "js/main-2f0460e5ccb816026595.js",
    31. "js/a-1ceb4b79e006a83c7f37.js"
    32. ],
    33. "css": []
    34. }
    35. options:{
    36. "template": "D:\\OpenSource\\webpack-demo\\node_modules\\html-webpack-plugin\\lib\\loader.js!D:\\OpenSource\\webpack-demo\\index.html",
    37. "filename": "index.html",
    38. "hash": false,
    39. "inject": "head",
    40. "compile": true,
    41. "favicon": false,
    42. "minify": false,
    43. "cache": true,
    44. "showErrors": true,
    45. "chunks": "all",
    46. "excludeChunks": [],
    47. "title": "webpack is good",
    48. "xhtml": false
    49. }
    50. </body>
    51. </html>
    • 需求限定JS位置 ```javascript var htmlWebpackPlugin = require(“html-webpack-plugin”); // 存在一个上下文就是根目录 module.exports = { entry: {
      1. main: "./src/script/main.js",
      2. a: "./src/script/a.js"
      },//打包入口 output: {
      1. path: "./dist",//打包之后文件的存放路径
      2. filename: "js/[name]-[chunkhash].js"//打包之后的文件名
      }, plugins: [
      1. new htmlWebpackPlugin({
      2. filename: "index.html",//html处理之后的文件名 index-[hash].html 此处的名字可以使用hash
      3. template: 'index.html',//以根目录下的index.html为模板
      4. inject: false, //js文件在<head></head>引入,还是在<body></body>引入
      5. title: "webpack is good",
      6. })
      ] }
    1. ```html
    2. <!DOCTYPE html>
    3. <html lang="en">
    4. <head>
    5. <meta charset="UTF-8">
    6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    8. <title>
    9. <%=htmlWebpackPlugin.options.title%>
    10. </title>
    11. <script type="text/javascript" src="<%= htmlWebpackPlugin.files.chunks.main.entry %>"></script>
    12. </head>
    13. <body>
    14. <script type="text/javascript" src="<%= htmlWebpackPlugin.files.chunks.a.entry %>"></script>
    15. </body>
    16. </html>
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>
    8. webpack is good
    9. </title>
    10. <script type="text/javascript" src="js/main-2f0460e5ccb816026595.js"></script>
    11. </head>
    12. <body>
    13. <script type="text/javascript" src="js/a-1ceb4b79e006a83c7f37.js"></script>
    14. </body>
    15. </html>
    • publicPath属性,发布到服务器之后的域名地址 ```javascript var htmlWebpackPlugin = require(“html-webpack-plugin”); // 存在一个上下文就是根目录 module.exports = { entry: { main: “./src/script/main.js”, a: “./src/script/a.js” },//打包入口 output: { path: “./dist”,//打包之后文件的存放路径 filename: “js/[name]-[chunkhash].js”,//打包之后的文件名 publicPath:”http://cdn.com"//发布服务器地址 }, plugins: [ new htmlWebpackPlugin({
      1. filename: "index.html",//html处理之后的文件名 index-[hash].html 此处的名字可以使用hash
      2. template: 'index.html',//以根目录下的index.html为模板
      3. inject: false, //js文件在<head></head>引入,还是在<body></body>引入
      4. title: "webpack is good",
      }) ] }
    1. ```javascript
    2. <!DOCTYPE html>
    3. <html lang="en">
    4. <head>
    5. <meta charset="UTF-8">
    6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    8. <title>
    9. webpack is good
    10. </title>
    11. <script type="text/javascript" src="http://cdn.com/js/main-c87d8b38bcd1c10a0a9b.js"></script>
    12. </head>
    13. <body>
    14. <script type="text/javascript" src="http://cdn.com/js/a-cbbbe45dd56850a7c396.js"></script>
    15. </body>
    16. </html>
    • index.html文件压缩,还有很多属性,自行上官网学习
      1. minify: {
      2. removeComments: true,//删除注释
      3. collapseWhitespace: true//去掉空格
      4. }
      ```javascript var htmlWebpackPlugin = require(“html-webpack-plugin”); // 存在一个上下文就是根目录 module.exports = { entry: {
      1. main: "./src/script/main.js",
      2. a: "./src/script/a.js"
      },//打包入口 output: {
      1. path: "./dist",//打包之后文件的存放路径
      2. filename: "js/[name]-[chunkhash].js",//打包之后的文件名
      3. publicPath: "http://cdn.com"//发布服务器地址
      }, plugins: [
      1. new htmlWebpackPlugin({
      2. filename: "index.html",//html处理之后的文件名 index-[hash].html 此处的名字可以使用hash
      3. template: 'index.html',//以根目录下的index.html为模板
      4. inject: false, //js文件在<head></head>引入,还是在<body></body>引入
      5. title: "webpack is good",
      6. date: new Date(),
      7. minify: {
      8. removeComments: true,//删除注释
      9. collapseWhitespace: true//去掉空格
      10. }
      11. })
      ] }
    1. 3. 自动化生成项目中HTML页面(下)
    2. 处理多页面应用情况(添加多个htmlWebpackPlugin实例):
    3. - chunks属性,页面导入指定JS
    4. - excludeChunks属性,除了指定的JS的,其它的JS都导入
    5. - htmlWebpackPlugin.files.chunkshtml中获取的就是webpack.config.js指定的JS
    6. ```javascript
    7. let htmlWebpackPlugin = require("html-webpack-plugin");
    8. // 存在一个上下文就是根目录
    9. module.exports = {
    10. entry: {
    11. main: "./src/script/main.js",
    12. a: "./src/script/a.js",
    13. b: "./src/script/b.js",
    14. c: "./src/script/c.js",
    15. },//打包入口
    16. output: {
    17. path: "./dist",//打包之后文件的存放路径
    18. filename: "js/[name]-[chunkhash].js",//打包之后的文件名
    19. publicPath: "http://cdn.com"//发布服务器地址
    20. },
    21. plugins: [
    22. new htmlWebpackPlugin({
    23. filename: "a.html",//html处理之后的文件名 index-[hash].html 此处的名字可以使用hash
    24. template: 'index.html',//以根目录下的index.html为模板
    25. inject: "body", //js文件在<head></head>引入,还是在<body></body>引入
    26. title: "This is a.html",
    27. date: new Date(),
    28. chunks:['main','a'],//导入main和a
    29. minify: {
    30. removeComments: true,//删除注释
    31. collapseWhitespace: true//去掉空格
    32. }
    33. }),
    34. new htmlWebpackPlugin({
    35. filename: "b.html",//html处理之后的文件名 index-[hash].html 此处的名字可以使用hash
    36. template: 'index.html',//以根目录下的index.html为模板
    37. inject: "body", //js文件在<head></head>引入,还是在<body></body>引入
    38. title: "This is b.html",
    39. excludeChunks:['c','a'],//除了a,c都导入
    40. date: new Date(),
    41. minify: {
    42. removeComments: true,//删除注释
    43. collapseWhitespace: true//去掉空格
    44. }
    45. }),
    46. new htmlWebpackPlugin({
    47. filename: "c.html",//html处理之后的文件名 index-[hash].html 此处的名字可以使用hash
    48. template: 'index.html',//以根目录下的index.html为模板
    49. inject: "body", //js文件在<head></head>引入,还是在<body></body>引入
    50. title: "This is c.html",
    51. excludeChunks:['b','a'],//除了a,b都导入
    52. date: new Date(),
    53. minify: {
    54. removeComments: true,//删除注释
    55. collapseWhitespace: true//去掉空格
    56. }
    57. })
    58. ]
    59. }

    注意:inject设定为head或者是body会加入页面所需js,false表示自定义

    • 导入初始化的main.js内容,不适用cdn(网络请求的方式加载)
      1. compilation.assets[htmlWebpackPlugin.files.chunks.main.entry
      2. .substr(htmlWebpackPlugin.files.publicPath.length)]
      3. .source()
      ```javascript

    let htmlWebpackPlugin = require(“html-webpack-plugin”); // 存在一个上下文就是根目录 module.exports = { entry: { main: “./src/script/main.js”, a: “./src/script/a.js”, b: “./src/script/b.js”, c: “./src/script/c.js”, },//打包入口 output: { path: “./dist”,//打包之后文件的存放路径 filename: “js/[name]-[chunkhash].js”,//打包之后的文件名 publicPath: “http://cdn.com"//发布服务器地址 }, plugins: [ new htmlWebpackPlugin({ filename: “a.html”,//html处理之后的文件名 index-[hash].html 此处的名字可以使用hash template: ‘index.html’,//以根目录下的index.html为模板 inject: false, //js文件在引入,还是在引入 title: “This is a.html”, date: new Date(), chunks: [‘main’, ‘a’],//导入main和a minify: { removeComments: true,//删除注释 collapseWhitespace: true//去掉空格 } }), new htmlWebpackPlugin({ filename: “b.html”,//html处理之后的文件名 index-[hash].html 此处的名字可以使用hash template: ‘index.html’,//以根目录下的index.html为模板 inject: false, //js文件在引入,还是在引入 title: “This is b.html”, excludeChunks: [‘c’, ‘a’],//除了a,c都导入 date: new Date(), minify: { removeComments: true,//删除注释 collapseWhitespace: true//去掉空格 } }), new htmlWebpackPlugin({ filename: “c.html”,//html处理之后的文件名 index-[hash].html 此处的名字可以使用hash template: ‘index.html’,//以根目录下的index.html为模板 inject: false, //js文件在引入,还是在引入
    title: “This is c.html”, excludeChunks: [‘b’, ‘a’],//除了a,b都导入 date: new Date(), minify: { removeComments: true,//删除注释 collapseWhitespace: true//去掉空格 } })

    1. ]

    }

    1. ```html
    2. <!--
    3. 模板html
    4. -->
    5. <!DOCTYPE html>
    6. <html lang="en">
    7. <head>
    8. <meta charset="UTF-8">
    9. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    10. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    11. <title>
    12. <%=htmlWebpackPlugin.options.title%>
    13. </title>
    14. <!-- 导入初始化JS,无需请求网络优化 -->
    15. <%=
    16. compilation.assets[htmlWebpackPlugin.files.chunks.main.entry
    17. .substr(htmlWebpackPlugin.files.publicPath.length)]
    18. .source() %>
    19. </head>
    20. <body>
    21. <% for(var key in htmlWebpackPlugin.files.chunks){ %>
    22. <% if(key != 'main' ){ %>
    23. <script src="<%= htmlWebpackPlugin.files.chunks[key].entry %>" type="text/javascript"></script>
    24. <% } %>
    25. <% } %>
    26. </body>
    27. </html>