requirejs / r.js

Runs RequireJS in Node and Rhino, and used to run the RequireJS optimizer
Other
2.57k stars 673 forks source link

Option to not use my namespace for loader config #843

Open sholladay opened 9 years ago

sholladay commented 9 years ago

Problem

My application (we'll call it banana, for sake of discussion), lives on a namespace. window.banana, for example. Yet, that namespace is not fully under my control, due to the 3rd-party nature of banana.

When using the r.js optimizer with the namespace: 'banana' setting, and includeing Alameda as I do here, the optimized file ends up having this at the top ...

var banana;
(function () {
    if (!banana || !banana.requirejs) {
        if (!banana) {
            banana = {};
        }
        else {
            require = banana;
        }
        var requirejs, require, define;
        ...

This is problematic for me, because banana already being defined as a truthy value (but without a requirejs property) is the normal state of affairs for where my application will be loaded.

Therefor, the require = banana code path is taken, and Alameda proceeds to treat require (aka banana) as its own configuration, since it was already defined as a truthy value.

In other words, if left unaddressed, this is going to lead to my customers being able to control my Alameda config, when I don't want them to be able to do so.

Use Case

See my use case in #842 for more info.

sholladay commented 9 years ago

I am thinking that r.js is doing a little too much work here automatically. While a config option could be added that disables this bad behavior, I think it makes more sense to push responsibility of this setup code to the user (via something akin to wrap), if they are going to use a namespace. Then we don't have to hack around various assumptions like this. To make it easier, there could simply be a default for the wrap-like option that does exactly what the existing code does.