fix(core): load config util supports absolute paths on windows (#22837)

This commit is contained in:
Colum Ferry 2024-04-17 16:32:09 +01:00 committed by GitHub
parent da1808d36c
commit 82145e7d02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,7 @@
import { dirname, extname, join } from 'path'; import { dirname, extname, join } from 'path';
import { existsSync, readdirSync } from 'fs'; import { existsSync, readdirSync } from 'fs';
import { requireNx } from '../../nx'; import { requireNx } from '../../nx';
import { pathToFileURL } from 'node:url';
const { workspaceRoot, registerTsProject } = requireNx(); const { workspaceRoot, registerTsProject } = requireNx();
@ -70,8 +71,9 @@ async function load(path: string): Promise<any> {
return require(path); return require(path);
} catch (e: any) { } catch (e: any) {
if (e.code === 'ERR_REQUIRE_ESM') { if (e.code === 'ERR_REQUIRE_ESM') {
// If `require` fails to load ESM, try dynamic `import()`. // If `require` fails to load ESM, try dynamic `import()`. ESM requires file url protocol for handling absolute paths.
return await dynamicImport(`${path}?t=${Date.now()}`); const pathAsFileUrl = pathToFileURL(path).pathname;
return await dynamicImport(`${pathAsFileUrl}?t=${Date.now()}`);
} }
// Re-throw all other errors // Re-throw all other errors