utils.js 7.7 KB

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