utils.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. "use strict";
  2. const path = require("path");
  3. const config = require("../config");
  4. const ExtractTextPlugin = require("extract-text-webpack-plugin");
  5. const packageConfig = require("../package.json");
  6. const HtmlWebpackPlugin = require("html-webpack-plugin");
  7. const merge = require("webpack-merge");
  8. const glob = require("glob");
  9. const pathSrc = path.resolve(__dirname, "../src"); // F:\webui\multiple-pages\demo\src
  10. const devPathSrc = path.resolve(__dirname, "../../../src"); // node_modules应用下
  11. // 指定开发模式下需要加载的模块(可以做到只加载当前模块,提高开发效率)
  12. // index模块(登录)为必须,all 为所有
  13. // 登录 合同 首页 内转 排队 资源 销售 零星物资
  14. // ['index','appoint','configManager','homepage',''inward,'queue','RMS','sale','serviceManager','SporadicManage'
  15. // 统计报表 组织机构/系统管理 采购 仓储
  16. // 'statisticalReport','systemConfig','TMS','WMS','workFlow']
  17. // let devModules = ["index", "appoint", "statisticalReport", "WMS","TMS"];
  18. // let devModules = ["index", "TMS", "appoint", "WMS", "queue"];
  19. // let devModules = ["index", "statisticalReport", "inward"];
  20. // let devModules = ["index", "WMS", "inward"];
  21. // let devModules = ["index", "inward", "WMS", "queue"];
  22. let devModules = ["all"];
  23. //let devModules = ["BMS","index","sale","appoint","AMS","queue","QMS","RMS",'statisticalReport',"inward"];
  24. if (pathSrc.indexOf("node_modules") > -1) {
  25. devModules = require("../../../cors.js").devModules;
  26. }
  27. // 获取入口集合
  28. const getEntries = function(suffix, polyfill) {
  29. // 自动获取
  30. const entryHtml = glob.sync(pathSrc + "/views/**/app.html"); // 根据html来
  31. const devEntryHtml = glob.sync(devPathSrc + "/views/**/app.html"); // 根据html来
  32. const entries = {};
  33. let setEntries = function(filePath, _chunk) {
  34. _chunk = _chunk.substring(0, _chunk.lastIndexOf("/"));
  35. let flag = true;
  36. flag = false;
  37. for (let item of devModules) {
  38. if (item && (_chunk.indexOf("views/" + item) >= 0 || item === "all")) {
  39. flag = true;
  40. break;
  41. }
  42. }
  43. if (flag) {
  44. let arr = _chunk.split("/"),
  45. larr = [],
  46. rarr = [];
  47. console.log("arr: ", arr);
  48. larr = arr.slice(0, 2);
  49. _chunk = larr.join("/");
  50. if (arr.length > 2) {
  51. rarr = arr.slice(2, arr.length);
  52. _chunk = _chunk + "/" + rarr.join("-");
  53. }
  54. if (suffix) {
  55. filePath = filePath.replace(".html", suffix);
  56. }
  57. if (polyfill) {
  58. entries[_chunk] = ["babel-polyfill", filePath];
  59. } else {
  60. entries[_chunk] = filePath;
  61. }
  62. }
  63. };
  64. entryHtml.forEach(filePath => {
  65. // views/index/app.html --> views/index chunk有'/'则js/css会有相应的目录
  66. let _chunk = filePath.split(pathSrc.replace(/\\/g, "/") + "/")[1]; // views/index/app.html
  67. setEntries(filePath, _chunk);
  68. });
  69. devEntryHtml.forEach(filePath => {
  70. // views/index/app.html --> views/index chunk有'/'则js/css会有相应的目录
  71. let _chunk = filePath.split(devPathSrc.replace(/\\/g, "/") + "/")[1]; // views/index/app.html
  72. setEntries(filePath, _chunk);
  73. });
  74. return entries;
  75. };
  76. // 多入口配置(入口JS固定为main.js)
  77. exports.entries = function() {
  78. // ['babel-polyfill', './src/main.js']
  79. let _entries = getEntries(".js", true);
  80. return _entries;
  81. };
  82. //多页面输出配置
  83. exports.htmlPlugins = function() {
  84. let entryHtmls = getEntries(".html");
  85. let arr = [];
  86. for (let _chunk in entryHtmls) {
  87. console.log("loading chunk:", _chunk);
  88. // _chunk :views/index/index
  89. let conf = {
  90. favicon: pathSrc + "/assets/img/favicon.ico", //favicon路径,通过webpack引入同时可以生成hash值
  91. template: entryHtmls[_chunk], // html模板路径
  92. // filename: 'views/' + fileName, // 生成的html存放路径,相对于path
  93. filename: _chunk + ".html",
  94. chunks: [_chunk],
  95. inject: true // js插入的位置,true/'head'/'body'/false
  96. };
  97. if (process.env.NODE_ENV === "production") {
  98. console.log("package content: ", _chunk);
  99. conf = merge(conf, {
  100. chunks: ["manifest", "vendor", _chunk],
  101. minify: {
  102. // removeAttributeQuotes: true, // 删除可删除的引号
  103. removeComments: true, // 移除HTML中的注释
  104. collapseWhitespace: true // 删除空白符与换行符
  105. },
  106. chunksSortMode: "dependency"
  107. });
  108. }
  109. arr.push(new HtmlWebpackPlugin(conf));
  110. }
  111. console.log("arr:", arr);
  112. return arr;
  113. };
  114. exports.assetsPath = function(_path) {
  115. const assetsSubDirectory =
  116. process.env.NODE_ENV === "production"
  117. ? config.build.assetsSubDirectory
  118. : config.dev.assetsSubDirectory;
  119. return path.posix.join(assetsSubDirectory, _path);
  120. };
  121. exports.cssLoaders = function(options) {
  122. options = options || {};
  123. const cssLoader = {
  124. loader: "css-loader",
  125. options: {
  126. sourceMap: options.sourceMap
  127. }
  128. };
  129. const postcssLoader = {
  130. loader: "postcss-loader",
  131. options: {
  132. sourceMap: options.sourceMap
  133. }
  134. };
  135. // generate loader string to be used with extract text plugin
  136. function generateLoaders(loader, loaderOptions) {
  137. const loaders = options.usePostCSS
  138. ? [cssLoader, postcssLoader]
  139. : [cssLoader];
  140. if (loader) {
  141. loaders.push({
  142. loader: loader + "-loader",
  143. options: Object.assign({}, loaderOptions, {
  144. sourceMap: options.sourceMap
  145. })
  146. });
  147. }
  148. // Extract CSS when that option is specified
  149. // (which is the case during production build)
  150. if (options.extract) {
  151. return ExtractTextPlugin.extract({
  152. use: loaders,
  153. // 解决打包后背景图片路径不对的问题
  154. // static/css/views/index/index.css
  155. // ../../../../static/img/xx.jpg
  156. publicPath: "../../../", // 注意: 此处根据路径, 自动更改
  157. fallback: "vue-style-loader"
  158. });
  159. } else {
  160. return ["vue-style-loader"].concat(loaders);
  161. }
  162. }
  163. // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  164. return {
  165. css: generateLoaders(),
  166. postcss: generateLoaders(),
  167. less: generateLoaders("less"),
  168. sass: generateLoaders("sass", {
  169. indentedSyntax: true
  170. }),
  171. scss: generateLoaders("sass"),
  172. stylus: generateLoaders("stylus"),
  173. styl: generateLoaders("stylus")
  174. };
  175. };
  176. // Generate loaders for standalone style files (outside of .vue)
  177. exports.styleLoaders = function(options) {
  178. const output = [];
  179. const loaders = exports.cssLoaders(options);
  180. for (const extension in loaders) {
  181. const loader = loaders[extension];
  182. output.push({
  183. test: new RegExp("\\." + extension + "$"),
  184. use: loader
  185. });
  186. }
  187. return output;
  188. };
  189. exports.createNotifierCallback = () => {
  190. const notifier = require("node-notifier");
  191. return (severity, errors) => {
  192. if (severity !== "error") return;
  193. const error = errors[0];
  194. const filename = error.file && error.file.split("!").pop();
  195. notifier.notify({
  196. title: packageConfig.name,
  197. message: severity + ": " + error.name,
  198. subtitle: filename || "",
  199. icon: path.join(__dirname, "logo.png")
  200. });
  201. };
  202. };