Skip to content

andriyor/cjs2mjsExport

Repository files navigation

cjs2mjsExport

A tool that can transform CommonJS to ESM

CLI

tsx [pathToLibrary]/cjs2mjsExport/src/index.ts --project-files='src/**/*.{tsx,ts,js}'

API

import { migrate } from './src/index.ts';

migrate({
  projectFiles: 'src/**/*.{tsx,ts,js}',
})

Related projects

Other projects makes changes file by file instead of fix usage of export from this file and doesn't change ESM import of CJS module

wessberg/cjstoesm: A tool that can transform CommonJS to ESM - produce a lot of changes and slow

have several issues:

5to6/5to6-codemod: A collection of codemods that allow you to transform your js code from ES5 to ES6.

azu/commonjs-to-es-module-codemod: Codemod that convert CommonJS(require/exports) to ES Modules(import/export) for JavaScript/TypeScript

TODO

  • run as cli
  • progress bar
  • handle module.exports.sum with import { sum } from './sum'; will be transformed to export const sum
  • handle module.exports.sum with import tool from './sum'; will be transformed to export const sum and import * tool from './sum';
  • handle module.exports = sum with import sum from './sum'; will be transformed to export const sum and import { sum } from './sum';
  • module.exports = { name: 'name'} need to be transformed to export const [fileName] = {
  • module.exports = {name} need to be transformed to export const name = 'name'
  • handle module.exports = {name: 'name'} with import { name } from './module';
  • handle module.exports.sum = function sum(a, b) {
  • publish package

References

Issue on typescript which can help handle this: Support find-all-references for module.exports · Issue #22205 · microsoft/TypeScript

About

Convert CommonJS export to ESM export

Resources

Stars

Watchers

Forks