multipage-helper.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * 多页面支持
  3. * @File:
  4. * @Description: 多页面支持
  5. * @author qingyi xuelongqy@foxmail.com
  6. * @date 2017/6/15 10:16
  7. * @version V1.0
  8. */
  9. var path = require('path')
  10. var fs = require("fs")
  11. var HtmlWebpackPlugin = require('html-webpack-plugin')
  12. var moduleList //缓存多页面模块列表
  13. var moduleRootPath = './src/page' //模块根目录(这个可以根据自己的需求命名)
  14. /**
  15. * 获取js入口数组
  16. */
  17. exports.getEntries = function getEntries(){
  18. //缓存js入口数组
  19. var entries = {}
  20. //初始化模块列表
  21. this.getModuleList()
  22. //变量模块列表
  23. moduleList.forEach(function (module) {
  24. if (module.moduleID != "" && module.moduleJS != ""){
  25. entries[module.moduleID] = module.moduleJS
  26. }
  27. })
  28. console.log("*********************************** entries ***********************************")
  29. console.log(entries)
  30. return entries
  31. }
  32. /**
  33. * 获取多页面模块列表
  34. * @returns {模块的信息集合}
  35. */
  36. exports.getModuleList = function getModuleList() {
  37. //判断是否为空,不为空则直接返回
  38. if (moduleList){
  39. return moduleList
  40. }else {//为空则读取列表
  41. moduleList = new Array();
  42. readDirSync(moduleRootPath, "")
  43. console.log("*********************************** moduleList ***********************************")
  44. console.log(moduleList)
  45. return moduleList
  46. }
  47. }
  48. /**
  49. * 获取dev的Html模板集合
  50. * @returns {dev的Html模板集合}
  51. */
  52. exports.getDevHtmlWebpackPluginList = function getDevHtmlWebpackPluginList(){
  53. console.log("*********************************** devHtmlWebpackPluginList ***********************************")
  54. //缓存dev的Html模板集合
  55. var devHtmlWebpackPluginList = []
  56. //获取多页面模块集合
  57. var moduleList = this.getModuleList()
  58. //遍历生成模块的HTML模板
  59. moduleList.forEach(function (mod) {
  60. //生成配置
  61. var conf = {
  62. filename: mod.moduleID+".html",
  63. template: mod.moduleHTML,
  64. chunks: [mod.moduleID],
  65. inject: true
  66. }
  67. console.log(conf)
  68. //添加HtmlWebpackPlugin对象
  69. devHtmlWebpackPluginList.push(new HtmlWebpackPlugin(conf))
  70. })
  71. return devHtmlWebpackPluginList
  72. }
  73. /**
  74. * 获取prod的Html模板集合
  75. * @returns {prod的Html模板集合}
  76. */
  77. exports.getProdHtmlWebpackPluginList = function getProdHtmlWebpackPluginList(){
  78. console.log("*********************************** prodHtmlWebpackPluginList ***********************************")
  79. //缓存dev的Html模板集合
  80. var prodHtmlWebpackPluginList = []
  81. //获取多页面模块集合
  82. var moduleList = this.getModuleList()
  83. //遍历生成模块的HTML模板
  84. moduleList.forEach(function (mod) {
  85. //生成配置
  86. var conf = {
  87. filename: mod.moduleID+".html",
  88. template: mod.moduleHTML,
  89. inject: true,
  90. minify: {
  91. removeComments: true,
  92. collapseWhitespace: true,
  93. removeAttributeQuotes: true
  94. // more options:
  95. // https://github.com/kangax/html-minifier#options-quick-reference
  96. },
  97. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  98. chunksSortMode: 'dependency',
  99. chunks: ['manifest','vendor',mod.moduleID]
  100. }
  101. console.log(conf)
  102. //添加HtmlWebpackPlugin对象
  103. prodHtmlWebpackPluginList.push(new HtmlWebpackPlugin(conf))
  104. })
  105. return prodHtmlWebpackPluginList
  106. }
  107. /**
  108. * 深度遍历目录,并整理多页面模块
  109. * @param path 需要变量的路径
  110. * @param moduleName 模块名称
  111. */
  112. function readDirSync(path,moduleName){
  113. //缓存模块对象
  114. var module = {moduleID:"",moduleHTML:"",moduleJS:""}
  115. //获取当前模块ID
  116. var moduleID = path.replace(moduleRootPath+"/","")
  117. if (path == moduleRootPath){
  118. moduleID = ""
  119. }
  120. module.moduleID = moduleID
  121. //获取目录下所有文件及文件夹
  122. var pa = fs.readdirSync(path)
  123. pa.forEach(function(ele,index){
  124. var info = fs.statSync(path+"/"+ele)
  125. if(info.isDirectory()){
  126. // console.log("dir: "+ele)
  127. readDirSync(path+"/"+ele, ele)
  128. }else{
  129. //判断当前模块的html是否存在
  130. if (moduleName+".html" == ele){
  131. module.moduleHTML = path+"/"+ele
  132. }
  133. //判断当前模块的js是否存在
  134. if (moduleName+".js" == ele){
  135. module.moduleJS = path+"/"+ele
  136. }
  137. // console.log("file: "+ele)
  138. }
  139. })
  140. //判断模块是否真实(可能只是个分级目录)
  141. if ((module.moduleID != "" && module.moduleHTML != "") || (module.moduleID != "" && module.moduleJS != "")){
  142. moduleList.push(module)
  143. }
  144. }