uglifyStatics.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // <<< CodeMirror
  72. // >>> Server data
  73. 'Class',
  74. 'isAdmin',
  75. 'isEditor',
  76. 'Login',
  77. 'ClassList',
  78. 'ClassSource',
  79. 'RepoNames',
  80. 'Z8Locales',
  81. 'Comments',
  82. 'LastUpdateTime',
  83. 'StatisticsUser',
  84. 'comment',
  85. 'timestamp',
  86. 'nearestParent',
  87. 'nearestParentRoot',
  88. 'type',
  89. 'value',
  90. 'key',
  91. 'inherited',
  92. 'overridden',
  93. 'dynamic',
  94. 'author',
  95. // <<< Server data
  96. // >>> WebSocket
  97. 'onopen',
  98. 'onclose',
  99. 'onerror',
  100. 'onmessage',
  101. 'send'
  102. // <<< WebSocket
  103. ],
  104. keep_quoted: true
  105. },
  106. toplevel: true
  107. }
  108. const uglified = minify_sync(fs.readFileSync('./build/static/merged_statics.js', 'utf8').toString(), { mangle: mangleProperties });
  109. const uglifiedCode = uglified.code.replace(/\/\*\!\s>>>\s(.+)\s\*\/([^\s])/g, '/*! >>> $1 */\n$2');
  110. const uglifiedCodeStrings = uglifiedCode.split('\n');
  111. let fPath = '';
  112. let fContents = {};
  113. for(const str of uglifiedCodeStrings) {
  114. const fnMatch = str.match(/\/\*\!\s>>>\s(.+)\s\*\//);
  115. if(fnMatch) {
  116. fPath = `./${fnMatch[1]}`;
  117. fContents[fPath] = '';
  118. } else {
  119. fContents[fPath] += str + '\n';
  120. }
  121. }
  122. for(const file of Object.keys(fContents)) {
  123. fs.writeFileSync(file, fContents[file], { encoding: 'utf8' });
  124. }
  125. }
  126. static clean() {
  127. fs.rmSync('./build/static/merged_statics.js');
  128. }
  129. static run() {
  130. Minifier.combine();
  131. Minifier.uglify();
  132. Minifier.clean();
  133. }
  134. }
  135. Minifier.run();