Skip to content

Commit

Permalink
feat: support .mts .cts
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Dec 11, 2021
1 parent 3a2848c commit 801b113
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 };
}

Expand Down
24 changes: 24 additions & 0 deletions src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
3 changes: 3 additions & 0 deletions tests/ts45-ext/ext-cts/index.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function main() {
return 'hello world';
}
5 changes: 5 additions & 0 deletions tests/ts45-ext/ext-cts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "CommonJS"
}
}
3 changes: 3 additions & 0 deletions tests/ts45-ext/ext-mts/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function main() {
return 'hello world';
}
5 changes: 5 additions & 0 deletions tests/ts45-ext/ext-mts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "ESNext"
}
}

0 comments on commit 801b113

Please sign in to comment.