GithubHelp home page GithubHelp logo

azu / commonjs-to-es-module-codemod Goto Github PK

View Code? Open in Web Editor NEW
76.0 3.0 14.0 266 KB

Codemod that convert CommonJS(require/exports) to ES Modules(import/export) for JavaScript/TypeScript

License: MIT License

JavaScript 100.00%
codemod commonjs esm esmodules migration tools

commonjs-to-es-module-codemod's Introduction

commonjs-to-es-module-codemod Actions Status: test

jscodeshift codemod that convert CommonJS(require/exports) to ES Modules(import/export) for JavaScript/TypeScript

Support Patterns

Exports

  • named export: module.exports.foo = foo & exports.foo = foo to export { foo }
  • named export: module.exports.bar = foo & exports.bar = foo to export { foo as bar }
  • default export: module.exports = foo to export default foo
  • ignore multiple module.exports = x

Imports

  • require("mod") to import "mod"
  • const foo = require("mod") to import foo from "mod"
  • const { foo } = require("mod") to import { foo } from "mod"
  • const foo = require("mod").foo to import { foo } from "mod"
  • const bar = require("mod").foo to import { foo as bar } from "mod"
  • const o = { foo: require("foo") } to import foo from "mod"; const o = { foo }

Usage

commonjs-to-es-module-codemod is published on npm.

You can convert index.js and index.ts to ES Modules using jscodeshift and unpkg.

# Install jscodeshift
npm install --global jscodeshift
# Transform using latest version
LATEST_VERSION=$(npm view commonjs-to-es-module-codemod version)
jscodeshift -t "https://unpkg.com/commonjs-to-es-module-codemod@${LATEST_VERSION}/dist/index.js" "index.js"
# Transform TypeScript
jscodeshift -t "https://unpkg.com/commonjs-to-es-module-codemod@${LATEST_VERSION}/dist/index.js" --extensions ts "index.ts"

Convert src/*.js:

LATEST_VERSION=$(npm view commonjs-to-es-module-codemod version)
find src -name "*.js" | xargs jscodeshift -t "https://unpkg.com/commonjs-to-es-module-codemod@${LATEST_VERSION}/dist/index.js"

Related

Tests

yarn test

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT ยฉ azu

It includes BuonOmo/CommonJS-to-ES6-codemod.

MIT ยฉ Ulysse Buonomo [email protected]

Related

commonjs-to-es-module-codemod's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

commonjs-to-es-module-codemod's Issues

Possible to use programmatically?

Is there a way to transfrom code without the command line?
Something as the pseudo code below.

import { transform } from 'jscodeshift'
import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)

const cjsToEsModule = require('commonjs-to-es-module-codemod')

const out = transform('exports.toto = true', cjsToEsModule)
out // "export const toto = true"

Wrong output code from `module.exports.b = a;`

Input:

const a = () => {
    return 42;
};
module.exports.a = a;
module.exports.b = a;

Current output:

const a = () => {
    return 42;
};
export { a };
export { b as a };

Expected output:

const a = () => {
    return 42;
};
export { a };
export { a as b };

Error: Cannot find module 'jscodeshift'

I am trying to follow the instructions that I am getting this error.
Any suggestion? I am using node v18

Error: Cannot find module 'jscodeshift'
Require stack:
- /private/var/folders/r9/0wyqy74d7630p6sxb_cfvrgr0000gp/T/jscodeshift202317-80737-16g8is6.q5z5.js
- /Users/<USER>/.nvm/versions/node/v18.12.1/lib/node_modules/jscodeshift/src/Worker.js

Fails on shebang

Hi! thanks for this mod ๐Ÿ’–

I just tried it on some cli files that started with:

#!/usr/bin/env node

'use strict'

const commander = require('commander')

And that is translated to:

#!/usr/bin/env node'use strict'

import commander from 'commander';

Naturally after running the script it will complain that node'use strict' is not an existing interpreter.

The added semicolons are fine, but the corrupted shebang sadly makes it that we cannot use your tool as is.

If you have an idea how to fix that would be nice, otherwise i may run some kind of post-cleanup job perhaps.

All the best :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.