Skip to content

Commit 5da4812

Browse files
committed
refactor: wip
1 parent 9e799f4 commit 5da4812

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/utils/src/lib/import-module.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createJiti as createJitiSource } from 'jiti';
22
import { stat } from 'node:fs/promises';
33
import path from 'node:path';
4+
import { pathToFileURL } from 'node:url';
45
import type { CompilerOptions } from 'typescript';
56
import { fileExists } from './file-system.js';
67
import { loadTargetConfig } from './load-ts-config.js';
@@ -25,6 +26,11 @@ export type ImportModuleOptions = JitiOptions & {
2526
filepath: string;
2627
tsconfig?: string;
2728
};
29+
30+
export function toFileUrl(filepath: string): string {
31+
return pathToFileURL(filepath).href;
32+
}
33+
2834
export async function importModule<T = unknown>(
2935
options: ImportModuleOptions,
3036
): Promise<T> {
@@ -50,7 +56,9 @@ export async function importModule<T = unknown>(
5056
tsconfigPath: tsconfig,
5157
});
5258

53-
return (await jitiInstance.import(absoluteFilePath, { default: true })) as T;
59+
return (await jitiInstance.import(toFileUrl(absoluteFilePath), {
60+
default: true,
61+
})) as T;
5462
}
5563

5664
/**

packages/utils/src/lib/import-module.unit.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import path from 'node:path';
2+
import { pathToFileURL } from 'node:url';
13
import type { CompilerOptions } from 'typescript';
24
import { describe, expect, it } from 'vitest';
35
import {
46
mapTsPathsToJitiAlias,
57
parseTsConfigToJitiConfig,
8+
toFileUrl,
69
} from './import-module.js';
710

811
describe('mapTsPathsToJitiAlias', () => {
@@ -107,3 +110,15 @@ describe('parseTsConfigToJitiConfig', () => {
107110
});
108111
});
109112
});
113+
114+
describe('toFileUrl', () => {
115+
it('returns a file:// URL for an absolute path', () => {
116+
const absolutePath = path.resolve('some', 'config.ts');
117+
expect(toFileUrl(absolutePath)).toBe(pathToFileURL(absolutePath).href);
118+
});
119+
120+
it('normalizes Windows absolute paths to file URLs', () => {
121+
const windowsPath = path.win32.join('C:\\', 'Users', 'me', 'config.ts');
122+
expect(toFileUrl(windowsPath)).toBe('file:///C:/Users/me/config.ts');
123+
});
124+
});

0 commit comments

Comments
 (0)