0
votes

I'm creating a JavaScript SDK using RequireJS to be used on other people's websites. My RequireJS build config uses a namespace "myNamespace" so that my SDK can be bundled with its own version of RequireJS without interfering with the other people's websites. While this works as expected I've run into a problem when trying to require my SDK modules from another RequireJS module:

myNamespace.define('public1', function (require) {
    var public2 = require('public2');

    return { x: 1, y: public2.y };
});

myNamespace.define('public2', function (require) {
    return { y: 1 };
});

define(function (require) {
    var private1 = require('private1');
    var private2 = require('private2');
    var public1 = myNamespace.require('public1'); // This doesn't work as it isn't using the provided `require` method.
});

Any ideas how to "mix" the two RequireJS libraries? More info on RequireJS namespace here: http://requirejs.org/docs/faq-advanced.html#rename

1

1 Answers

0
votes

So, I figured out a solution. Please let me know if you think this could be done smarter. I've created a plugin that functions as a wrapper:

define(['https://example.com/mySDK.js'], {
    load: function (name, req, onload, config) {
        myNamespace.require([name], onload);
    }
});