buildWasm.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * This script is used to convert the wasm modules into js modules, with the binary converted into base64 encoded strings.
  3. */
  4. const fs = require("fs");
  5. const path = require("path");
  6. const wasmModules = [
  7. {
  8. pkg: `../node_modules/fonteditor-core`,
  9. src: `./wasm/woff2.wasm`,
  10. dest: `../packages/excalidraw/fonts/wasm/woff2-wasm.ts`,
  11. },
  12. {
  13. pkg: `../node_modules/harfbuzzjs`,
  14. src: `./wasm/hb-subset.wasm`,
  15. dest: `../packages/excalidraw/fonts/wasm/hb-subset-wasm.ts`,
  16. },
  17. ];
  18. for (const { pkg, src, dest } of wasmModules) {
  19. const packagePath = path.resolve(__dirname, pkg, "package.json");
  20. const licensePath = path.resolve(__dirname, pkg, "LICENSE");
  21. const sourcePath = path.resolve(__dirname, src);
  22. const destPath = path.resolve(__dirname, dest);
  23. const {
  24. name,
  25. version,
  26. author,
  27. license,
  28. authors,
  29. licenses,
  30. } = require(packagePath);
  31. const licenseContent = fs.readFileSync(licensePath, "utf-8") || "";
  32. const base64 = fs.readFileSync(sourcePath, "base64");
  33. const content = `// GENERATED CODE -- DO NOT EDIT!
  34. /* eslint-disable */
  35. // @ts-nocheck
  36. /**
  37. * The following wasm module is generated with \`scripts/buildWasm.js\` and encoded as base64.
  38. *
  39. * The source of this content is taken from the package "${name}", which contains the following metadata:
  40. *
  41. * @author ${author || JSON.stringify(authors)}
  42. * @license ${license || JSON.stringify(licenses)}
  43. * @version ${version}
  44. ${licenseContent}
  45. */
  46. // faster atob alternative - https://github.com/evanw/esbuild/issues/1534#issuecomment-902738399
  47. const __toBinary = /* @__PURE__ */ (() => {
  48. const table = new Uint8Array(128);
  49. for (let i = 0; i < 64; i++)
  50. {table[i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i * 4 - 205] = i;}
  51. return (base64) => {
  52. const n = base64.length; const bytes = new Uint8Array((n - (base64[n - 1] == "=") - (base64[n - 2] == "=")) * 3 / 4 | 0);
  53. for (let i2 = 0, j = 0; i2 < n; ) {
  54. const c0 = table[base64.charCodeAt(i2++)]; const c1 = table[base64.charCodeAt(i2++)];
  55. const c2 = table[base64.charCodeAt(i2++)]; const c3 = table[base64.charCodeAt(i2++)];
  56. bytes[j++] = c0 << 2 | c1 >> 4;
  57. bytes[j++] = c1 << 4 | c2 >> 2;
  58. bytes[j++] = c2 << 6 | c3;
  59. }
  60. return bytes;
  61. };
  62. })();
  63. export default __toBinary(\`${base64}\`);
  64. `;
  65. fs.writeFileSync(destPath, content);
  66. }