Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(napi/parser): auto download wasm binding on webcontainer
  • Loading branch information
hi-ogawa committed Mar 26, 2025
commit ee4a91820a9cc425c298d53e275f70251b1a8f1d
8 changes: 8 additions & 0 deletions napi/parser/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
}
}

if (!nativeBinding && globalThis.process?.versions?.["webcontainer"]) {
try {
nativeBinding = require('./webcontainer-fallback.js');
} catch (err) {
loadErrors.push(err)
}
}

if (!nativeBinding) {
if (loadErrors.length > 0) {
// TODO Link to documentation with potential fixes
Expand Down
23 changes: 23 additions & 0 deletions napi/parser/webcontainer-fallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('node:fs');
const childProcess = require('node:child_process');

const rolldownPkg = JSON.parse(
fs.readFileSync(require.resolve('oxc-parser/package.json'), 'utf-8'),
);
const version = rolldownPkg.version;
const baseDir = `/tmp/oxc-parser-${version}`;
const bindingEntry = `${baseDir}/node_modules/@oxc-parser/binding-wasm32-wasi/parser.wasi.cjs`;

if (!fs.existsSync(bindingEntry)) {
fs.rmSync(baseDir, { recursive: true, force: true });
fs.mkdirSync(baseDir, { recursive: true });
const bindingPkg = `@oxc-parser/binding-wasm32-wasi@${version}`;
// eslint-disable-next-line: no-console
console.log(`[oxc-parser] Downloading ${bindingPkg} on WebContainer...`);
childProcess.execFileSync('pnpm', ['i', bindingPkg], {
cwd: baseDir,
stdio: 'inherit',
});
}

module.exports = require(bindingEntry);
Loading