Skip to content

Commit

Permalink
fix: cannot resolve path when build with EXTERNAL_PATH (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
mochaaP committed Apr 28, 2024
1 parent 425e14d commit 8ea767a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
5 changes: 3 additions & 2 deletions build.js
@@ -1,5 +1,6 @@
const fs = require('fs');
const { buildSync } = require('esbuild');
const path = require('path/posix')

const { EXTERNAL_PATH } = process.env;
const MINIFY = !EXTERNAL_PATH;
Expand All @@ -22,7 +23,7 @@ buildSync({
},
define: EXTERNAL_PATH ? {
WASM_BINARY: 'undefined',
EXTERNAL_PATH: `'${EXTERNAL_PATH}'`,
EXTERNAL_PATH: `'${path.join(EXTERNAL_PATH, 'lib/lexer.wasm')}'`,
} : {
WASM_BINARY: `'${wasmBuffer.toString('base64')}'`,
EXTERNAL_PATH: 'undefined'
Expand All @@ -36,7 +37,7 @@ if (EXTERNAL_PATH) {
let lazy;
async function init () {
if (!lazy) {
lazy = await import(require('node:url').pathToFileURL(require('node:module').createRequire('${EXTERNAL_PATH}/dist/lexer.js').resolve('./lexer.mjs')));
lazy = await import(require('node:url').pathToFileURL(require('node:module').createRequire('${path.join(EXTERNAL_PATH, 'dist/lexer.js')}').resolve('./lexer.mjs')));
}
module.exports = lazy;
return lazy.init();
Expand Down
6 changes: 1 addition & 5 deletions src/lexer.js
Expand Up @@ -91,11 +91,7 @@ function copyLE (src, outBuf16) {
}

const loadWasm = (typeof EXTERNAL_PATH === "string" && (async () => {
const { readFile } = await import('node:fs/promises');
const { fileURLToPath } = await import('node:url');
return readFile(fileURLToPath(
import.meta.resolve("undefined/dist/lexer.wasm")
));
return (await import('node:fs/promises')).readFile(EXTERNAL_PATH);
})) || (async () => {
const binary = WASM_BINARY
if (typeof window !== "undefined" && typeof atob === "function") {
Expand Down

0 comments on commit 8ea767a

Please sign in to comment.