|
| 1 | +const { readFileSync, writeFileSync } = require('fs'); |
| 2 | +const { resolve, join } = require('path'); |
| 3 | +const { runTransform } = require('esm-to-cjs'); |
| 4 | + |
| 5 | +const pkgPath = resolve(__dirname, '../pkg'); |
| 6 | +const webPkgPath = resolve(__dirname, '../pkg/web'); |
| 7 | + |
| 8 | +const wasmBase64 = readFileSync(join(webPkgPath, 'index_bg.wasm')).toString( |
| 9 | + 'base64' |
| 10 | +); |
| 11 | + |
| 12 | +function base64Decode(input) { |
| 13 | + return Uint8Array.from(atob(input), (m) => m.codePointAt(0)).buffer; |
| 14 | +} |
| 15 | + |
| 16 | +const content = |
| 17 | + readFileSync(join(webPkgPath, 'index.js')) |
| 18 | + .toString() |
| 19 | + .replace(/\nasync function __wbg_load\([\s\S]+?\n}/, '') |
| 20 | + .replace(/\nasync function __wbg_init\([\s\S]+?\n}/, '') |
| 21 | + .replace(/__wbg_init\.[^;]+?;\s+/, '') |
| 22 | + .replace('export { initSync }\n', '') |
| 23 | + .replace('export default __wbg_init;\n', '') + |
| 24 | + `${base64Decode.toString()} |
| 25 | +
|
| 26 | +initSync(base64Decode('${wasmBase64}')); |
| 27 | +`; |
| 28 | + |
| 29 | +writeFileSync( |
| 30 | + join(pkgPath, 'index.browser.js'), |
| 31 | + runTransform(content, { quote: 'single', lenIdentifier: 30 }) |
| 32 | +); |
| 33 | + |
| 34 | +const packageJsonPath = join(pkgPath, 'package.json'); |
| 35 | +const packageJson = JSON.parse(readFileSync(packageJsonPath)); |
| 36 | + |
| 37 | +packageJson.files.push('index.browser.js'); |
| 38 | +packageJson.browser = 'index.browser.js'; |
| 39 | + |
| 40 | +writeFileSync(packageJsonPath, JSON.stringify(packageJson, undefined, 2)); |
0 commit comments