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