Minor tweak to build script (#19686)
This commit is contained in:
1 parent
1cfcbbb993
commit
57f6882d2b
2 files changed
+32
-27
No files matched your search
@@ -40,7 +40,7 @@ async function install(version) {
|
||||
}
|
||||
|
||||
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) {
|
||||
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(
|
||||
|
||||
@@ -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\("(?<wasmFile>.\/.+\.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\("(?<wasmUrl>.\/[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;
|
||||
},
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user