Code Monkey home page Code Monkey logo

cjs-to-es6's Introduction

cjs-to-es6

Maintenance notice: this package is no longer under active maintenance.

CLI to convert JavaScript files from CommonJS to ES6 / ES2015 modules format. The process isn't foolproof, but it can usually get you ~95% of the way there.

This tool uses jscodeshift to run 5to6-codemod and js-codemod under the hood. It attempts to convert require() and module.exports / exports to import and export.

Install

npm i -g cjs-to-es6

Usage

cjs-to-es6 [ --verbose ] files/directories...

All files are modified in-place. You may want to review & rename them to the .mjs extension, if using Node 14 or later. Un-converted files should use the .cjs extension.

Examples:

cjs-to-es6 index.js             # convert a single file
cjs-to-es6 lib/                 # convert all files in a directory & its subdirectories (.js & .cjs)
cjs-to-es6 foo.js bar.js lib/   # convert many files/directories

cjs-to-es6's People

Contributors

altano avatar amilajack avatar nolanlawson avatar unquietwiki avatar

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  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  avatar  avatar

cjs-to-es6's Issues

recast module not found

Hey! to get this to work, I had to

npm install -g recast as the module wasn't found

Thanks! This was a total time saver!

"spawn UNKNOWN" error show after cjs-to-es6 User.js on windows 10

Error: spawn UNKNOWN
at exports._errnoException (util.js:1023:11)
at ChildProcess.spawn (internal/child_process.js:313:11)
at Object.exports.spawn (child_process.js:387:9)
at spawn (C:\Users\sunzhjie\AppData\Roaming\npm\node_modules\cjs-to-es6\node
_modules\child-process-promise\lib\index.js:81:23)
at runCodeshift (C:\Users\sunzhjie\AppData\Roaming\npm\node_modules\cjs-to-e
s6\index.js:57:15)
at derequireify (C:\Users\sunzhjie\AppData\Roaming\npm\node_modules\cjs-to-e
s6\index.js:76:10)
at C:\Users\sunzhjie\AppData\Roaming\npm\node_modules\cjs-to-es6\index.js:10
5:10

npm audit vulnerabilities

A quick heads up that the module needs a few dependency bumps. npm audit is returning several warnings and some of them are considered high severity.

Typescript Support?

Wondering if there are any plans to add support for TS files to this package, or if someone is aware of a way to make this work with TS?

Awkward code doesn't get converted

var graph = module.exports;

graph.utilities = require('./utilities.js');
graph.events = require('./events.js');
graph.redraw = require('./redraw.js').redraw;

Transform error.

cjs-to-es6 should offer a way to invoke "named-export-generation" transform on the converted modules

Many of my CommonJS modules export an object via module.exports.

5to6-codemod/exports transform basically renames "module exports" to "export default".
But I need 5to6-codemod/named-export-generation transform to happen afterwards, so that
I have also named exports in the converted ES6 module.

CommonJS:
module.exports = {export1, export2}

ES6 after 'exports' transform:
export default {export1, export2}

ES6 after 'named-export-generation' (the desired state):
export {export1, export2}

Could you introduce for example a new command line option for this purpose?

request: output option

Ahoy. Thanks for putting this module together. I'm intending to use it to generate an mjs file from a CommonJS in an automated prepublish step. One drawback right now is that I can't specify a file to output to; the file I specify to convert is just overwritten. It's a mild inconvenience, as I just have to copy the file to the new extension before running cjs-to-es6.

So being able to specify --out or some such would be keen.

tried to convert appium.js. It seems does not work.

C:\Users\Ritesh Mittal\AppData\Roaming\npm\node_modules\appium\lib>cjs-to-es6 --verbose appium.js

Ahoy! ES6ifyin' your CommonJS for ya...

Found 1 files.

Transforming require() to import ...
Processing 1 files...
Spawning 1 workers...
Sending 1 files to free worker...
All done.
Results:
0 errors
1 unmodified
0 skipped
0 ok
Time elapsed: 1.170seconds

Transforming module.exports/exports to export ...
Processing 1 files...
Spawning 1 workers...
Sending 1 files to free worker...
All done.
Results:
0 errors
1 unmodified
0 skipped
0 ok
Time elapsed: 1.215seconds

ES6ification complete!

glob support

Hey,

Thanks for the library. This doesn't seem to support glob support when running. Is there a way to do this or do you plan to add it?

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.