utils.js 7.4 KB

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