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

Commit

Permalink
try to load typescript fallback for specifier with extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Oct 9, 2022
1 parent d50ddac commit 7b37b46
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
42 changes: 38 additions & 4 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,60 @@ type resolve = (
recursiveCall?: boolean,
) => MaybePromise<Resolved>;

const extensions = ['.js', '.json', '.ts', '.tsx', '.jsx'] as const;
const mappedExtensions: Record<string, string[]> = {
'.js': ['.ts', '.tsx'],
'.jsx': ['.ts', '.tsx'],
'.cjs': ['.cts'],
'.mjs': ['.mts'],
'': ['.ts', '.js', '.json', '.tsx', '.jsx'],
};

function resolveTsAlternativeSpecifiers(specifier: string) {
const alternativePaths = mappedExtensions[''].map(alternativeExtension => ({
specifier,
specifierExtension: '',
alternativeExtension,
}));
const specifierExtension = path.extname(specifier);
if (!specifierExtension) {
return alternativePaths;
}
const extensionsToTry = mappedExtensions[specifierExtension] ?? [];
specifier = specifier.slice(0, specifier.length - specifierExtension.length);
return [
// Try using subextension first
...alternativePaths,
...extensionsToTry.map(alternativeExtension => ({
specifier,
specifierExtension,
alternativeExtension,
})),
];
}

async function tryExtensions(
specifier: string,
context: Context,
defaultResolve: resolve,
) {
let error;
for (const extension of extensions) {
for (const alternativePath of resolveTsAlternativeSpecifiers(specifier)) {
const {
specifier: alternativeSpecifier,
specifierExtension,
alternativeExtension,
} = alternativePath;
try {
return await resolve(
specifier + extension,
`${alternativeSpecifier}${alternativeExtension}`,
context,
defaultResolve,
true,
);
} catch (_error: any) {
if (error === undefined) {
const { message } = _error;
_error.message = _error.message.replace(`${extension}'`, "'");
_error.message = _error.message.replace(`${alternativeExtension}'`, `${specifierExtension}'`);
_error.stack = _error.stack.replace(message, _error.message);
error = _error;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/typescript/cts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
describe('full path via .cjs', ({ test }) => {
const importPath = './lib/ts-ext-cts/index.cjs';

test('Load - should not work', async () => {
test('Load', async () => {
const nodeProcess = await node.load(importPath);
expect(nodeProcess.stderr).toMatch('Cannot find module');
expect(nodeProcess.stderr).toMatch('SyntaxError: Unexpected token \':\'');
});

test('Import', async () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/typescript/mts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
describe('full path via .mjs', ({ test }) => {
const importPath = './lib/ts-ext-mts/index.mjs';

test('Load - should not work', async () => {
test('Load', async () => {
const nodeProcess = await node.load(importPath);
expect(nodeProcess.stderr).toMatch('Cannot find module');
assertResults(nodeProcess.stdout);
});

test('Import', async () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/typescript/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
describe('full path via .js', ({ test }) => {
const importPath = './lib/ts-ext-ts/index.js';

test('Load - should not work', async () => {
test('Load', async () => {
const nodeProcess = await node.load(importPath);
expect(nodeProcess.stderr).toMatch('Cannot find module');
assertResults(nodeProcess.stdout, 'ts-ext-ts/index.ts');
});

test('Import', async () => {
Expand Down

0 comments on commit 7b37b46

Please sign in to comment.