Skip to content

Commit 831ef09

Browse files
check without extensions
1 parent b782365 commit 831ef09

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

lib/internal/modules/esm/resolve.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,34 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
246246
throw err;
247247
}
248248

249-
const stats = internalFsBinding.internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path, '/') ?
250-
StringPrototypeSlice(path, -1) : path));
249+
let stats;
250+
if (getOptionValue('--experimental-typescript')) {
251+
if (StringPrototypeEndsWith(path, '/')) {
252+
path = StringPrototypeSlice(path, 0, -1);
253+
}
254+
// Try to add .ts extension
255+
let attemptTsPath = path;
256+
if (!StringPrototypeEndsWith(attemptTsPath, '.ts')) {
257+
attemptTsPath += '.ts';
258+
}
259+
260+
stats = internalFsBinding.internalModuleStat(toNamespacedPath(attemptTsPath));
261+
switch (stats) {
262+
case 0:
263+
// Found the .ts file
264+
path = attemptTsPath;
265+
break;
266+
case 1:
267+
throw new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base), String(resolved));
268+
default:
269+
// In case we cannot find the .ts file, we fill fall back to js
270+
stats = internalFsBinding.internalModuleStat(path);
271+
break;
272+
}
273+
} else {
274+
stats = internalFsBinding.internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path, '/') ?
275+
StringPrototypeSlice(path, -1) : path));
276+
}
251277

252278
// Check for stats.isDirectory()
253279
if (stats === 1) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { a } from './a';
2+
console.log(a);

test/parallel/test-typescript.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,17 @@ test('execute a typescript with node_modules', async () => {
4545
stdout: 'Hello, TypeScript!\n',
4646
});
4747
});
48+
49+
test('execute a typescript file with imports with no extensions', async () => {
50+
const result = await spawnPromisified(process.execPath, [
51+
'--experimental-typescript',
52+
fixtures.path('typescript/test-import-no-extension.ts'),
53+
]);
54+
55+
deepStrictEqual(result, {
56+
code: 0,
57+
signal: null,
58+
stderr: '',
59+
stdout: 'Hello, TypeScript!\n',
60+
});
61+
});

0 commit comments

Comments
 (0)