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

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Sep 19, 2023
1 parent da7ad8f commit d2eaf9e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 14 deletions.
61 changes: 47 additions & 14 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
import {
transform,
transformDynamicImport,
resolveTsPath,
// resolveTsPath,
compareNodeVersion,
} from '@esbuild-kit/core-utils';
import type { TransformOptions } from 'esbuild';
Expand All @@ -23,6 +23,34 @@ import {
type NodeError,
} from './utils.js';


Check failure on line 26 in src/loaders.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

More than 1 blank line not allowed

const tsExtensions: Record<string, string[]> = Object.create(null);
tsExtensions['.js'] = ['.ts', '.tsx', '.js', '.jsx'];
tsExtensions['.jsx'] = ['.tsx', '.ts', '.js', '.js'];
tsExtensions['.cjs'] = ['.cts'];
tsExtensions['.mjs'] = ['.mts'];

const resolveTsPath = (
filePath: string,
) => {
const extension = path.extname(filePath);
const [extensionNoQuery, query] = path.extname(filePath).split('?');
const tsExtension = tsExtensions[extensionNoQuery];

if (tsExtension) {
const extensionlessPath = filePath.slice(0, -extension.length);
return tsExtension.map(
(tsExtension) => (

Check failure on line 44 in src/loaders.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected parentheses around single function argument having a body with no curly braces

Check failure on line 44 in src/loaders.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

'tsExtension' is already declared in the upper scope on line 39 column 8
extensionlessPath
+ tsExtension
+ (query ? `?${query}` : '')
)

Check failure on line 48 in src/loaders.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Missing trailing comma
);
}
};


Check failure on line 53 in src/loaders.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

More than 1 blank line not allowed
const isDirectoryPattern = /\/(?:$|\?)/;

type NextResolve = (
Expand Down Expand Up @@ -199,19 +227,24 @@ export const resolve: resolve = async function (
/**
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
*/
if (tsExtensionsPattern.test(context.parentURL!)) {
const tsPath = resolveTsPath(specifier);

if (tsPath) {
try {
return await resolveExplicitPath(defaultResolve, tsPath, context);
} catch (error) {
const { code } = error as NodeError;
if (
code !== 'ERR_MODULE_NOT_FOUND'
&& code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'
) {
throw error;
if (
// !recursiveCall &&
tsExtensionsPattern.test(context.parentURL!)
) {
const tsPaths = resolveTsPath(specifier);
if (tsPaths) {
for (const tsPath of tsPaths) {
try {
return await resolveExplicitPath(defaultResolve, tsPath, context);
// return await resolve(tsPath, context, defaultResolve, true);
} catch (error) {
const { code } = error as NodeError;
if (
code !== 'ERR_MODULE_NOT_FOUND'
&& code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'
) {
throw error;
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/specs/typescript/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
});
});

// Does .js resolve .jsx?

// describe('full path via .js', ({ test }) => {
// const importPath = './lib/ts-ext-jsx/index.js';

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

// test('Import', async () => {
// const nodeProcess = await node.import(importPath);
// assertResults(nodeProcess.stdout);
// expect(nodeProcess.stdout).toMatch('{"default":["div",null,"hello world"]}');
// });
// });

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

Expand Down
34 changes: 34 additions & 0 deletions tests/specs/typescript/tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
});
});

describe('full path via js', ({ test }) => {
const importPath = './lib/ts-ext-tsx/index.js';

test('Load - should not work', async () => {
const nodeProcess = await node.load(importPath);
assertNotFound(nodeProcess.stderr, importPath);
});

test('Import', async () => {
const nodeProcess = await node.import(importPath, {
typescript: true,
});
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('{"default":["div",null,"hello world"]}');
});
});

describe('full path via jsx', ({ test }) => {
const importPath = './lib/ts-ext-tsx/index.jsx';

test('Load - should not work', async () => {
const nodeProcess = await node.load(importPath);
assertNotFound(nodeProcess.stderr, importPath);
});

test('Import', async () => {
const nodeProcess = await node.import(importPath, {
typescript: true,
});
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('{"default":["div",null,"hello world"]}');
});
});

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

Expand Down

0 comments on commit d2eaf9e

Please sign in to comment.