Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cannot resolve path when building with EXTERNAL_PATH #93

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions build.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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