Paciolan / remote-module-loader

Loads a CommonJS module from a remote URL for the Browser or Node.js.
MIT License
85 stars 21 forks source link

Cannot load umd module (need add module object in eval) #5

Closed unlight closed 5 years ago

unlight commented 5 years ago

UMD (rollup) wrapper could looks like this:

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
    typeof define === 'function' && define.amd ? define('h-document-element', ['exports'], factory) :
    (global = global || self, factory(global['h-document-element'] = {}));
}(this, function (exports) { 'use strict';

So, checking for exports is not enough, need add module in this eval

var exports = {};
new Function("require", "exports", data)(requires, exports);

Webpack umd:

(function webpackUniversalModuleDefinition(root, factory) {
    if(typeof exports === 'object' && typeof module === 'object')
        module.exports = factory();
    else if(typeof define === 'function' && define.amd)
        define([], factory);
    else {
        var a = factory();
        for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
    }
})(window, function() {
unlight commented 5 years ago

@joelnet will you fix it?

joelnet commented 5 years ago

I have updated this to 2.3.0 which should now support UMD modules.

Can you please test this and verify that it is working for you?

unlight commented 5 years ago

Does not work. Issue about remote loaded modules, but not this module @paciolan/remote-module-loader. createLoadRemoteModule function should be changed to something like this:

const module = { exports };
new Function('require', 'module', 'exports', code)(requires, module, exports);
return module.exports;
joelnet commented 5 years ago

Hmm. I have added the babel plugin @babel/plugin-transform-modules-umd, which should have handled that.

Do you have some example code that I could use to test this?

unlight commented 5 years ago

Sure, here is the example.

import { createLoadRemoteModule} from '@paciolan/remote-module-loader';

test('umd load', async () => {
    const remoteModuleLoader = createLoadRemoteModule();
    const result = await remoteModuleLoader('https://unpkg.com/h-document-element@2.0.0/bundles/h-document-element.umd.js');
    expect(result.Fragment).toBeDefined();
    expect(result.createElement).toBeDefined();
    expect(result.h).toBeDefined();
});
joelnet commented 5 years ago

AHH Got it. Misunderstanding. I made remove-module-loader accessible as a UMD. You were referring to loading a UMD.

Now that is cleared up, I can fix the right thing. lol

joelnet commented 5 years ago

A fix has been pushed to 2.4.0

unlight commented 5 years ago

d3adef2429da26cbe14b08a895d9e581b79be104 It should fix. Thank you.