utils.js 6.9 KB

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