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