post_process.cjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const fs = require('fs');
  2. const path = require('path');
  3. const { exec }= require("child_process");
  4. const directoryPath = path.resolve(__dirname, 'build');
  5. function postProcess() {
  6. fs.readdir(directoryPath, (err, files) => {
  7. if (err) {
  8. console.error("Error reading directory: ", err);
  9. return;
  10. }
  11. processHtmlFiles(files);
  12. //
  13. // wanted to take it out of the readdir callback but it messes up print orders
  14. //
  15. appendToCxcore();
  16. console.log("---Finished post-processing---");
  17. });
  18. }
  19. function processHtmlFiles(files) {
  20. console.log();
  21. console.log(`---Starting post-processing---`);
  22. console.log();
  23. const htmlFiles = files.filter(file => file.endsWith('html'));
  24. htmlFiles.forEach(htmlFile => {
  25. console.log( '-' + htmlFile);
  26. const filePath = path.join(directoryPath, htmlFile);
  27. const fileContent = fs.readFileSync(filePath, 'utf-8');
  28. const scriptContent = extractScript(fileContent);
  29. if (scriptContent === '') {
  30. if (htmlFile === 'login.html') {
  31. console.log(`\tAll good! No expected script for ${filePath}, skipped`);
  32. return;
  33. }
  34. console.warn(`\tWarning: no script content for ${filePath}`);
  35. return;
  36. }
  37. const scriptName = createJsFile(htmlFile, scriptContent);
  38. if (scriptName === '') {
  39. console.warn(`\tWarning: no scriptName for ${filePath}`);
  40. return;
  41. }
  42. replaceScript(fileContent, filePath, scriptName);
  43. console.log(`\tSuccessfully extracted scripts from [${htmlFile}] into separate [${scriptName}].`);
  44. });
  45. console.log();
  46. console.log("---Finished post-processing HTML files---");
  47. }
  48. function appendToCxcore() {
  49. const cxcoreFile = path.resolve(directoryPath, 'cxcore.js');
  50. const content = 'cxCoreInit.promise.then(function(){cxCoreInit();}).catch(function(e){postMessage({type:", m, ",value:e.toString()});})';
  51. try {
  52. fs.appendFileSync(cxcoreFile, content);
  53. console.log(`\nappended cxCoreInit() to build/cxcore.js`);
  54. } catch (err) {
  55. console.error("Failed to append: ", err);
  56. }
  57. }
  58. function extractScript(fileContent) {
  59. const scriptStart = fileContent.indexOf('__sveltekit_');
  60. if (scriptStart === -1) {
  61. console.log('\tCould not find __sveltekit_ script start');
  62. return '';
  63. }
  64. var scriptEnd = fileContent.lastIndexOf('</script>');
  65. scriptEnd -= 6; // getting rid of extra } closing bracket. Will find a better solution than this.
  66. if (scriptEnd === -1) {
  67. console.log('\tCould not match the closing <script> tag');
  68. return '';
  69. }
  70. const scriptContent = fileContent.substring(scriptStart, scriptEnd);
  71. const containsPromiseAll = scriptContent.indexOf('Promise.all');
  72. if (containsPromiseAll !== -1) {
  73. return scriptContent;
  74. } else {
  75. console.log('\tCould not find Promise.all call in generated __sveltekit_ script');
  76. return '';
  77. }
  78. }
  79. function createJsFile(ogFileName, fileContent) {
  80. const name = path.parse(ogFileName).name;
  81. const filename = name + '_extracted_script.js'
  82. fs.writeFileSync(directoryPath + '/' + filename, fileContent, (err) => {
  83. if (err) {
  84. console.log("Error writing extracted script into .js file: ", err);
  85. return '';
  86. }
  87. });
  88. return filename;
  89. }
  90. function replaceScript(fileContent, filePath, scriptName) {
  91. const updatedContent = fileContent.replace(/<script>[\s\S]*?<\/script>/, `<script src="${scriptName}" defer></script>`);
  92. fs.writeFileSync(filePath, updatedContent, 'utf-8', (err) =>{
  93. if (err) {
  94. console.log("Failed to write replacedContent instead of inlined script: ", err);
  95. }
  96. });
  97. }
  98. postProcess();