undefaultify
Post-processor for UMD bundles if source was ES6 Modules, and you want to export default
directly.
For instance, transpiling ES6 code to CommonJS and then use Browserify in standalone, results in such a UMD bundle.
Why
When transpiling ES6 code to CommonJS, the export value is assigned to the default
property. This is a nice solution for interoperability between various module systems, but I don't like my library consumers to require an unexpected require('mylib').default
(either in CommonJS or AMD).
What
It rewrites the prelude for "standalone" UMD bundles, so it exports the default
property of the exported object instead of the exported object itself. For consumers of your library:
// Before
var lib = require('lib').default;
// After
var lib = require('lib');
How
It's horrible. With a regular expression, the source code is modified:
// Before
!function(e){})
// After
!function(_e) {
function e() {
return _e()["default"]
}
});
The definition function e
is renamed to _e
and returns the default
property of the original return value of e()
.
And
You can also tell it to rename the exported name. I wasn't able to export as "_" or "$" with Browserify directly, so included it here. This is pretty rare, though.
Example usage
browserify es6/entry.js --transform 6to5-browserify --standalone mylib | undefaultify > dist/lib.js
browserify es6/entry.js --transform es6ify --standalone mylib | undefaultify mylib _ > dist/lib.js