utils.js 7.6 KB

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