uglifyStatics.mjs 3.3 KB

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