Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

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

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

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

leeyeh opened this issue Feb 5, 2016 · 2 comments

Comments

@leeyeh
Copy link

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
Copy link
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 ([email protected])
 * @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
Copy link
Contributor

Fixed in 3.1.0 – sorry for the long wait

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants