finalize.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const { gzipAsync } = require('@gfx/zopfli');
  2. const FS = require('fs');
  3. const path = require('path');
  4. const SAVE_PATH = '../src';
  5. const BUNDLE_JS = FS.readFileSync(path.resolve(__dirname, './dist/js/app.js'));
  6. const INDEX_HTML =
  7. `<!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset='utf-8'>
  11. <meta name='viewport' content='width=device-width,initial-scale=1'>
  12. <title>WebSerial</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>
  16. <div id="app"></div>
  17. <script>
  18. ${BUNDLE_JS}
  19. </script>
  20. </body>
  21. </html>
  22. `;
  23. function chunkArray(myArray, chunk_size){
  24. var index = 0;
  25. var arrayLength = myArray.length;
  26. var 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. let 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. (async function(){
  46. try{
  47. const GZIPPED_INDEX = await gzipAsync(INDEX_HTML, { numiterations: 15 });
  48. const FILE =
  49. `
  50. #ifndef _webserial_webapge_h
  51. #define _webserial_webpage_h
  52. const uint32_t WEBSERIAL_HTML_SIZE = ${GZIPPED_INDEX.length};
  53. const uint8_t WEBSERIAL_HTML[] PROGMEM = {
  54. ${ addLineBreaks(GZIPPED_INDEX) }
  55. };
  56. #endif
  57. `;
  58. FS.writeFileSync(path.resolve(__dirname, SAVE_PATH+'/webserial_webpage.h'), FILE);
  59. console.log(`[COMPRESS.js] Compressed Bundle into webpage.h header file | Total Size: ${(GZIPPED_INDEX.length / 1024).toFixed(2) }KB`)
  60. }catch(err){
  61. return console.error(err);
  62. }
  63. })();