SPIFFSUpdate.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { Compiler } from 'webpack';
  2. const path = require('path');
  3. const fs = require('fs');
  4. const zlib = require("zlib");
  5. export = SPIFFSUpdate;
  6. // Define the interface for the plugin options
  7. interface SPIFFSUpdateOptions {
  8. sourceFiles?: string[];
  9. targetFiles?: string[];
  10. compress?: boolean;
  11. }
  12. class SPIFFSUpdate {
  13. private sourceFiles: string[];
  14. private targetFiles: string[];
  15. private compress: boolean;
  16. constructor(options: SPIFFSUpdateOptions) {
  17. this.sourceFiles = options.sourceFiles || []; // Array of proto_path directories
  18. this.targetFiles = options.targetFiles || []; // Array of proto source files or directories
  19. this.compress = options.compress || true; // Output directory
  20. }
  21. apply(compiler: Compiler) {
  22. compiler.hooks.afterEmit.tapAsync('GrpcToolsNodeProtocPlugin', (compilation, callback) => {
  23. try {
  24. fs.appendFileSync('./dist/index.html.gz',
  25. zlib.gzipSync(fs.readFileSync('./dist/index.html'),
  26. {
  27. chunckSize: 65536,
  28. level: zlib.constants.Z_BEST_COMPRESSION
  29. })
  30. );
  31. // if (options.mode !== "production") return;
  32. // let buildRootPath = path.join(process.cwd(), '..', '..', '..');
  33. // let wifiManagerPath = glob.sync(path.join(buildRootPath, 'components/**/wifi-manager*'))[0];
  34. // let buildCRootPath = glob.sync(buildRootPath)[0];
  35. // fs.appendFileSync('./dist/index.html.gz',
  36. // zlib.gzipSync(fs.readFileSync('./dist/index.html'),
  37. // {
  38. // chunckSize: 65536,
  39. // level: zlib.constants.Z_BEST_COMPRESSION
  40. // }));
  41. // var getDirectories = function (src, callback) {
  42. // var searchPath = path.posix.join(src, '/**/*(*.gz|favicon-32x32.png)');
  43. // console.log(`Post build: Getting file list from ${searchPath}`);
  44. // glob(searchPath, callback);
  45. // };
  46. // var cleanUpPath = path.posix.join(buildCRootPath, '/build/*.S');
  47. // console.log(`Post build: Cleaning up previous builds in ${cleanUpPath}`);
  48. // glob(cleanUpPath, function (err, list) {
  49. // if (err) {
  50. // console.error('Error', err);
  51. // } else {
  52. // list.forEach(fileName => {
  53. // try {
  54. // console.log(`Post build: Purging old binary file ${fileName} from C project.`);
  55. // fs.unlinkSync(fileName)
  56. // //file removed
  57. // } catch (ferr) {
  58. // console.error(ferr)
  59. // }
  60. // });
  61. // }
  62. // },
  63. // 'afterEmit'
  64. // );
  65. // console.log('Generating C include files from webpack build output');
  66. // getDirectories('./dist', function (err, list) {
  67. // console.log(`Post build: found ${list.length} files. Relative path: ${wifiManagerPath}.`);
  68. // if (err) {
  69. // console.log('Error', err);
  70. // } else {
  71. // let exportDefHead =
  72. // `/***********************************
  73. // webpack_headers
  74. // ${arguments[1]}
  75. // ***********************************/
  76. // #pragma once
  77. // #include <inttypes.h>
  78. // extern const char * resource_lookups[];
  79. // extern const uint8_t * resource_map_start[];
  80. // extern const uint8_t * resource_map_end[];`;
  81. // let exportDef = '// Automatically generated. Do not edit manually!.\n' +
  82. // '#include <inttypes.h>\n';
  83. // let lookupDef = 'const char * resource_lookups[] = {\n';
  84. // let lookupMapStart = 'const uint8_t * resource_map_start[] = {\n';
  85. // let lookupMapEnd = 'const uint8_t * resource_map_end[] = {\n';
  86. // let cMake = '';
  87. // list.forEach(foundFile => {
  88. // let exportName = path.basename(foundFile).replace(/[\. \-]/gm, '_');
  89. // //take the full path of the file and make it relative to the build directory
  90. // let cmakeFileName = path.posix.relative(wifiManagerPath, glob.sync(path.resolve(foundFile))[0]);
  91. // let httpRelativePath = path.posix.join('/', path.posix.relative('dist', foundFile));
  92. // exportDef += `extern const uint8_t _${exportName}_start[] asm("_binary_${exportName}_start");\nextern const uint8_t _${exportName}_end[] asm("_binary_${exportName}_end");\n`;
  93. // lookupDef += `\t"${httpRelativePath}",\n`;
  94. // lookupMapStart += '\t_' + exportName + '_start,\n';
  95. // lookupMapEnd += '\t_' + exportName + '_end,\n';
  96. // cMake += `target_add_binary_data( __idf_wifi-manager ${cmakeFileName} BINARY)\n`;
  97. // console.log(`Post build: adding cmake file reference to ${cmakeFileName} from C project, with web path ${httpRelativePath}.`);
  98. // });
  99. // lookupDef += '""\n};\n';
  100. // lookupMapStart = lookupMapStart.substring(0, lookupMapStart.length - 2) + '\n};\n';
  101. // lookupMapEnd = lookupMapEnd.substring(0, lookupMapEnd.length - 2) + '\n};\n';
  102. // try {
  103. // fs.writeFileSync('webapp.cmake', cMake);
  104. // fs.writeFileSync('webpack.c', exportDef + lookupDef + lookupMapStart + lookupMapEnd);
  105. // fs.writeFileSync('webpack.h', exportDefHead);
  106. // //file written successfully
  107. // } catch (e) {
  108. // console.error(e);
  109. // }
  110. // }
  111. // });
  112. // console.log('Post build completed.');
  113. // })
  114. } catch (error) {
  115. console.error('Error setting up grpc-tools protoc', error);
  116. }
  117. callback();
  118. });
  119. }
  120. }