nrb.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. const fs = require('fs');
  2. const path = require('path');
  3. const webpack = require('webpack');
  4. const webpackDevServer = require('webpack-dev-server');
  5. const webpackBaseConfig = require('./webpack.base.config');
  6. const webpackDllConfig = require('./webpack.dll.config');
  7. const getEntry = require('./getEntry');
  8. const config = require('../new-config.json');
  9. const dependjsConf = require('../dependjs.json');
  10. Object.assign(config, dependjsConf);
  11. // 在编译之后打印文字的webpack插件
  12. const LogAfterCompilePlugin = require('./LogAfterCompilePlugin');
  13. const CreateHtmlPlugin = require('./create-html-webpack-plugin/CreateHtmlPlugin');
  14. // 工具集
  15. const h = require('./hammer');
  16. // 入口对象集
  17. let entryTmp = getEntry();
  18. let libEntryTmp = getEntry('lib');
  19. // 取出js入口路径
  20. let entries = entryTmp.entry;
  21. let libEntries = libEntryTmp.entry;
  22. // 取出模版路径
  23. let entryTmpMap = entryTmp.template;
  24. let libEntryTmpMap = libEntryTmp.template;
  25. // 目标路径
  26. let targetPath = webpackBaseConfig.output.path
  27. // 定义项目路径
  28. let projectPath = path.join(__dirname, '../');
  29. // 根据模版路径文件,取出模版
  30. let templateMap = getTemplate(entryTmpMap);
  31. module.exports = (m) => {
  32. copyDir();
  33. if (config['dll-entry'] && config['dll-entry'].length > 0) {
  34. runDllCompile()
  35. .then(() => {
  36. let manifestPath = getManifestPath();
  37. webpackBaseConfig.plugins.unshift(
  38. new webpack.DllReferencePlugin({
  39. manifest: require(manifestPath)
  40. })
  41. )
  42. runCompile(m);
  43. });
  44. } else {
  45. runCompile(m);
  46. }
  47. }
  48. // 获取模板内容并且拼装模板
  49. function getTemplate(templateMap) {
  50. let templateContentMap = {};
  51. templateContentMap['default'] = path.join(projectPath, config['default-template'])
  52. Object.keys(templateMap).map((item) => {
  53. let templatePath = templateMap[item];
  54. templateContentMap[item] = path.join(projectPath, templatePath)
  55. });
  56. return templateContentMap
  57. }
  58. // 获取dll生成得manifest文件
  59. function getManifestPath() {
  60. let outputPath = webpackDllConfig.output.path;
  61. let fileList = fs.readdirSync(outputPath);
  62. let manifestPath = '../dist/hrpub/dll/vendor-manifest.json';
  63. for (let filename of fileList) {
  64. if (path.extname(filename) === '.json') {
  65. manifestPath = path.join(outputPath, filename);
  66. break;
  67. }
  68. }
  69. return manifestPath;
  70. }
  71. // 运行dll编译
  72. function runDllCompile() {
  73. webpackDllConfig.plugins.push(new LogAfterCompilePlugin({
  74. word: 'note: dll has compiled'
  75. }));
  76. return new Promise((resolve, reject) => {
  77. webpack(webpackDllConfig, (err) => {
  78. if (err) {
  79. console.log(err);
  80. }
  81. resolve();
  82. });
  83. });
  84. }
  85. // 执行编译
  86. function runCompile(m = []) {
  87. let pageCompiler = null;
  88. let nEntries = {};
  89. let reg = new RegExp(m.join('|'));
  90. let externals = {};
  91. /*let configjs = ''; //额外配置的js文件
  92. let configcss = ''; //额外配置的css文件*/
  93. Object.keys(entries).map((item) => {
  94. if (reg.test(item) || m.length === 0) {
  95. nEntries[item] = entries[item];
  96. }
  97. });
  98. // dependModuleName: 依赖的模块名
  99. // dependjs: 依赖的js文件配置
  100. /*if (Array.isArray(config.dependjs)) {
  101. configjs += config.dependjs.map(src => {
  102. let moduleName = /(?:\.\.\/)*([^\.]*)\.js/.exec(src);
  103. if (moduleName && moduleName[1]) {
  104. externals[moduleName[1]] = moduleName[1];
  105. }
  106. return `<script src="${src}?v=${Date.now()}"></script>`;
  107. }).join('');
  108. }*/
  109. // dependModuleName: 依赖的模块名
  110. if (Array.isArray(config.dependModuleName)) {
  111. // 打包时排除
  112. config.dependModuleName.forEach(item => (externals[`${item}`] = `${item}/index`));
  113. }
  114. // dependcss: 依赖的css文件配置
  115. /*if (Array.isArray(config.dependcss)) {
  116. configcss += config.dependcss
  117. .map(item => `<link rel="stylesheet" href=${item}?v=${Date.now()}>`)
  118. .join('');
  119. }*/
  120. entries = nEntries;
  121. let pageWBC = {
  122. ...webpackBaseConfig,
  123. plugins: [...webpackBaseConfig.plugins],
  124. entry: entries,
  125. externals: {
  126. ...webpackBaseConfig.externals,
  127. ...externals
  128. }
  129. };
  130. pageWBC.plugins.push(new CreateHtmlPlugin({
  131. templateMap: templateMap,
  132. beforeAppendCss: ($, filePath = "") => {
  133. // 为了去掉平台对于console的影响
  134. /*$('head').prepend(`
  135. <script>
  136. window.__console = {
  137. ...window.console
  138. };
  139. </script>
  140. `);*/
  141. let configcss = getConfigCss(filePath.substr(0, filePath.length - 5));
  142. $('head').prepend(configcss)
  143. },
  144. beforeAppendJs: ($, filePath = "") => {
  145. if (config['dll-entry'] && config['dll-entry'].length > 0) {
  146. $('body').append(`<script src="/hrpub/dll/vendor.js"></script>`);
  147. }
  148. /*$('body').append(`
  149. <script>
  150. window.console = window.__console
  151. </script>
  152. `);*/
  153. let configjs = getConfigJs(filePath.substr(0, filePath.length - 5));
  154. $('body').append(configjs);
  155. }
  156. }));
  157. /*Object.keys(entries).map((key) => {
  158. let htmlWebpackOption = {
  159. filename: `${key}.html`,
  160. template: path.join(projectPath, config['default-template']),
  161. chunks: [key],
  162. inject: true,
  163. templateParameters: {
  164. buildInfo: '',
  165. configjs: configjs, //为模板添加js
  166. configcss: configcss //为模板添加css
  167. }
  168. };
  169. if (entryTmpMap[key]) {
  170. htmlWebpackOption.template = path.join(projectPath, entryTmpMap[key]);
  171. }
  172. pageWBC.plugins.push(new HtmlWebpackPlugin(htmlWebpackOption));
  173. });*/
  174. pageWBC.plugins.push(new LogAfterCompilePlugin());
  175. pageCompiler = webpack(pageWBC);
  176. let libCompiler = webpack({
  177. ...webpackBaseConfig,
  178. entry: libEntries
  179. });
  180. libCompiler.run(function (err, stat) {
  181. if (err) {
  182. console.log(err);
  183. }
  184. let server = new webpackDevServer(pageCompiler, {
  185. contentBase: path.join(__dirname, '../dist'),
  186. proxy: config.proxy,
  187. stats: 'errors-only',
  188. inline: config['dev-server']['refresh-immediately']
  189. });
  190. server.listen(3006, '', () => {
  191. console.log('open localhost:3006');
  192. });
  193. });
  194. }
  195. // 执行文件复制
  196. function copyDir() {
  197. if (!h.isFileExist(targetPath)) {
  198. fs.mkdirSync(targetPath);
  199. }
  200. config.copy.map((obj) => {
  201. let absFrom = path.join(projectPath, obj.from);
  202. let absTo = path.join(targetPath, obj.to);
  203. if (!h.isFileExist(absTo)) {
  204. h.copyDirSync(targetPath, absFrom, absTo);
  205. }
  206. });
  207. }
  208. /*
  209. * 引用js优化,如果需要只在特定的html中引入uap的js,可以放开这里的代码
  210. * */
  211. function getConfigJs(filePath) {
  212. // dependjs: 依赖的js文件配置
  213. let configjs = '';
  214. if (config.dependjs && Array.isArray(config.dependjs[filePath])) {
  215. config.dependjs[filePath].forEach(file => {
  216. configjs += `<script src="${file}?v=${Date.now()}"></script>`;
  217. })
  218. }
  219. if (Array.isArray(config.report) && config.report.includes(filePath)) {
  220. configjs += `<script src="../../../../lappreportrt/nc-report/public/vendor.js"></script>`;
  221. configjs += `<script src="../../../../lappreportrt/nc-report/index.js"></script>`;
  222. }
  223. if (Array.isArray(config.wpsconfig) && config.wpsconfig.includes(filePath)) {
  224. configjs += `<script src="../../../../hrpub/public/wpsconfig/wps_sdk.js"></script>`;
  225. }
  226. return configjs;
  227. }
  228. function getConfigCss(filePath) {
  229. // dependcss: 依赖的css文件配置
  230. let configcss = '';
  231. if (Array.isArray(config.dependcss)) {
  232. configcss += config.dependcss
  233. .map(item => `<link rel="stylesheet" href=${item}?v=${Date.now()}>`)
  234. .join('');
  235. }
  236. if (Array.isArray(config.report) && config.report.includes(filePath)) {
  237. configcss += `<link rel="stylesheet" href="../../../../lappreportrt/nc-report/public/vendor.css" />`;
  238. configcss += `<link rel="stylesheet" href="../../../../lappreportrt/nc-report/index.css" />`;
  239. }
  240. return configcss;
  241. }