uglifyStatics.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import fs from 'fs';
  2. import { minify_sync } from 'terser';
  3. import * as path from 'path';
  4. class Minifier {
  5. static MinifyList = [
  6. 'CDClientLib',
  7. 'websocket',
  8. 'modules',
  9. 'page',
  10. 'App.js'
  11. ];
  12. static combine() {
  13. const getAllJSFiles = (dirPath, fileList = []) => {
  14. const files = fs.readdirSync(dirPath);
  15. files.forEach(file => {
  16. const filePath = path.join(dirPath, file);
  17. if (fs.statSync(filePath).isDirectory()) {
  18. getAllJSFiles(filePath, fileList);
  19. } else if (filePath.endsWith('.js')) {
  20. fileList.push(filePath);
  21. }
  22. });
  23. return fileList;
  24. };
  25. const mergeFilesWithComments = (fileList) => {
  26. let mergedContent = '';
  27. for (let i = 0; i < fileList.length; i++) {
  28. mergedContent += `\n\n/*! >>> ${fileList[i]} */\n\n`;
  29. const filePath = fileList[i];
  30. const fileContent = fs.readFileSync(filePath, 'utf8');
  31. mergedContent += fileContent;
  32. }
  33. return mergedContent;
  34. }
  35. const allJSFiles = this.MinifyList.reduce((accumulator, currentPath) => {
  36. currentPath = path.join('./build/static/', currentPath);
  37. if (fs.statSync(currentPath).isDirectory()) {
  38. return accumulator.concat(getAllJSFiles(currentPath));
  39. } else if (currentPath.endsWith('.js')) {
  40. return accumulator.concat(currentPath);
  41. }
  42. return accumulator;
  43. }, []);
  44. const mergedContent = mergeFilesWithComments(allJSFiles);
  45. fs.writeFileSync('./build/static/merged_statics.js', mergedContent, { encoding: 'utf8' });
  46. }
  47. static uglify() {
  48. const mangleProperties = {
  49. properties: {
  50. reserved: [
  51. '$', // jQuery
  52. // >>> CodeMirror
  53. 'CodeMirror',
  54. 'cmLineCount',
  55. 'cmEachLine',
  56. 'cmRefresh',
  57. 'cmSetValue',
  58. 'scrollIntoView',
  59. 'markText',
  60. 'getSearchCursor',
  61. 'cmFirstLine',
  62. 'setSelection',
  63. 'cmGetValue',
  64. 'replaceRange',
  65. 'setCursor',
  66. 'cmGetLine',
  67. 'cmFocus',
  68. 'lineNo',
  69. 'onClassLinkClick',
  70. 'onPropertyClick',
  71. 'line',
  72. // <<< CodeMirror
  73. // >>> Server data
  74. 'Class',
  75. 'isAdmin',
  76. 'isEditor',
  77. 'Login',
  78. 'ClassList',
  79. 'ClassSource',
  80. 'RepoNames',
  81. 'Z8Locales',
  82. 'Comments',
  83. 'LastUpdateTime',
  84. 'StatisticsUser',
  85. 'comment',
  86. 'timestamp',
  87. 'nearestParent',
  88. 'nearestParentRoot',
  89. 'type',
  90. 'value',
  91. 'key',
  92. 'inherited',
  93. 'overridden',
  94. 'dynamic',
  95. 'author',
  96. // <<< Server data
  97. // >>> WebSocket
  98. 'onopen',
  99. 'onclose',
  100. 'onerror',
  101. 'onmessage',
  102. 'send'
  103. // <<< WebSocket
  104. ],
  105. keep_quoted: true
  106. },
  107. toplevel: true
  108. }
  109. const uglified = minify_sync(fs.readFileSync('./build/static/merged_statics.js', 'utf8').toString(), { mangle: mangleProperties });
  110. const uglifiedCode = uglified.code.replace(/\/\*\!\s>>>\s(.+)\s\*\/([^\s])/g, '/*! >>> $1 */\n$2');
  111. const uglifiedCodeStrings = uglifiedCode.split('\n');
  112. let fPath = '';
  113. let fContents = {};
  114. for(const str of uglifiedCodeStrings) {
  115. const fnMatch = str.match(/\/\*\!\s>>>\s(.+)\s\*\//);
  116. if(fnMatch) {
  117. fPath = `./${fnMatch[1]}`;
  118. fContents[fPath] = '';
  119. } else {
  120. fContents[fPath] += str + '\n';
  121. }
  122. }
  123. for(const file of Object.keys(fContents)) {
  124. fs.writeFileSync(file, fContents[file], { encoding: 'utf8' });
  125. }
  126. }
  127. static clean() {
  128. fs.rmSync('./build/static/merged_statics.js');
  129. }
  130. static run() {
  131. Minifier.combine();
  132. Minifier.uglify();
  133. Minifier.clean();
  134. }
  135. }
  136. Minifier.run();