Skip to content

Commit

Permalink
Fix for importing createRequire in some environments (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis authored Apr 5, 2024
1 parent a889520 commit 68a1f7d
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/pglite/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,27 @@ export async function nodeValues() {
let dirname: string | undefined = undefined;
let require: ((id: string) => any) | undefined = undefined;
if (IN_NODE) {
const module = await import("module");
// In some environments importing 'module' doesn't have a 'default' property and
// createRequire is on the top level of the import.
// This is a workaround for that.
// See https://github.com/electric-sql/pglite/issues/71
const createRequire =
module.default?.createRequire ??
((module as any)
.createRequire as (typeof module.default)["createRequire"]);
require = createRequire(import.meta.url);
dirname = (await import("path")).dirname(import.meta.url);
require = (await import("module")).default.createRequire(import.meta.url);
}
return { dirname, require };
}


export async function makeLocateFile() {
const PGWASM_URL = new URL("../release/postgres.wasm", import.meta.url);
const PGSHARE_URL = new URL("../release/share.data", import.meta.url);
let fileURLToPath = (fileUrl: URL) => fileUrl.pathname
let fileURLToPath = (fileUrl: URL) => fileUrl.pathname;
if (IN_NODE) {
fileURLToPath = (await import("url")).fileURLToPath
fileURLToPath = (await import("url")).fileURLToPath;
}
return (base: string) => {
let url: URL | null = null;
Expand All @@ -32,10 +40,10 @@ export async function makeLocateFile() {
break;
default:
}

if (url?.protocol === "file:") {
return fileURLToPath(url);
}
return url?.toString() ?? '';
}
}
return url?.toString() ?? "";
};
}

0 comments on commit 68a1f7d

Please sign in to comment.