utils.js 7.2 KB

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