utils.js 6.8 KB

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