Skip to content

Commit 801b113

Browse files
committed
feat: support .mts .cts
1 parent 3a2848c commit 801b113

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from './module-type-classifier';
2525
import { createResolverFunctions } from './resolver-functions';
2626
import type { createEsmHooks as createEsmHooksFn } from './esm';
27+
import { ModuleKind } from 'typescript';
2728

2829
export { TSCommon };
2930
export {
@@ -502,11 +503,20 @@ export interface DiagnosticFilter {
502503
export function getExtensions(config: _ts.ParsedCommandLine) {
503504
const tsExtensions = ['.ts'];
504505
const jsExtensions = [];
506+
const useESNext = [ModuleKind.ES2015, ModuleKind.ES2020, 7 as any, ModuleKind.ESNext, 199 as any].indexOf(config.options.module) !== -1;
507+
const useCommonJS = [ModuleKind.CommonJS, 100 as any].indexOf(config.options.module) !== -1;
505508

506509
// Enable additional extensions when JSX or `allowJs` is enabled.
507510
if (config.options.jsx) tsExtensions.push('.tsx');
508-
if (config.options.allowJs) jsExtensions.push('.js');
509-
if (config.options.jsx && config.options.allowJs) jsExtensions.push('.jsx');
511+
// Support .mts .cts
512+
if (useESNext) tsExtensions.push('.mts');
513+
if (useCommonJS) tsExtensions.push('.cts');
514+
if (config.options.allowJs) {
515+
jsExtensions.push('.js');
516+
if (config.options.jsx) jsExtensions.push('.jsx');
517+
if (useESNext) tsExtensions.push('.mjs');
518+
if (useCommonJS) tsExtensions.push('.cjs');
519+
}
510520
return { tsExtensions, jsExtensions };
511521
}
512522

src/test/index.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,30 @@ test.suite('ts-node', (test) => {
183183
expect(err).toBe(null);
184184
expect(stdout).toBe('hello world\n');
185185
});
186+
187+
test('should support cts when module = CommonJS', async () => {
188+
const { err, stdout } = await exec(
189+
[
190+
CMD_TS_NODE_WITH_PROJECT_FLAG,
191+
'-O "{\\"module\\":"CommonJS"}"',
192+
'-pe "import { main } from \'./ts45-ext/ext-cts/index\';main()"',
193+
].join(' ')
194+
);
195+
expect(err).toBe(null);
196+
expect(stdout).toBe('hello world\n');
197+
});
198+
199+
test('should support cts when module = ESNext', async () => {
200+
const { err, stdout } = await exec(
201+
[
202+
CMD_TS_NODE_WITH_PROJECT_FLAG,
203+
'-O "{\\"module\\":"ESNext"}"',
204+
'-pe "import { main } from \'./ts45-ext/ext-mts/index\';main()"',
205+
].join(' ')
206+
);
207+
expect(err).toBe(null);
208+
expect(stdout).toBe('hello world\n');
209+
});
186210
}
187211

188212
test('should eval code', async () => {

tests/ts45-ext/ext-cts/index.cts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function main() {
2+
return 'hello world';
3+
}

tests/ts45-ext/ext-cts/tsconfig.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"module": "CommonJS"
4+
}
5+
}

tests/ts45-ext/ext-mts/index.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function main() {
2+
return 'hello world';
3+
}

tests/ts45-ext/ext-mts/tsconfig.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ESNext"
4+
}
5+
}

0 commit comments

Comments
 (0)