compress.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. <script data-name="BMC-Widget" async src="https://cdnjs.buymeacoffee.com/1.0.0/widget.prod.min.js" data-id="6QGVpSj" data-description="Support me on Buy me a coffee!" data-message="You can always support my work by buying me a coffee!" data-color="#FF813F" data-position="right" data-x_margin="24" data-y_margin="24"></script>
  14. </head>
  15. <body style="overflow: hidden;">
  16. <noscript>
  17. <strong>We're sorry but ElegantOTA doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
  18. </noscript>
  19. <div id=app></div>
  20. <script defer>${BUNDLE_JS}</script>
  21. </body>
  22. </html>
  23. `;
  24. function chunkArray(myArray, chunk_size) {
  25. let index = 0;
  26. const arrayLength = myArray.length;
  27. const tempArray = [];
  28. for (index = 0; index < arrayLength; index += chunk_size) {
  29. myChunk = myArray.slice(index, index + chunk_size);
  30. // Do something if you want with the group
  31. tempArray.push(myChunk);
  32. }
  33. return tempArray;
  34. }
  35. function addLineBreaks(buffer) {
  36. let data = '';
  37. const chunks = chunkArray(buffer, 30);
  38. chunks.forEach((chunk, index) => {
  39. data += chunk.join(',');
  40. if (index + 1 !== chunks.length) {
  41. data += ',\n';
  42. }
  43. });
  44. return data;
  45. }
  46. gzip(HTML, { numiterations: 15 }, (err, output) => {
  47. if (err) {
  48. return console.error(err);
  49. }
  50. const FILE = `#ifndef ElegantOTAWebpage_h
  51. #define ElegantOTAWebpage_h
  52. const uint32_t ELEGANT_HTML_SIZE = ${output.length};
  53. const uint8_t ELEGANT_HTML[] PROGMEM = {
  54. ${addLineBreaks(output)}
  55. };
  56. #endif
  57. `;
  58. FS.writeFileSync(path.resolve(__dirname, '../src/elegantWebpage.h'), FILE);
  59. console.log(`[COMPRESS] Compressed Build Files to elegantWebpage.h: ${ (output.length/1024).toFixed(2) }KB`);
  60. });