Skip to content

Commit 8ea767a

Browse files
authored
fix: cannot resolve path when build with EXTERNAL_PATH (#93)
1 parent 425e14d commit 8ea767a

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

build.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs');
22
const { buildSync } = require('esbuild');
3+
const path = require('path/posix')
34

45
const { EXTERNAL_PATH } = process.env;
56
const MINIFY = !EXTERNAL_PATH;
@@ -22,7 +23,7 @@ buildSync({
2223
},
2324
define: EXTERNAL_PATH ? {
2425
WASM_BINARY: 'undefined',
25-
EXTERNAL_PATH: `'${EXTERNAL_PATH}'`,
26+
EXTERNAL_PATH: `'${path.join(EXTERNAL_PATH, 'lib/lexer.wasm')}'`,
2627
} : {
2728
WASM_BINARY: `'${wasmBuffer.toString('base64')}'`,
2829
EXTERNAL_PATH: 'undefined'
@@ -36,7 +37,7 @@ if (EXTERNAL_PATH) {
3637
let lazy;
3738
async function init () {
3839
if (!lazy) {
39-
lazy = await import(require('node:url').pathToFileURL(require('node:module').createRequire('${EXTERNAL_PATH}/dist/lexer.js').resolve('./lexer.mjs')));
40+
lazy = await import(require('node:url').pathToFileURL(require('node:module').createRequire('${path.join(EXTERNAL_PATH, 'dist/lexer.js')}').resolve('./lexer.mjs')));
4041
}
4142
module.exports = lazy;
4243
return lazy.init();

src/lexer.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@ function copyLE (src, outBuf16) {
9191
}
9292

9393
const loadWasm = (typeof EXTERNAL_PATH === "string" && (async () => {
94-
const { readFile } = await import('node:fs/promises');
95-
const { fileURLToPath } = await import('node:url');
96-
return readFile(fileURLToPath(
97-
import.meta.resolve("undefined/dist/lexer.wasm")
98-
));
94+
return (await import('node:fs/promises')).readFile(EXTERNAL_PATH);
9995
})) || (async () => {
10096
const binary = WASM_BINARY
10197
if (typeof window !== "undefined" && typeof atob === "function") {

0 commit comments

Comments
 (0)