From 801b113113c940b5d4b3b531e8398a19f07aac9c Mon Sep 17 00:00:00 2001 From: bluelovers Date: Thu, 9 Dec 2021 23:19:39 +0800 Subject: [PATCH] feat: support .mts .cts --- src/index.ts | 14 ++++++++++++-- src/test/index.spec.ts | 24 ++++++++++++++++++++++++ tests/ts45-ext/ext-cts/index.cts | 3 +++ tests/ts45-ext/ext-cts/tsconfig.json | 5 +++++ tests/ts45-ext/ext-mts/index.mts | 3 +++ tests/ts45-ext/ext-mts/tsconfig.json | 5 +++++ 6 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/ts45-ext/ext-cts/index.cts create mode 100644 tests/ts45-ext/ext-cts/tsconfig.json create mode 100644 tests/ts45-ext/ext-mts/index.mts create mode 100644 tests/ts45-ext/ext-mts/tsconfig.json 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 () => { diff --git a/tests/ts45-ext/ext-cts/index.cts b/tests/ts45-ext/ext-cts/index.cts new file mode 100644 index 000000000..9001625ad --- /dev/null +++ b/tests/ts45-ext/ext-cts/index.cts @@ -0,0 +1,3 @@ +export function main() { + return 'hello world'; +} diff --git a/tests/ts45-ext/ext-cts/tsconfig.json b/tests/ts45-ext/ext-cts/tsconfig.json new file mode 100644 index 000000000..28900bb1b --- /dev/null +++ b/tests/ts45-ext/ext-cts/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "module": "CommonJS" + } +} diff --git a/tests/ts45-ext/ext-mts/index.mts b/tests/ts45-ext/ext-mts/index.mts new file mode 100644 index 000000000..9001625ad --- /dev/null +++ b/tests/ts45-ext/ext-mts/index.mts @@ -0,0 +1,3 @@ +export function main() { + return 'hello world'; +} diff --git a/tests/ts45-ext/ext-mts/tsconfig.json b/tests/ts45-ext/ext-mts/tsconfig.json new file mode 100644 index 000000000..1ac61592b --- /dev/null +++ b/tests/ts45-ext/ext-mts/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "module": "ESNext" + } +}