-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
".mts" and ".cts" default format #4
Comments
It's just invalid TS to have Furthered by the fact that these extensions exist so that your typescript can include import { foobar } from "./hello.mjs";
// Automatically resolved to hello.mts |
nothing you said changes the fact that tsm should have "format": "cjs" for ".cts" and "format": "esm" for ".mts", which is what I mentioned |
Actually it does, because the integrity of the file itself is determined by the TS checker. tsm transforms the files based on how it was used, so This is/was a big reason why others like A real-world example of this is the following: // src/math.mjs
// or src/math.js w/ type: module
export const sum = (a, b) => a + b;
// test/math.ts
import * as assert from 'assert';
import * as math from '../src/math';
assert.equal(math.sum(1, 2), 3); run via $ node -r tsm test/math.ts This would convert the TS file into The above would work with Forcing a format here breaks this and really has no benefit. |
your example doesn't have anything to do with what I mentioned, what I am suggesting is following the Typescript 4.5 new convention of |
I understand you, but I don't think you're understanding me. Forcing a format would break this example. That's because when the // src/math.mjs (converted, forced/remains as ESM)
export const sum = (a, b) => a + b;
// test/math.ts (converted, forced as CJS because of --require hook)
const assert = require('assert');
const math = require('../src/math.mjs');
// ^^ THIS IS STILL ESM -> throws syntax error Instead, for tsm transpilation, we need to ignore the file's required format and transform it to the usage's desired format. This can only work if the converter itself produces semantically correct format conversion ... and esbuild does. So instead, when you run // src/math.mjs (converted, forced because of --require hook)
const sum = (a, b) => a + b;
exports.sum = sum;
// test/math.ts (converted, forced because of --require hook)
const assert = require('assert');
const math = require('../src/math.mjs');
// ^^ THIS IS NOW COMMONJS -> success The semantics are preserved. And the same guarantee happens when you run |
Shouldn't the config have these defaults to follow the TypeScript logic?
One could also argue that these extensions format shouldn't even be configurable and force these formats, just as .cjs forces commonjs and .mjs forces ecmascript modules
The text was updated successfully, but these errors were encountered: