compress.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const { gzip } = require('@gfx/zopfli');
  2. const FS = require('fs');
  3. const path = require('path');
  4. const BUNDLE_JS = FS.readFileSync(path.resolve(__dirname, './dist/js/app.js'));
  5. const HTML = `
  6. <!DOCTYPE html>
  7. <html lang=en>
  8. <head>
  9. <meta charset=utf-8>
  10. <meta http-equiv=X-UA-Compatible content="IE=edge">
  11. <meta name=viewport content="width=device-width,initial-scale=1">
  12. <link rel=icon href=/favicon.ico> <title>ElegantOTA</title>
  13. </head>
  14. <body style="overflow: hidden;">
  15. <noscript>
  16. <strong>We're sorry but ElegantOTA doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
  17. </noscript>
  18. <div id=app></div>
  19. <script defer>${BUNDLE_JS}</script>
  20. </body>
  21. </html>
  22. `;
  23. function chunkArray(myArray, chunk_size) {
  24. let index = 0;
  25. const arrayLength = myArray.length;
  26. const tempArray = [];
  27. for (index = 0; index < arrayLength; index += chunk_size) {
  28. myChunk = myArray.slice(index, index + chunk_size);
  29. // Do something if you want with the group
  30. tempArray.push(myChunk);
  31. }
  32. return tempArray;
  33. }
  34. function addLineBreaks(buffer) {
  35. let data = '';
  36. const chunks = chunkArray(buffer, 30);
  37. chunks.forEach((chunk, index) => {
  38. data += chunk.join(',');
  39. if (index + 1 !== chunks.length) {
  40. data += ',\n';
  41. }
  42. });
  43. return data;
  44. }
  45. gzip(HTML, { numiterations: 15 }, (err, output) => {
  46. if (err) {
  47. return console.error(err);
  48. }
  49. const FILE = `#ifndef ElegantOTAWebpage_h
  50. #define ElegantOTAWebpage_h
  51. const uint32_t ELEGANT_HTML_SIZE = ${output.length};
  52. const uint8_t ELEGANT_HTML[] PROGMEM = {
  53. ${addLineBreaks(output)}
  54. };
  55. #endif
  56. `;
  57. FS.writeFileSync(path.resolve(__dirname, '../src/elegantWebpage.h'), FILE);
  58. console.log(`[COMPRESS] Compressed Build Files to elegantWebpage.h: ${ (output.length/1024).toFixed(2) }KB`);
  59. });