buildEntry.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * @Author: liyxt
  3. * @Date: 2019-04-23 09:37:04
  4. * @LastEditors: liyxt
  5. * @LastEditTime: 2020-05-19 16:43:33
  6. * @Description: file content
  7. */
  8. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  9. const glob = require('glob');
  10. const CopyWebpackPlugin = require('copy-webpack-plugin');
  11. const HtmlWebpackPlugin = require('html-webpack-plugin');
  12. const fs = require('fs');
  13. const webpack = require('webpack');
  14. module.exports = function buildEntry({ buildPath, buildWithoutHTML, hash, mode, client, fse }) {
  15. fse = fse === 'true' || fse === true || false;
  16. Array.isArray(buildWithoutHTML) && buildWithoutHTML.unshift('refer');
  17. let projects = [],
  18. plugins = [],
  19. entries = {},
  20. externals = {};
  21. // 遍历src下的js
  22. (function(callback) {
  23. if (Array.isArray(buildPath)) {
  24. buildPath.forEach(_buildPath => {
  25. callback(_buildPath);
  26. });
  27. } else {
  28. callback(buildPath);
  29. }
  30. })(function(buildPath) {
  31. getFiles(buildPath);
  32. });
  33. projects.forEach(e => {
  34. if (e === 'uapbd') {
  35. // guozhq让弄的,供应链特殊
  36. fs.existsSync('./src/uapbd/scmbase/public') &&
  37. plugins.push(
  38. new CopyWebpackPlugin([{ from: `./src/uapbd/scmbase/public`, to: `./uapbd/scmbase/public` }])
  39. );
  40. // wanghxm让弄的,hr特殊
  41. fs.existsSync('./src/uapbd/hrbase/public') &&
  42. plugins.push(
  43. new CopyWebpackPlugin([{ from: `./src/uapbd/hrbase/public`, to: `./uapbd/hrbase/public` }])
  44. );
  45. }
  46. fs.existsSync(`./src/${e}/public`) &&
  47. plugins.push(
  48. new CopyWebpackPlugin([
  49. // {output}/to/file.txt
  50. { from: `./src/${e}/public`, to: `./${e}/public` }
  51. ])
  52. );
  53. });
  54. function getFiles(buildPath) {
  55. glob.sync(buildPath).forEach(path => {
  56. // path ---为加载的每个index.js文件:./src/reva_demo/module/apply/list/index.js
  57. // chunk = 节点+list/card: reva_demo/module/apply/list
  58. if (
  59. (client === 'mobile' && path.includes('/mobile_')) ||
  60. (client !== 'mobile' && !path.includes('/mobile_'))
  61. ) {
  62. // 移动端 || web端
  63. let chunk = path.split('./src/')[1].split('/index.js')[0],
  64. project = chunk.split('/')[0]; // reva_demo
  65. //把src自定义命名下的文件层级减掉,更改第二层级,把领域名改为 extend_领域名 by bbqin
  66. if (fse) {
  67. let chunkarr = chunk.split('/');
  68. chunkarr[0] = 'NCCExtend';
  69. chunkarr[1] = `extend_${chunkarr[1]}`;
  70. chunk = chunkarr.join('/');
  71. }
  72. projects.includes(project) || projects.push(project);
  73. // 生成webpack.config.js的入口
  74. let configJSONPath = './src/' + chunk + '/config.json',
  75. isExists = fs.existsSync(configJSONPath),
  76. _hash;
  77. if (isExists) {
  78. // 特殊处理的
  79. _hash = require('.' + configJSONPath).hash;
  80. }
  81. if (hash === 'false') {
  82. hash = false;
  83. } else if (hash === 'true') {
  84. hash = true;
  85. }
  86. let _chunk = ('/' + chunk + '/').toLowerCase();
  87. if (mode === 'development') {
  88. entries[`${chunk}/index`] = path;
  89. } else {
  90. if (hash) {
  91. // 筛选出带hash的
  92. if (_hash) {
  93. // config.json里的hash优先级高
  94. entries[`${chunk}/index`] = path;
  95. } else if (_hash !== false) {
  96. // 非参照页面生成hash
  97. !(
  98. _chunk.includes('/refer/') ||
  99. _chunk.includes('/ref/') ||
  100. _chunk.includes('/refers/') ||
  101. _chunk.includes('/mobile_refer/') ||
  102. fse
  103. ) && (entries[`${chunk}/index`] = path);
  104. }
  105. } else {
  106. // 筛选出不带hash的
  107. if (_hash === false) {
  108. // config.json里的hash优先级高
  109. entries[`${chunk}/index`] = path;
  110. } else if (_hash !== true) {
  111. // 参照页面不生成hash
  112. (_chunk.includes('/refer/') ||
  113. _chunk.includes('/ref/') ||
  114. _chunk.includes('/refers/') ||
  115. _chunk.includes('/mobile_refer/') ||
  116. _hash === false ||
  117. fse) &&
  118. (entries[`${chunk}/index`] = path);
  119. }
  120. }
  121. }
  122. // buildWithoutHTML中的页面不生成html
  123. if (entries[`${chunk}/index`]) {
  124. let templatePath = client === 'mobile' ? './template/mobileTemplate.html' : './template/index.html';
  125. let configjs = ''; //额外配置的js文件
  126. let configcss = ''; //额外配置的css文件
  127. if (isExists) {
  128. let {
  129. template,
  130. output,
  131. dependjs,
  132. dependcss,
  133. dependModuleName,
  134. report,
  135. echarts,
  136. prodProxy
  137. } = require('.' + configJSONPath);
  138. // template: HTML模板路径
  139. if (template) {
  140. templatePath = template;
  141. }
  142. // output: 单独输出的文件配置
  143. if (output) {
  144. entries[`${output}/index`] = path;
  145. }
  146. // report: 报表依赖
  147. if (report) {
  148. configjs += `<script src="../../../../lappreportrt/nc-report/public/vendor.js"></script>`;
  149. configjs += `<script src="../../../../lappreportrt/nc-report/index.js"></script>`;
  150. configcss += `<link rel="stylesheet" href="../../../../lappreportrt/nc-report/public/vendor.css" />`;
  151. configcss += `<link rel="stylesheet" href="../../../../lappreportrt/nc-report/index.css" />`;
  152. }
  153. if (echarts) {
  154. configjs += `<script src="../../../../platform/echarts.js"></script>`;
  155. }
  156. // dependjs: 依赖的js文件配置
  157. if (Array.isArray(dependjs)) {
  158. configjs += dependjs.map(src => `<script src="${src}?v=${Date.now()}"></script>`).join('');
  159. }
  160. // dependcss: 依赖的css文件配置
  161. if (Array.isArray(dependcss)) {
  162. configcss += dependcss
  163. .map(item => `<link rel="stylesheet" href=${item}?v=${Date.now()}>`)
  164. .join('');
  165. }
  166. // dependModuleName: 依赖的模块名
  167. if (Array.isArray(dependModuleName)) {
  168. // 打包时排除
  169. dependModuleName.forEach(item => (externals[`${item}`] = `${item}/index`));
  170. }
  171. plugins.push(
  172. new webpack.DefinePlugin({
  173. PROD_PROXY: JSON.stringify((mode !== 'development' && prodProxy) || '')
  174. })
  175. );
  176. }
  177. if (!(buildWithoutHTML || []).some(e => path.includes(e))) {
  178. const htmlConf = {
  179. filename: `${chunk}/index.html`, // 生成的html文件名,可加目录/.../.../index.html
  180. template: `${templatePath}`, // 模板html路径
  181. inject: true, //允许插件修改哪些内容,包括head与body
  182. chunks: [`${chunk}/index`], // 生成的html文件引入哪些js,不传的话引入所有js
  183. cache: true,
  184. templateParameters: {
  185. configjs: configjs, //为模板添加js
  186. configcss: configcss //为模板添加css
  187. }
  188. };
  189. plugins.push(new HtmlWebpackPlugin(htmlConf));
  190. }
  191. }
  192. }
  193. });
  194. }
  195. let cleanOnceBeforeBuildPatterns = Object.values(entries).map(e => e.replace('index.js', '').replace('./src/', ''));
  196. plugins.push(
  197. new CleanWebpackPlugin({
  198. cleanOnceBeforeBuildPatterns,
  199. cleanAfterEveryBuildPatterns: [],
  200. verbose: true
  201. })
  202. );
  203. return {
  204. plugins,
  205. entries,
  206. externals
  207. };
  208. };