Skip to content

Commit

Permalink
fix: support imports of absolute paths of ESM files on Windows (#8669)
Browse files Browse the repository at this point in the history
Closes #8651
  • Loading branch information
giladgd committed Feb 18, 2022
1 parent 6713a42 commit 12cbfcd
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/util/ImportUtils.ts
@@ -1,11 +1,14 @@
import fs from "fs";
import path from "path";
import {pathToFileURL} from "url";

export async function importOrRequireFile(filePath: string): Promise<[result: any, moduleType: "esm" | "commonjs"]> {
const tryToImport = async (): Promise<[any, "esm"]> => {
// `Function` is required to make sure the `import` statement wil stay `import` after
// transpilation and won't be converted to `require`
return [await Function("return filePath => import(filePath)")()(filePath), "esm"];
return [await Function("return filePath => import(filePath)")()(
filePath.startsWith("file://") ? filePath : pathToFileURL(filePath).toString()
), "esm"];
};
const tryToRequire = async (): Promise<[any, "commonjs"]> => {
return [require(filePath), "commonjs"];
Expand Down

0 comments on commit 12cbfcd

Please sign in to comment.