utils.js 6.7 KB

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