stealjs / transpile

Transpiles from everything to everything else

Home Page:https://www.npmjs.com/package/transpile

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

  1. the slim loader calls the module factory bound to stealModule.exports, so references to this expecting to be bound to the window are broken. (We might detect the top level this and wrap the UMD in a IIFE so this is bound to the window thanks to JS weirdness)

  2. The actual module factory expects exports to be passed in (in this example), so if we want to continue "faking" the define function we'll have to add logic to inject exports if define lists it as a dependency.
    2.1) when exports is listed as a dependency we only need to call factory(stealExports); the
    assignment stealModule.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 complex define usage, like define depending
    module or require; should we cross this bridge when we get there?

cc: @matthewp

fwiw, I manually updated the shim in the build output and I got the bitcentive homepage to render fine:

it's something!

google chrome