Skip to content

Instantly share code, notes, and snippets.

@tbranyen tbranyen/cjs.js
Last active Aug 30, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.