question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

".mts" and ".cts" default format

See original GitHub issue

Shouldn’t the config have these defaults to follow the TypeScript logic?

{
  ".mts": {
    "format": "esm"
  },
  ".cts": {
    "format":  "cjs"
  }
}

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lukeedcommented, Oct 8, 2021

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 – or src/math.mts for that matter – is loaded, your suggestion would always make it result in ESM syntax (because, natively, it should/is). However, if that were to happen, then the node -r tsm test/math.ts case would have code that looks like this:

// 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 node -r tsm test/math.ts on the above example, you should get this:

// 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 tsm directly or use --loader tsm except everything is coerced into ESM syntax instead.

0reactions
PabloSzxcommented, Oct 8, 2021

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 .mts is always ESM and .cts is always CJS, that’s it, following this convention allows you to specify what is the expected format of the transpilation, since I will know that a typescript file with .cts will have “require” available, while “.mts” will always be esm, that’s it

Read more comments on GitHub >

github_iconTop Results From Across the Web

`.mts` is a cool file extension (TypeScript ES modules)
.mts is a cool file extension (TypeScript ES modules) ... that "Prettier will now format files with .mts and .cts extensions as TypeScript....
Read more >
What're the use cases for the mts and cts file extensions?
You use commonJS as the default for .js files, and selectively apply .mjs as needed. You set "type: "module" and use ESM ...
Read more >
CTSProfile register does not support source format: pdf and ...
default coding does not allow 2 renditions of the same format attached to a document. Suggest create a new format based on pdf,...
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
When TypeScript generates declaration files for .mts and .cts , their ... to import CommonJS modules as if they were ES modules with...
Read more >
Luke Edwards on Twitter: "Introducing `tsm` – TypeScript ...
Here's an example `tsm.js` config file. It's *completely* optional – tsm includes the defaults you're likely to want/need, but you can ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found