utils.js 7.2 KB

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