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