Minor tweak to build script (#19686)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-07-22 09:31:56 +08:00
1 parent 1cfcbbb993
commit 57f6882d2b
2 files changed
+32 -27

No files matched your search

+10 -7
View File
@@ -40,7 +40,7 @@ async function install(version) {
} }
const wasmUrlPattern = const wasmUrlPattern =
/const __wasmUrl = new URL\("(?<wasmFile>.\/[a-z0-9.-]+\.wasm)", import\.meta\.url\)\.href;/; /const __wasmUrl = new URL\("(?<wasmUrl>.\/[a-z0-9.-]+\.wasm)", import\.meta\.url\)\.href;/;
async function buildEntry(directory) { async function buildEntry(directory) {
const packageDirectory = new URL( const packageDirectory = new URL(
"./node_modules/@oxc-parser/binding-wasm32-wasi/", "./node_modules/@oxc-parser/binding-wasm32-wasi/",
@@ -70,15 +70,14 @@ async function buildEntry(directory) {
${text.slice(moduleEnd)} ${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( const wasmBase64String = await fs.readFile(wasmFile, "base64");
new URL(wasmFile, entryFile),
"base64",
);
text = outdent` text = outdent`
import { decode as __decode } from "base64-arraybuffer-es6"; import { decode as __decode } from "base64-arraybuffer-es6";
const __base64ToArrayBuffer = Uint8Array.fromBase64 const __base64ToArrayBuffer = Uint8Array.fromBase64
? (string) => Uint8Array.fromBase64(string).buffer ? (string) => Uint8Array.fromBase64(string).buffer
: __decode; : __decode;
@@ -89,7 +88,11 @@ async function buildEntry(directory) {
text = text.replace(wasmUrlPattern, ""); text = text.replace(wasmUrlPattern, "");
text = text.replace( text = text.replace(
"await fetch(__wasmUrl).then((res) => res.arrayBuffer())", "await fetch(__wasmUrl).then((res) => res.arrayBuffer())",
`/* "${wasmFile}" */ __base64ToArrayBuffer(${JSON.stringify(wasmBase64String)})`, outdent`
__base64ToArrayBuffer(
/* "${wasmUrl}" */ ${JSON.stringify(wasmBase64String)}
)
`,
); );
text = text.replaceAll( text = text.replaceAll(
+22 -20
View File
@@ -1,5 +1,6 @@
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { pathToFileURL } from "node:url";
import { outdent } from "outdent"; import { outdent } from "outdent";
import { DIST_DIR, PACKAGES_DIRECTORY } from "../../utilities/index.js"; import { DIST_DIR, PACKAGES_DIRECTORY } from "../../utilities/index.js";
import { createJavascriptModuleBuilder } from "../builders/javascript-module.js"; import { createJavascriptModuleBuilder } from "../builders/javascript-module.js";
@@ -45,37 +46,38 @@ const mainModule = {
}, },
{ {
module: getPackageFile("@yuku-parser/wasm"), module: getPackageFile("@yuku-parser/wasm"),
async process(text) { async process(text, file) {
const wasmUrlPattern = const wasmUrlPattern =
/const wasmUrl = new URL\("(?<wasmFile>.\/.+\.wasm)", import\.meta\.url\);/; /const wasmUrl = new URL\("(?<wasmUrl>.\/[a-z0-9.-]+\.wasm)", import\.meta\.url\);/;
const { wasmFile } = text.match(wasmUrlPattern).groups; const { wasmUrl } = text.match(wasmUrlPattern).groups;
const wasmBase64String = await fs.readFile( const wasmFile = new URL(wasmUrl, pathToFileURL(file));
getPackageFile(`@yuku-parser/wasm/${wasmFile}`), const wasmBase64String = await fs.readFile(wasmFile, "base64");
"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;
`,
);
text = outdent` text = outdent`
import { decode as __decode } from "base64-arraybuffer-es6"; import { decode as __decode } from "base64-arraybuffer-es6";
const __base64ToArrayBuffer = Uint8Array.fromBase64 const __base64ToArrayBuffer = Uint8Array.fromBase64
? (string) => Uint8Array.fromBase64(string).buffer ? (string) => Uint8Array.fromBase64(string).buffer
: __decode; : __decode;
const wasmBinary = /* "${wasmFile}" */ __base64ToArrayBuffer(${JSON.stringify(wasmBase64String)});
${text} ${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; return text;
}, },
}, },