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