Skip to content

Instantly share code, notes, and snippets.

@tbranyen
Last active August 30, 2018 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbranyen/29607da5bcaf7fc140d466fed605b140 to your computer and use it in GitHub Desktop.
Save tbranyen/29607da5bcaf7fc140d466fed605b140 to your computer and use it in GitHub Desktop.
CJS to ESM
const something = require('something');
const { somethingElse } = require('something-else');
const { exports } = module;
exports.somethingElse = somethingElse;
module.exports = something;
Object.assign(module.exports, exports);
// Replaced by transform
import something from 'something';
import { somethingElse } from 'something-else';
// Added by trasnform
let module = {
exports: {}
};
const { exports } = module;
exports.somethingElse = somethingElse;
module.exports = something;
Object.assign(module.exports, exports);
// Added by transform
export default module.exports;
@tbranyen
Copy link
Author

tbranyen commented Aug 30, 2018

Dynamic require source value will be a separate issue to address, as it will involve using dynamic import.

Something like require('pat' + 'h') could get reduced down to require('path'), but const h = () => { return h; }(); require('pat' + h); won't be possible to flatten. We can rewrite it with something like:

const h = () => { return 'h'; }(); import('pat' + h);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment