diff --git a/scripts/build/hacks/build-oxc-wasm-parser.js b/scripts/build/hacks/build-oxc-wasm-parser.js index 902f101f3..9c9d5d258 100644 --- a/scripts/build/hacks/build-oxc-wasm-parser.js +++ b/scripts/build/hacks/build-oxc-wasm-parser.js @@ -40,7 +40,7 @@ async function install(version) { } const wasmUrlPattern = - /const __wasmUrl = new URL\("(?.\/[a-z0-9.-]+\.wasm)", import\.meta\.url\)\.href;/; + /const __wasmUrl = new URL\("(?.\/[a-z0-9.-]+\.wasm)", import\.meta\.url\)\.href;/; async function buildEntry(directory) { const packageDirectory = new URL( "./node_modules/@oxc-parser/binding-wasm32-wasi/", @@ -70,15 +70,14 @@ async function buildEntry(directory) { ${text.slice(moduleEnd)} `; - const { wasmFile } = text.match(wasmUrlPattern).groups; + const { wasmUrl } = text.match(wasmUrlPattern).groups; + const wasmFile = new URL(wasmUrl, entryFile); - const wasmBase64String = await fs.readFile( - new URL(wasmFile, entryFile), - "base64", - ); + const wasmBase64String = await fs.readFile(wasmFile, "base64"); text = outdent` import { decode as __decode } from "base64-arraybuffer-es6"; + const __base64ToArrayBuffer = Uint8Array.fromBase64 ? (string) => Uint8Array.fromBase64(string).buffer : __decode; @@ -89,7 +88,11 @@ async function buildEntry(directory) { text = text.replace(wasmUrlPattern, ""); text = text.replace( "await fetch(__wasmUrl).then((res) => res.arrayBuffer())", - `/* "${wasmFile}" */ __base64ToArrayBuffer(${JSON.stringify(wasmBase64String)})`, + outdent` + __base64ToArrayBuffer( + /* "${wasmUrl}" */ ${JSON.stringify(wasmBase64String)} + ) + `, ); text = text.replaceAll( diff --git a/scripts/build/packages/plugin-yuku.js b/scripts/build/packages/plugin-yuku.js index 6c7c3c040..d70060c3b 100644 --- a/scripts/build/packages/plugin-yuku.js +++ b/scripts/build/packages/plugin-yuku.js @@ -1,5 +1,6 @@ import fs from "node:fs/promises"; import path from "node:path"; +import { pathToFileURL } from "node:url"; import { outdent } from "outdent"; import { DIST_DIR, PACKAGES_DIRECTORY } from "../../utilities/index.js"; import { createJavascriptModuleBuilder } from "../builders/javascript-module.js"; @@ -45,37 +46,38 @@ const mainModule = { }, { module: getPackageFile("@yuku-parser/wasm"), - async process(text) { + async process(text, file) { const wasmUrlPattern = - /const wasmUrl = new URL\("(?.\/.+\.wasm)", import\.meta\.url\);/; - const { wasmFile } = text.match(wasmUrlPattern).groups; - const wasmBase64String = await fs.readFile( - getPackageFile(`@yuku-parser/wasm/${wasmFile}`), - "base64", - ); - - text = text.replace('import("node:fs/promises")', "whatever"); - text = text.replace(wasmUrlPattern, ""); - - text = text.replace( - "const { memory, alloc, free, parse: wasmParse } = (await instantiate()).exports;", - outdent` - const module = new WebAssembly.Module((wasmBinary)); - const instance = new WebAssembly.Instance(module); - const { memory, alloc, free, parse: wasmParse } = instance.exports; - `, - ); + /const wasmUrl = new URL\("(?.\/[a-z0-9.-]+\.wasm)", import\.meta\.url\);/; + const { wasmUrl } = text.match(wasmUrlPattern).groups; + const wasmFile = new URL(wasmUrl, pathToFileURL(file)); + const wasmBase64String = await fs.readFile(wasmFile, "base64"); text = outdent` import { decode as __decode } from "base64-arraybuffer-es6"; + const __base64ToArrayBuffer = Uint8Array.fromBase64 ? (string) => Uint8Array.fromBase64(string).buffer : __decode; - const wasmBinary = /* "${wasmFile}" */ __base64ToArrayBuffer(${JSON.stringify(wasmBase64String)}); ${text} `; + text = text.replace('import("node:fs/promises")', "whatever"); + text = text.replace(wasmUrlPattern, ""); + text = text.replace( + "(await instantiate())", + outdent` + new WebAssembly.Instance( + new WebAssembly.Module( + __base64ToArrayBuffer( + /* "${wasmUrl}" */ ${JSON.stringify(wasmBase64String)} + ) + ) + ) + `, + ); + return text; }, },