Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
fix(typescript): allow subextensions (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored May 31, 2022
1 parent ab24e0c commit 85e27f5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ type resolve = (
specifier: string,
context: Context,
defaultResolve: resolve,
recursiveCall?: boolean,
) => MaybePromise<Resolved>;

const hasExtensionPattern = /\.\w+$/;

const extensions = ['.js', '.json', '.ts', '.tsx', '.jsx'] as const;

async function tryExtensions(
Expand All @@ -51,6 +50,7 @@ async function tryExtensions(
specifier + extension,
context,
defaultResolve,
true,
);
} catch (_error: any) {
if (error === undefined) {
Expand Down Expand Up @@ -86,6 +86,7 @@ export const resolve: resolve = async function (
specifier,
context,
defaultResolve,
resursiveCall,
) {
// Added in v12.20.0
// https://nodejs.org/api/esm.html#esm_node_imports
Expand All @@ -106,7 +107,7 @@ export const resolve: resolve = async function (

if (tsPath) {
try {
return await resolve(tsPath, context, defaultResolve);
return await resolve(tsPath, context, defaultResolve, true);
} catch (error) {
if ((error as any).code !== 'ERR_MODULE_NOT_FOUND') {
throw error;
Expand All @@ -119,15 +120,15 @@ export const resolve: resolve = async function (
try {
resolved = await defaultResolve(specifier, context, defaultResolve);
} catch (error) {
if (error instanceof Error) {
if (
(error instanceof Error)
&& !resursiveCall
) {
if ((error as any).code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
return await tryDirectory(specifier, context, defaultResolve);
}

if (
(error as any).code === 'ERR_MODULE_NOT_FOUND'
&& !hasExtensionPattern.test(specifier)
) {
if ((error as any).code === 'ERR_MODULE_NOT_FOUND') {
return await tryExtensions(specifier, context, defaultResolve);
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/fixtures/package-module/lib/ts-ext-ts/index.tsx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from 'node:fs';

console.log(
'loaded ts-ext-ts/index.tsx.ts',
JSON.stringify({
nodePrefix: Boolean(fs),
hasDynamicImport: Boolean(import('fs')),
...(() => {
let nameInError;
try {
nameInError();
} catch (error) {
return {
nameInError: error.message.includes('nameInError'),
sourceMap: error.stack.includes(':11:5'),
};
}
})(),
}),
);

function valueNumber(value: number) {
return value;
}

export default valueNumber(1234);
15 changes: 15 additions & 0 deletions tests/specs/typescript/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
});
});

describe('extensionless with subextension', ({ test }) => {
const importPath = './lib/ts-ext-ts/index.tsx';
const outputSubextension = 'loaded ts-ext-ts/index.tsx.ts {"nodePrefix":true,"hasDynamicImport":true,"nameInError":true,"sourceMap":true}';

test('Load', async () => {
const nodeProcess = await node.load(importPath);
expect(nodeProcess.stdout).toBe(outputSubextension);
});

test('Import', async () => {
const nodeProcess = await node.import(importPath);
expect(nodeProcess.stdout).toBe(`${outputSubextension}\n{"default":1234}`);
});
});

describe('directory', ({ test }) => {
const importPath = './lib/ts-ext-ts';

Expand Down

0 comments on commit 85e27f5

Please sign in to comment.