utils.js 7.1 KB

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