https://github.com/streetsidesoftware/ts2mjs
Rename TypeScript Created ESM .js files to .mjs
https://github.com/streetsidesoftware/ts2mjs
Last synced: about 1 month ago
JSON representation
Rename TypeScript Created ESM .js files to .mjs
- Host: GitHub
- URL: https://github.com/streetsidesoftware/ts2mjs
- Owner: streetsidesoftware
- License: mit
- Created: 2023-02-18T08:41:45.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-20T12:07:59.000Z (about 2 months ago)
- Last Synced: 2025-04-20T13:26:28.644Z (about 2 months ago)
- Language: TypeScript
- Size: 942 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
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`**
```diff
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`**
```diff
-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`**
```jsonc
{
"$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`**
```jsonc
{
"$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`**
```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"
}
}
}
``````sh
tsc -p tsconfig.cjs.json
tsc -p tsconfig.esm.json
ts2mjs dist/esm
```## Help
```
Usage: ts2mjs [options]Rename ESM .js files to .mjs
Arguments:
files The files to rename.Options:
-o, --output The output directory.
--cwd The current working directory.
--root The root directory.
--dry-run Dry Run do not update files.
--no-must-find-files No error if files are not found.
--no-enforce-root Do not fail if relative `.js` files outside of the root
are imported.
--color Force color.
--no-color Do not use color.
-v, --verbose Verbose mode
-V, --version output the version number
-h, --help display help for command
```
---
Brought to you byStreet Side Software