Last active
August 30, 2018 18:47
-
-
Save tbranyen/29607da5bcaf7fc140d466fed605b140 to your computer and use it in GitHub Desktop.
CJS to ESM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const something = require('something'); | |
const { somethingElse } = require('something-else'); | |
const { exports } = module; | |
exports.somethingElse = somethingElse; | |
module.exports = something; | |
Object.assign(module.exports, exports); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 torequire('path')
, butconst h = () => { return h; }(); require('pat' + h);
won't be possible to flatten. We can rewrite it with something like: