|
| 1 | +import { initSync, parse as __wbg_parse } from "./pkg/cjs-module-lexer.js"; |
| 2 | +const wasmPath = "./pkg/cjs-module-lexer_bg.wasm"; |
| 3 | + |
| 4 | +let wasm; |
| 5 | +if (globalThis.process || globalThis.Deno || globalThis.Bun) { |
| 6 | + const { readFileSync } = await import("node:fs"); |
| 7 | + const url = new URL(wasmPath, import.meta.url); |
| 8 | + const wasmData = readFileSync(url.pathname); |
| 9 | + wasm = await WebAssembly.compile(wasmData); |
| 10 | +} else { |
| 11 | + const url = new URL(wasmPath, import.meta.url); |
| 12 | + const pkgPrefix = "/@esm.sh/cjs-module-lexer@"; |
| 13 | + if (url.pathname.startsWith(pkgPrefix)) { |
| 14 | + const version = url.pathname.slice(pkgPrefix.length).split("/", 1)[0]; |
| 15 | + url.pathname = pkgPrefix + version + wasmPath.slice(1); |
| 16 | + } |
| 17 | + const res = await fetch(url); |
| 18 | + if (!res.ok) { |
| 19 | + throw new Error(`failed to fetch wasm: ${res.statusText}`); |
| 20 | + } |
| 21 | + wasm = await WebAssembly.compileStreaming(res); |
| 22 | +} |
| 23 | + |
| 24 | +initSync({ module: wasm }); |
| 25 | + |
| 26 | +/** |
| 27 | + * parse the given code and return the exports and reexports |
| 28 | + * @param {string} filename |
| 29 | + * @param {string} code |
| 30 | + * @param {{ nodeEnv?: 'development' | 'production', callMode?: boolean }} options |
| 31 | + * @returns {{ exports: string[], reexports: string[] }} |
| 32 | + */ |
| 33 | +export function parse(filename, code, options = {}) { |
| 34 | + return __wbg_parse(filename, code, options); |
| 35 | +} |
0 commit comments