Repository navigation
Description
Environment
mkdist 1.1.2 on Node.js 16.14.2, v18.14.0.
Reproduction
- Have
.mtsand.ctsfiles in yoursrcdirectory. - Run
mkdist -d --ext js
Example project files
// farewells.cts
export function sayGoodbye(name: string) {
console.log(`Goodbye, ${name}!`)
}// greetings.ts
export function sayHello(name: string) {
console.log(`Hello, ${name}!`)
}// index.mts
import { sayHello } from './greetings.js'
import { sayGoodbye } from './farewells.cjs'
sayHello('World')
sayGoodbye('World')StackBlitz example: https://stackblitz.com/edit/node-erv3qh?file=package.json&view=editor (use pnpm build:tsc to build with TypeScript and pnpm build:mkdist to build with mkdist, start to run ./dist/index.mjs with Node).
Describe the bug
mkdist ignores .cts and .mts files and copies them as is instead of compiling them to .cjs (+.d.cts) and .mjs (+.d.mts) respectfully.
Additional context
I could've tried to explain the importance of these file extensions on my own but Bing did a nicer job than me 😢
The
.mjsand.cjsextensions are used to specify the module type of a JavaScript file in Node.js 16+. The.mjsextension indicates that the file is an ECMAScript module (ESM), which is a standard format for modular JavaScript code. The.cjsextension indicates that the file is a CommonJS module, which is a legacy format that was widely used before ESM was introduced12.The
.mtsand.ctsextensions are used to specify the module type of a TypeScript file. TypeScript is a superset of JavaScript that adds static types and other features. The.mtsextension indicates that the file is an ESM TypeScript file, which will be emitted as an.mjsfile when compiled to JavaScript. The.ctsextension indicates that the file is a CommonJS TypeScript file, which will be emitted as a.cjsfile when compiled to JavaScript1.These extensions help Node.js to determine how to load and execute different types of modules without relying on ambiguous heuristics or configuration options3. They also help developers to write clear and consistent code across different environments.
While this is a nice explanation, it fails to mention a few other important things:
If your file has a .ts extension, TypeScript always assumes the resulting file will have .js extension. So if any of the files in your project imports the other file that has .ts extension, and you use Node16 (NodeNext) module resolution (which you very much should btw), TypeScript forces you to write import with .js extension, which is incompatible with mkdist, unless you force it in .js extension mode (--ext js), which you cannot do in environments like nuxt-module-builder without patching it (which is exactly what I'm struggling with and what I am doing).
The other important thing is that .ts, .cts, .mts on the declaration files imply the existence of the file with the same basename: that is, index.d.ts says ‘the declaration file is for index.js’, a.d.cts is ‘for a.cjs’, and b.d.mts is ‘for b.mjs’. That becomes super important for libraries that ship with wildcard exports in package.json, as well helps you as developer to avoid writing types condition which may or may not lead to a correct declaration file (without types export condition TypeScript looks for [basename].d.[ts/cts/mts]).
{
"exports": {
"./locale-data/*": { "import": "./dist/locale-data/*" }
}
}Logs
No response
Footnotes
-
https://www.typescriptlang.org/docs/handbook/esm-node.html ↩ ↩2
-
https://blog.logrocket.com/how-to-use-ecmascript-modules-with-node-js/ ↩
-
https://nodejs.org/api/packages.html ↩
Activity
pi0 commentedon Jul 18, 2023
Hi and thanks for well explaining in issue. Primary support added vis #162. Please feel free to mention if you think something is not following up spec with new handler.
brawaru commentedon Jul 19, 2023
@pi0 I have quickly checked the 1.3.0 and it doesn't seem to produce valid output in the updated playground: https://stackblitz.com/edit/node-yk5rfx?file=package.json&view=editor :(
In the playground the output of mkdist should match the TypeScript output (
pnpm build:tsc):farewells.d.cts— DTS forfarewells.cjsfarewells.cjs— CJS modulegreetings.d.ts— DTS forgreetings.jsgreetings.js— CJS/ESM module depending ontsConfig.compilerOptions.module/pkgJson.type?index.d.mts— DTS forindex.mjsindex.mjs— ESM moduleCurrently it outputs (
pnpm build:mkdist):falewells.d.cts— DTS forfarewells.cjs(file missing)farewells.mjs— ESM module (no declaration)greetings.d.ts— DTS forgreetings.js(file missing)greetings.mjs— ESM module (no declaration)index.d.mts— DTS forindex.mjsindex.mjs— ESM module (contains unresolved imports)The test is also indeed flawed.
To simplify:
[filename].d.tsfiles tell TypeScript ‘hey, there's[filename].js, I'm a declaration for it’. Likewise,[filename].d.mtsis ‘there's[filename].mjs’, and[filename].d.ctsis ‘there's[filename].cjs’..mtsand.ctsin sources tell TypeScript how to compile these files regardless the config,.mtscompiles to ESM files with.mjsextension, and.ctsto CJS with.cjs.While updating I noticed I used
extargument (which I removed in updated playground), and I'm not sure howextargument can be applied in this scenario. Maybe it can affect only.js/.tssource files and overridetsConfig.compilerOptions.module/pkgJson.type, e.g. if you set it tomjs, then all.jssource files will be emitted as.mjsand have associated.d.mtsdeclarations, whereas.cjsand.mjsfiles are emitted as is.[-].mts and .cts copied as is[/-][+]Declarations not genered to `.mts` and `.cts` files[/+]fix: declaration extensions should correspond to their js extension (u…
fix: declaration extensions should correspond to their js extension (u…
fix: declaration extensions should correspond to their js extension (u…
fix: declaration extensions should correspond to their js extension (u…