ts2mjs
v3.0.0
Published
Rename TypeScript Created ESM .js files to .mjs
Downloads
8,320
Maintainers
Readme
ts2mjs
Rename TypeScript created ESM .js files to .mjs
This tool takes output from tsc
and copies/renames the files.
file.js
=>file.mjs
file.js.map
=>file.mjs.map
file.d.ts
=>file.d.mts
file.d.ts.map
=>file.d.mts.map
dist/code.js
-> dist/code.mjs
import * as path from 'path';
import { lib } from 'package/lib/index.js'
-import { findFiles } from './findFiles.js';
+import { findFiles } from './findFiles.mjs';
dist/index.d.ts
-> dist/index.d.mts
-export { PrimeNumber, Tuple, GUID, Address, Person, Annotation, } from './types.js';
+export { PrimeNumber, Tuple, GUID, Address, Person, Annotation, } from './types.mjs';
-export { lookUpPerson } from './lookup.js';
+export { lookUpPerson } from './lookup.mjs';
Usage
This is an example on how to create a package that exports both CommonJS and ESM from TypeScript source.
tsconfig.esm.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tsconfig/node18/tsconfig.json",
"compilerOptions": {
"declaration": true,
"module": "ES2022",
"moduleResolution": "node",
"outDir": "dist/esm",
"sourceMap": true
},
"include": ["src"]
}
tsconfig.cjs.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tsconfig/node18/tsconfig.json",
"compilerOptions": {
"declaration": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "dist/cjs",
"sourceMap": true
},
"include": ["src"]
}
package.json
{
"type": "commonjs",
"main": "dist/csj/index.js",
"module": "dist/esm/index.mjs",
"types": "dist/cjs/index.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.mjs",
"require": "./dist/cjs/index.js"
}
}
}
tsc -p tsconfig.cjs.json
tsc -p tsconfig.esm.json
ts2mjs dist/esm