New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some wrapper checks for `require` will not export as expected #38

Closed
leeyeh opened this Issue Feb 5, 2016 · 2 comments

Comments

Projects
None yet
4 participants
@leeyeh

leeyeh commented Feb 5, 2016

I'm trying to rollup protobufjs with rollup-plugin-commonjs, but I get undefined as export. It turns out the UMD wrapper of protobufjs will check the type of require to export the module as commonjs.

(function(global, factory) {
    /* AMD */ if (typeof define === 'function' && define["amd"])
        define(["bytebuffer"], factory);
    /* CommonJS */ else if (typeof require === "function" && typeof module === "object" && module && module["exports"])
        module["exports"] = factory(require("bytebuffer"), true);
    /* Global */ else
        (global["dcodeIO"] = global["dcodeIO"] || {})["ProtoBuf"] = factory(global["dcodeIO"]["ByteBuffer"]);
})(this, function(ByteBuffer, isCommonJS) {
  // factory
})

The specs of Commonjs defines require as a Function. It make sense for wrapper to check for it. Currently, I use a plugin to add require as a noop function since it should never been called anyway. I hope rollup-plugin-commonjs can handle such use case more elegantly.

function () {
    return {
      intro() {
        return 
           `var require = require || function(id) {throw new Error('Unexpected required ' + id)};`;
      }
    };
  };
@lautis

This comment has been minimized.

Show comment
Hide comment
@lautis

lautis Feb 11, 2016

Contributor

Sinon.js has similar issues with its UMD bundle. Additionally require is passed to a function as an argument, which leads the plugin to ignore those wrapped requires.

/**
 * Sinon core utilities. For internal use only.
 *
 * @author Christian Johansen (christian@cjohansen.no)
 * @license BSD
 *
 * Copyright (c) 2010-2013 Christian Johansen
 */
var sinon = (function () { // eslint-disable-line no-unused-vars
    "use strict";

    var sinonModule;
    var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
    var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;

    function loadDependencies(require, exports, module) {
        sinonModule = module.exports = require("./sinon/util/core");
        require("./sinon/extend");
        require("./sinon/walk");
        require("./sinon/typeOf");
        require("./sinon/times_in_words");
        require("./sinon/spy");
        require("./sinon/call");
        require("./sinon/behavior");
        require("./sinon/stub");
        require("./sinon/mock");
        require("./sinon/collection");
        require("./sinon/assert");
        require("./sinon/sandbox");
        require("./sinon/test");
        require("./sinon/test_case");
        require("./sinon/match");
        require("./sinon/format");
        require("./sinon/log_error");
    }

    if (isAMD) {
        define(loadDependencies);
    } else if (isNode) {
        loadDependencies(require, module.exports, module);
        sinonModule = module.exports;
    } else {
        sinonModule = {};
    }

    return sinonModule;
}());
Contributor

lautis commented Feb 11, 2016

Sinon.js has similar issues with its UMD bundle. Additionally require is passed to a function as an argument, which leads the plugin to ignore those wrapped requires.

/**
 * Sinon core utilities. For internal use only.
 *
 * @author Christian Johansen (christian@cjohansen.no)
 * @license BSD
 *
 * Copyright (c) 2010-2013 Christian Johansen
 */
var sinon = (function () { // eslint-disable-line no-unused-vars
    "use strict";

    var sinonModule;
    var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
    var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;

    function loadDependencies(require, exports, module) {
        sinonModule = module.exports = require("./sinon/util/core");
        require("./sinon/extend");
        require("./sinon/walk");
        require("./sinon/typeOf");
        require("./sinon/times_in_words");
        require("./sinon/spy");
        require("./sinon/call");
        require("./sinon/behavior");
        require("./sinon/stub");
        require("./sinon/mock");
        require("./sinon/collection");
        require("./sinon/assert");
        require("./sinon/sandbox");
        require("./sinon/test");
        require("./sinon/test_case");
        require("./sinon/match");
        require("./sinon/format");
        require("./sinon/log_error");
    }

    if (isAMD) {
        define(loadDependencies);
    } else if (isNode) {
        loadDependencies(require, module.exports, module);
        sinonModule = module.exports;
    } else {
        sinonModule = {};
    }

    return sinonModule;
}());
@Rich-Harris

This comment has been minimized.

Show comment
Hide comment
@Rich-Harris

Rich-Harris Jun 24, 2016

Contributor

Fixed in 3.1.0 – sorry for the long wait

Contributor

Rich-Harris commented Jun 24, 2016

Fixed in 3.1.0 – sorry for the long wait

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment