Skip to content

Commit 4a8a2dc

Browse files
committed
fix(cjs): support file url (#18)
1 parent 942e9bc commit 4a8a2dc

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
lines changed

src/cjs/api/module-resolve-filename.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import path from 'node:path';
22
import Module from 'node:module';
3+
import { fileURLToPath } from 'node:url';
34
import { createPathsMatcher } from 'get-tsconfig';
45
import { resolveTsPath } from '../../utils/resolve-ts-path.js';
56
import type { NodeError } from '../../types.js';
67
import { isRelativePath } from '../../utils/path-utils.js';
8+
import { fileUrlPrefix } from '../../utils/file-url.js';
79
import {
810
isTsFilePatten,
911
tsconfig,
@@ -69,6 +71,11 @@ export const resolveFilename: ResolveFilename = (
6971
request = request.slice(0, queryIndex);
7072
}
7173

74+
// Support file protocol
75+
if (request.startsWith(fileUrlPrefix)) {
76+
request = fileURLToPath(request);
77+
}
78+
7279
if (
7380
tsconfigPathsMatcher
7481

src/esm/hook/resolve.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import type {
66
import { resolveTsPath } from '../../utils/resolve-ts-path.js';
77
import type { NodeError } from '../../types.js';
88
import { requestAcceptsQuery } from '../../utils/path-utils.js';
9+
import { fileUrlPrefix } from '../../utils/file-url.js';
910
import {
1011
tsconfigPathsMatcher,
1112
tsExtensionsPattern,
1213
getFormatFromFileUrl,
13-
fileProtocol,
1414
allowJs,
1515
namespaceQuery,
1616
getNamespace,
@@ -41,7 +41,7 @@ const resolveExplicitPath = async (
4141

4242
if (
4343
!resolved.format
44-
&& resolved.url.startsWith(fileProtocol)
44+
&& resolved.url.startsWith(fileUrlPrefix)
4545
) {
4646
resolved.format = await getFormatFromFileUrl(resolved.url);
4747
}

src/esm/hook/utils.ts

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ export const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
2121
export const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
2222
export const allowJs = tsconfig?.config.compilerOptions?.allowJs ?? false;
2323

24-
export const fileProtocol = 'file://';
25-
2624
export const tsExtensionsPattern = /\.([cm]?ts|[tj]sx)($|\?)/;
2725

2826
export const isJsonPattern = /\.json(?:$|\?)/;

src/utils/file-url.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const fileUrlPrefix = 'file://';

tests/specs/smoke.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import outdent from 'outdent';
66
import type { NodeApis } from '../utils/tsx.js';
77
import { hasCoverageSourcesContent } from '../utils/coverage-sources-content.js';
88

9+
const isWindows = process.platform === 'win32';
10+
911
const cjsContextCheck = 'typeof module !== \'undefined\'';
1012
const tsCheck = '1 as number';
1113

@@ -513,10 +515,17 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
513515
import './js/index.js?query=123';
514516
import './js/index';
515517
import './js/';
518+
519+
// absolute path
520+
${
521+
isWindows
522+
? ''
523+
: `import ${JSON.stringify(path.join(fixturePath, 'js/index.js'))};`
524+
}
525+
526+
// absolute file url
516527
import ${JSON.stringify(
517-
packageType === 'module'
518-
? new URL('js/index.js', pathToFileURL(fixturePath)).toString()
519-
: path.resolve(fixturePath, 'js/index.js'),
528+
new URL('js/index.js', pathToFileURL(fixturePath)).toString(),
520529
)};
521530
522531
// No double .default.default in Dynamic Import

0 commit comments

Comments
 (0)