BuildEventsHook.js 4.9 KB

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