It is a script to convert a TypeScript library project to a Node.js Dual CommonJS/ES module packages.
This script aim to convert following project:
- Use TypeScript
- Use ts-node
- Use mocha
This script modify following files:
package.json
: Addexports
field and Install tsconfig-to-dual-package to build dual package.mocharc.json
: Support ESM viats-node/esm
tsconfig.json
: Compile source code to ESMtsconfig.cjs.json
: Compile source code to CJS.gitignore
: Ignore compiled files
As a result, your library will be following
- Your Library has
{ "type" : "module" }
andexports
field - Your Library can be used from CJS(
require
) and ESM(import
)
- Node.js 16+
- npm 8+ (
npm pkg
command is used)
Install dependencies to project and update package.json:
$ bash <(curl -s https://gist.githubusercontent.com/azu/f383ba74c80d17806badd49745ce2129/raw/convert-to-dual-package.sh)
Check source code via eslint-cjs-to-esm
$ npx eslint-cjs-to-esm "./{src,test}/**/*.{js,ts}"
Convert sourc code to ESM via eslint-cjs-to-esm
$ npx eslint-cjs-to-esm "./{src,test}/**/*.{js,ts}" --fix
Test project via publint
npm test
npm run clean && npm run build && npx publint
MIT © azu