diff --git a/src/index.ts b/src/index.ts index b3506a4cd..2bf4be322 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,6 +24,7 @@ import { } from './module-type-classifier'; import { createResolverFunctions } from './resolver-functions'; import type { createEsmHooks as createEsmHooksFn } from './esm'; +import { ModuleKind } from 'typescript'; export { TSCommon }; export { @@ -502,11 +503,20 @@ export interface DiagnosticFilter { export function getExtensions(config: _ts.ParsedCommandLine) { const tsExtensions = ['.ts']; const jsExtensions = []; + const useESNext = [ModuleKind.ES2015, ModuleKind.ES2020, 7 as any, ModuleKind.ESNext, 199 as any].indexOf(config.options.module) !== -1; + const useCommonJS = [ModuleKind.CommonJS, 100 as any].indexOf(config.options.module) !== -1; // Enable additional extensions when JSX or `allowJs` is enabled. if (config.options.jsx) tsExtensions.push('.tsx'); - if (config.options.allowJs) jsExtensions.push('.js'); - if (config.options.jsx && config.options.allowJs) jsExtensions.push('.jsx'); + // Support .mts .cts + if (useESNext) tsExtensions.push('.mts'); + if (useCommonJS) tsExtensions.push('.cts'); + if (config.options.allowJs) { + jsExtensions.push('.js'); + if (config.options.jsx) jsExtensions.push('.jsx'); + if (useESNext) tsExtensions.push('.mjs'); + if (useCommonJS) tsExtensions.push('.cjs'); + } return { tsExtensions, jsExtensions }; } diff --git a/src/test/index.spec.ts b/src/test/index.spec.ts index 3b128afdc..b5f8adad4 100644 --- a/src/test/index.spec.ts +++ b/src/test/index.spec.ts @@ -183,6 +183,30 @@ test.suite('ts-node', (test) => { expect(err).toBe(null); expect(stdout).toBe('hello world\n'); }); + + test('should support cts when module = CommonJS', async () => { + const { err, stdout } = await exec( + [ + CMD_TS_NODE_WITH_PROJECT_FLAG, + '-O "{\\"module\\":"CommonJS"}"', + '-pe "import { main } from \'./ts45-ext/ext-cts/index\';main()"', + ].join(' ') + ); + expect(err).toBe(null); + expect(stdout).toBe('hello world\n'); + }); + + test('should support cts when module = ESNext', async () => { + const { err, stdout } = await exec( + [ + CMD_TS_NODE_WITH_PROJECT_FLAG, + '-O "{\\"module\\":"ESNext"}"', + '-pe "import { main } from \'./ts45-ext/ext-mts/index\';main()"', + ].join(' ') + ); + expect(err).toBe(null); + expect(stdout).toBe('hello world\n'); + }); } test('should eval code', async () => {