Improve build script for oxc plugin (#18755)
This commit is contained in:
1 parent
a58a070154
commit
48a6e73d5b
2 files changed
+23
-25
No files matched your search
@@ -5,7 +5,6 @@ import spawn from "nano-spawn";
|
||||
import { outdent } from "outdent";
|
||||
import packageJson from "../../../package.json" with { type: "json" };
|
||||
|
||||
const CACHE_VERSION = "v1";
|
||||
const TEMPORARY_DIRECTORY = new URL("../../../.tmp/", import.meta.url);
|
||||
const DIRECTORY_NAME = "prettier-oxc-wasm-parser";
|
||||
const PACKAGE_NAME = "@oxc-parser/binding-wasm32-wasi";
|
||||
@@ -40,16 +39,22 @@ async function install(version) {
|
||||
return directory;
|
||||
}
|
||||
|
||||
const wasmUrlPattern =
|
||||
/var __wasmUrl = new URL\("(?<wasmFile>.\/[a-z0-9.-]+\.wasm)", import\.meta\.url\)\.href;/;
|
||||
async function inlineWasmBinary(directory) {
|
||||
const packageDirectory = new URL(
|
||||
"./node_modules/@oxc-parser/binding-wasm32-wasi/",
|
||||
directory,
|
||||
);
|
||||
const entryFile = new URL("./browser-bundle.js", packageDirectory);
|
||||
const wasmFile = new URL("./parser.wasm32-wasi.wasm", packageDirectory);
|
||||
|
||||
let text = await fs.readFile(entryFile, "utf8");
|
||||
const wasmBase64String = await fs.readFile(wasmFile, "base64");
|
||||
|
||||
const { wasmFile } = text.match(wasmUrlPattern).groups;
|
||||
|
||||
const wasmBase64String = await fs.readFile(
|
||||
new URL(wasmFile, entryFile),
|
||||
"base64",
|
||||
);
|
||||
|
||||
text = outdent`
|
||||
import { decode as __decode } from "base64-arraybuffer-es6";
|
||||
@@ -60,17 +65,18 @@ async function inlineWasmBinary(directory) {
|
||||
${text}
|
||||
`;
|
||||
|
||||
text = text.replace(wasmUrlPattern, "");
|
||||
text = text.replace(
|
||||
"await fetch(__wasmUrl).then((res) => res.arrayBuffer())",
|
||||
`/* "${wasmFile}" */ __base64ToArrayBuffer(${JSON.stringify(wasmBase64String)})`,
|
||||
);
|
||||
|
||||
text = text.replaceAll(
|
||||
/new URL\((?<url>".*?"), import\.meta\.url\)/g,
|
||||
"{/* $<url> */}",
|
||||
);
|
||||
|
||||
text = text.replace(
|
||||
"await fetch(__wasmUrl).then((res) => res.arrayBuffer())",
|
||||
`__base64ToArrayBuffer(${JSON.stringify(wasmBase64String)})`,
|
||||
);
|
||||
|
||||
await fs.writeFile(entryFile, text);
|
||||
return { entry: url.fileURLToPath(entryFile), text, directory };
|
||||
}
|
||||
|
||||
async function buildOxcWasmParser() {
|
||||
@@ -79,26 +85,15 @@ async function buildOxcWasmParser() {
|
||||
`./${DIRECTORY_NAME}@${version}/`,
|
||||
TEMPORARY_DIRECTORY,
|
||||
);
|
||||
const entry = new URL(`./index-${CACHE_VERSION}.mjs`, directory);
|
||||
|
||||
if (!existsSync(entry)) {
|
||||
if (!existsSync(directory)) {
|
||||
await fs.rm(directory, { recursive: true, force: true });
|
||||
|
||||
const installDirectory = await install(version);
|
||||
await fs.rename(installDirectory, directory);
|
||||
|
||||
await fs.writeFile(
|
||||
entry,
|
||||
`export {parseSync} from '${PACKAGE_NAME}/browser-bundle.js'`,
|
||||
);
|
||||
|
||||
await inlineWasmBinary(directory);
|
||||
}
|
||||
|
||||
return {
|
||||
entry: url.fileURLToPath(entry),
|
||||
directory: url.fileURLToPath(directory),
|
||||
};
|
||||
return await inlineWasmBinary(directory);
|
||||
}
|
||||
|
||||
export default buildOxcWasmParser;
|
||||
@@ -21,7 +21,6 @@ const mainModule = {
|
||||
{
|
||||
input: "index.js",
|
||||
output: "index.mjs",
|
||||
prepare: buildOxcWasmParser,
|
||||
build: createJavascriptModuleBuilder({
|
||||
input: "index.js",
|
||||
output: "index.mjs",
|
||||
@@ -35,7 +34,7 @@ const mainModule = {
|
||||
input: "index.js",
|
||||
output: "index.browser.mjs",
|
||||
build: createJavascriptModuleBuilder(async () => {
|
||||
const { entry } = await buildOxcWasmParser();
|
||||
const { entry, text } = await buildOxcWasmParser();
|
||||
return {
|
||||
format: "esm",
|
||||
platform: "universal",
|
||||
@@ -45,6 +44,10 @@ const mainModule = {
|
||||
module: getPackageFile("oxc-parser/src-js/wasm.js"),
|
||||
path: entry,
|
||||
},
|
||||
{
|
||||
module: entry,
|
||||
text,
|
||||
},
|
||||
],
|
||||
allowedWarnings: ["indirect-require", "package.json"],
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user