Skip to content

Commit

Permalink
fix(cjs): support file url (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed May 16, 2024
1 parent 942e9bc commit 4a8a2dc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/cjs/api/module-resolve-filename.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import path from 'node:path';
import Module from 'node:module';
import { fileURLToPath } from 'node:url';
import { createPathsMatcher } from 'get-tsconfig';
import { resolveTsPath } from '../../utils/resolve-ts-path.js';
import type { NodeError } from '../../types.js';
import { isRelativePath } from '../../utils/path-utils.js';
import { fileUrlPrefix } from '../../utils/file-url.js';
import {
isTsFilePatten,
tsconfig,
Expand Down Expand Up @@ -69,6 +71,11 @@ export const resolveFilename: ResolveFilename = (
request = request.slice(0, queryIndex);
}

// Support file protocol
if (request.startsWith(fileUrlPrefix)) {
request = fileURLToPath(request);
}

if (
tsconfigPathsMatcher

Expand Down
4 changes: 2 additions & 2 deletions src/esm/hook/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import type {
import { resolveTsPath } from '../../utils/resolve-ts-path.js';
import type { NodeError } from '../../types.js';
import { requestAcceptsQuery } from '../../utils/path-utils.js';
import { fileUrlPrefix } from '../../utils/file-url.js';
import {
tsconfigPathsMatcher,
tsExtensionsPattern,
getFormatFromFileUrl,
fileProtocol,
allowJs,
namespaceQuery,
getNamespace,
Expand Down Expand Up @@ -41,7 +41,7 @@ const resolveExplicitPath = async (

if (
!resolved.format
&& resolved.url.startsWith(fileProtocol)
&& resolved.url.startsWith(fileUrlPrefix)
) {
resolved.format = await getFormatFromFileUrl(resolved.url);
}
Expand Down
2 changes: 0 additions & 2 deletions src/esm/hook/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
export const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
export const allowJs = tsconfig?.config.compilerOptions?.allowJs ?? false;

export const fileProtocol = 'file://';

export const tsExtensionsPattern = /\.([cm]?ts|[tj]sx)($|\?)/;

export const isJsonPattern = /\.json(?:$|\?)/;
Expand Down
1 change: 1 addition & 0 deletions src/utils/file-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const fileUrlPrefix = 'file://';
15 changes: 12 additions & 3 deletions tests/specs/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import outdent from 'outdent';
import type { NodeApis } from '../utils/tsx.js';
import { hasCoverageSourcesContent } from '../utils/coverage-sources-content.js';

const isWindows = process.platform === 'win32';

const cjsContextCheck = 'typeof module !== \'undefined\'';
const tsCheck = '1 as number';

Expand Down Expand Up @@ -513,10 +515,17 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
import './js/index.js?query=123';
import './js/index';
import './js/';
// absolute path
${
isWindows
? ''
: `import ${JSON.stringify(path.join(fixturePath, 'js/index.js'))};`
}
// absolute file url
import ${JSON.stringify(
packageType === 'module'
? new URL('js/index.js', pathToFileURL(fixturePath)).toString()
: path.resolve(fixturePath, 'js/index.js'),
new URL('js/index.js', pathToFileURL(fixturePath)).toString(),
)};
// No double .default.default in Dynamic Import
Expand Down

0 comments on commit 4a8a2dc

Please sign in to comment.