Undefined `exports` in UMD -> SLIM
m-mujica opened this issue · comments
When transpile sees a module like the one below (nested define
with a define.amd
check)
// this is generated by rollup
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.Kefir = global.Kefir || {})));
}(this, (function (exports) {
exports.....
});
It adds the following shim to the transpiled module:
window.define = function() {
var factory = arguments[arguments.length - 1];
stealModule.exports = typeof factory === "function" ? factory() : factory;
};
Transpile's shim falls short to handle rollup's UMD for a couple of reasons:
-
the slim loader calls the module factory bound to
stealModule.exports
, so references tothis
expecting to be bound to thewindow
are broken. (We might detect the top levelthis
and wrap the UMD in a IIFE sothis
is bound to the window thanks to JS weirdness) -
The actual module factory expects
exports
to be passed in (in this example), so if we want to continue "faking" thedefine
function we'll have to add logic to injectexports
ifdefine
lists it as a dependency.
2.1) whenexports
is listed as a dependency we only need to callfactory(stealExports)
; the
assignmentstealModule.exports =
won't work unless the factory returns the exported
value which is not likely.
2.2) if we do 2.1, I'm concerned about most complexdefine
usage, likedefine
depending
module
orrequire
; should we cross this bridge when we get there?
cc: @matthewp