buildWasm.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 { name, version, author, license, authors, licenses } = require(
  24. packagePath,
  25. );
  26. const licenseContent = fs.readFileSync(licensePath, "utf-8") || "";
  27. const base64 = fs.readFileSync(sourcePath, "base64");
  28. const content = `// GENERATED CODE -- DO NOT EDIT!
  29. /* eslint-disable */
  30. // @ts-nocheck
  31. /**
  32. * The following wasm module is generated with \`scripts/buildWasm.js\` and encoded as base64.
  33. *
  34. * The source of this content is taken from the package "${name}", which contains the following metadata:
  35. *
  36. * @author ${author || JSON.stringify(authors)}
  37. * @license ${license || JSON.stringify(licenses)}
  38. * @version ${version}
  39. ${licenseContent}
  40. */
  41. // faster atob alternative - https://github.com/evanw/esbuild/issues/1534#issuecomment-902738399
  42. const __toBinary = /* @__PURE__ */ (() => {
  43. const table = new Uint8Array(128);
  44. for (let i = 0; i < 64; i++)
  45. {table[i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i * 4 - 205] = i;}
  46. return (base64) => {
  47. const n = base64.length; const bytes = new Uint8Array((n - (base64[n - 1] == "=") - (base64[n - 2] == "=")) * 3 / 4 | 0);
  48. for (let i2 = 0, j = 0; i2 < n; ) {
  49. const c0 = table[base64.charCodeAt(i2++)]; const c1 = table[base64.charCodeAt(i2++)];
  50. const c2 = table[base64.charCodeAt(i2++)]; const c3 = table[base64.charCodeAt(i2++)];
  51. bytes[j++] = c0 << 2 | c1 >> 4;
  52. bytes[j++] = c1 << 4 | c2 >> 2;
  53. bytes[j++] = c2 << 6 | c3;
  54. }
  55. return bytes;
  56. };
  57. })();
  58. export default __toBinary(\`${base64}\`);
  59. `;
  60. fs.writeFileSync(destPath, content);
  61. }