Skip to content

Commit

Permalink
feat: support ".mts" and ".cts" extensions (#3)
Browse files Browse the repository at this point in the history
* chore: bump esbuild version

* feat: add ".cts" and ".mts" as default extns

* chore: add "mts" and "cts" fixtures;

- typescript file uses "mjs" and "cjs" imports
- javascript files use real "mts" and "cts" imports

* chore(ci): also run typescript test w/ --require hook
  • Loading branch information
lukeed authored Oct 5, 2021
1 parent c935537 commit cc7ce98
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 8 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ jobs:
- name: Tests <~ ESM
run: node --loader ./loader.mjs test/index.mjs

- name: Tests <~ ESM <~ TypeScript
run: node --loader ./loader.mjs test/config/index.ts --tsmconfig test/config/tsm.js

- name: Tests <~ CommonJS
run: node -r ./require.js test/index.js

- name: Tests <~ TS w/ Config
run: node --loader ./loader.mjs test/config/index.ts --tsmconfig test/config/tsm.js
- name: Tests <~ CommonJS <~ TypeScript
run: node -r ./require.js test/config/index.ts --tsmconfig test/config/tsm.js
2 changes: 2 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Additionally, tsm defines a few extensions by default, each of which is assigned
let config = {
'.jsx': { ...options, loader: 'jsx' },
'.tsx': { ...options, loader: 'tsx' },
'.mts': { ...options, loader: 'ts' },
'.cts': { ...options, loader: 'ts' },
'.ts': { ...options, loader: 'ts' },
}
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"types": "tsc"
},
"dependencies": {
"esbuild": "^0.13.3"
"esbuild": "^0.13.4"
},
"devDependencies": {
"@types/node": "16.10.2",
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ exports.$finalize = function (env: Defaults, custom?: tsm.ConfigFile): tsm.Confi
}

let config: tsm.Config = {
'.mts': { ...base, loader: 'ts' },
'.jsx': { ...base, loader: 'jsx' },
'.tsx': { ...base, loader: 'tsx' },
'.cts': { ...base, loader: 'ts' },
'.ts': { ...base, loader: 'ts' },
};

Expand Down
14 changes: 14 additions & 0 deletions test/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import * as assert from 'assert';

// NOTE: doesn't actually exist yet
import * as js from '../fixtures/math.js';
// @ts-ignore - cannot find types
import * as mjs from '../fixtures/utils.mjs';
// @ts-ignore - cannot find types
import * as cjs from '../fixtures/utils.cjs';

// NOTE: avoid need for syntheticDefault + analysis
import * as data from '../fixtures/data.json';
Expand All @@ -17,4 +21,14 @@ assert.equal(typeof js.div, 'function', 'JS :: typeof :: div');
assert.equal(typeof js.mul, 'function', 'JS :: typeof :: mul');
assert.equal(js.foobar, 3, 'JS :: value :: foobar');

// NOTE: raw MJS missing
assert.equal(typeof mjs, 'object', 'MJS :: typeof');
assert.equal(typeof mjs.capitalize, 'function', 'MJS :: typeof :: capitalize');
assert.equal(mjs.capitalize('hello'), 'Hello', 'MJS :: value :: capitalize');

// NOTE: raw CJS missing
assert.equal(typeof cjs, 'object', 'CJS :: typeof');
assert.equal(typeof cjs.dashify, 'function', 'CJS :: typeof :: dashify');
assert.equal(cjs.dashify('FooBar'), 'foo-bar', 'CJS :: value :: dashify');

console.log('DONE~!');
3 changes: 3 additions & 0 deletions test/fixtures/utils.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function dashify(str: string): string {
return str.replace(/([a-zA-Z])(?=[A-Z\d])/g, '$1-').toLowerCase();
}
3 changes: 3 additions & 0 deletions test/fixtures/utils.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function capitalize(str: string): string {
return str[0].toUpperCase() + str.substring(1);
}
22 changes: 19 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
// @ts-check
const assert = require('assert');

const jsx = require('./fixtures/App1');
const jsx = require('./fixtures/App1.jsx');
const json = require('./fixtures/data.json');
const tsx = require('./fixtures/App2');
const ts = require('./fixtures/math');
// @ts-ignore – prefers extensionless
const tsx = require('./fixtures/App2.tsx');
// @ts-ignore – prefers extensionless
const ts = require('./fixtures/math.ts');
// @ts-ignore – prefers extensionless
const mts = require('./fixtures/utils.mts');
// @ts-ignore – prefers extensionless
const cts = require('./fixtures/utils.cts');

const props = {
foo: 'bar'
Expand Down Expand Up @@ -39,4 +45,14 @@ assert.equal(typeof ts.div, 'function', 'TS :: typeof :: div');
assert.equal(typeof ts.mul, 'function', 'TS :: typeof :: mul');
assert.equal(ts.foobar, 3, 'TS :: value :: foobar');

assert(mts, 'MTS :: typeof');
assert.equal(typeof mts, 'object', 'MTS :: typeof');
assert.equal(typeof mts.capitalize, 'function', 'MTS :: typeof :: capitalize');
assert.equal(mts.capitalize('hello'), 'Hello', 'MTS :: value :: capitalize');

assert(cts, 'CTS :: typeof');
assert.equal(typeof cts, 'object', 'CTS :: typeof');
assert.equal(typeof cts.dashify, 'function', 'CTS :: typeof :: dashify');
assert.equal(cts.dashify('FooBar'), 'foo-bar', 'CTS :: value :: dashify');

console.log('DONE~!');
18 changes: 16 additions & 2 deletions test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import assert from 'assert';

import jsx from './fixtures/App1';
import json from './fixtures/data.json';
import * as ts from './fixtures/math';
import tsx from './fixtures/App2';
// @ts-ignore – expects definitions
import * as mts from './fixtures/utils.mts';
// @ts-ignore – expects definitions
import * as cts from './fixtures/utils.cts';
// @ts-ignore – prefers extensionless
import * as ts from './fixtures/math.ts';
// @ts-ignore – prefers extensionless
import tsx from './fixtures/App2.tsx';

const props = {
foo: 'bar'
Expand Down Expand Up @@ -39,4 +45,12 @@ assert.equal(typeof ts.div, 'function', 'TS :: typeof :: div');
assert.equal(typeof ts.mul, 'function', 'TS :: typeof :: mul');
assert.equal(ts.foobar, 3, 'TS :: value :: foobar');

assert.equal(typeof mts, 'object', 'MTS :: typeof');
assert.equal(typeof mts.capitalize, 'function', 'MTS :: typeof :: capitalize');
assert.equal(mts.capitalize('hello'), 'Hello', 'MTS :: value :: capitalize');

assert.equal(typeof cts, 'object', 'CTS :: typeof');
assert.equal(typeof cts.dashify, 'function', 'CTS :: typeof :: dashify');
assert.equal(cts.dashify('FooBar'), 'foo-bar', 'CTS :: value :: dashify');

console.log('DONE~!');

0 comments on commit cc7ce98

Please sign in to comment.