Skip to content

Commit

Permalink
refactor(esm): resolve missing format
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed May 28, 2024
1 parent e7dae3b commit 3d7bce7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
30 changes: 16 additions & 14 deletions src/esm/hook/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import type {
ResolveFnOutput, ResolveHookContext,
ResolveFnOutput,
ResolveHookContext,
} from 'node:module';
import { resolveTsPath } from '../../utils/resolve-ts-path.js';
import type { NodeError } from '../../types.js';
Expand Down Expand Up @@ -32,15 +33,12 @@ type resolve = (
recursiveCall?: boolean,
) => MaybePromise<ResolveFnOutput>;

const resolveExplicitPath = async (
nextResolve: NextResolve,
specifier: string,
context: ResolveHookContext,
const resolveMissingFormat = async (
resolved: ResolveFnOutput,
) => {
const resolved = await nextResolve(specifier, context);

if (
!resolved.format
// Filter out node: and data: (sourcemaps)
&& resolved.url.startsWith(fileUrlPrefix)
) {
resolved.format = await getFormatFromFileUrl(resolved.url);
Expand All @@ -60,10 +58,11 @@ const tryExtensions = async (
let throwError: Error | undefined;
for (const extension of extensions) {
try {
return await resolveExplicitPath(
nextResolve,
specifierWithoutQuery + extension + (query ? `?${query}` : ''),
context,
return await resolveMissingFormat(
await nextResolve(
specifierWithoutQuery + extension + (query ? `?${query}` : ''),
context,
),
);
} catch (_error) {
if (
Expand Down Expand Up @@ -167,8 +166,9 @@ export const resolve: resolve = async (
if (tsPaths) {
for (const tsPath of tsPaths) {
try {
return await resolveExplicitPath(nextResolve, tsPath, context);
// return await resolve(tsPath, context, nextResolve, true);
return await resolveMissingFormat(
await nextResolve(tsPath, context),
);
} catch (error) {
const { code } = error as NodeError;
if (
Expand All @@ -183,7 +183,9 @@ export const resolve: resolve = async (
}

try {
const resolved = await resolveExplicitPath(nextResolve, specifier, context);
const resolved = await resolveMissingFormat(
await nextResolve(specifier, context),
);
// Could be a core Node module (e.g. `fs`)
if (requestAcceptsQuery(resolved.url)) {
const resolvedNamespace = getNamespace(resolved.url);
Expand Down
5 changes: 3 additions & 2 deletions src/esm/hook/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { getPackageType } from './package-json.js';

const getFormatFromExtension = (fileUrl: string): ModuleFormat | undefined => {
const queryIndex = fileUrl.indexOf('?');
const extension = path.extname(
fileUrl = (
queryIndex === -1
? fileUrl
: fileUrl.slice(0, queryIndex),
: fileUrl.slice(0, queryIndex)
);
const extension = path.extname(fileUrl);
if (extension === '.json') {
return 'json';
}
Expand Down

0 comments on commit 3d7bce7

Please sign in to comment.