uglifyStatics.mjs 3.3 KB

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