Skip to content
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

Store the noconflict aliases in the global loader object #205

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/loader/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ var loader, define, requireModule, require, requirejs;
};

loader = {
__aliases: {},
noConflict: function(aliases) {
this.__aliases = aliases;
var oldName, newName;

for (oldName in aliases) {
Expand Down Expand Up @@ -203,7 +205,11 @@ var loader, define, requireModule, require, requirejs;
Module.prototype.makeRequire = function() {
var id = this.id;
var r = function(dep) {
return require(resolve(dep, id));
if (loader.__aliases && loader.__aliases.require) {
return window[loader.__aliases.require](resolve(dep, id));
} else {
return require(resolve(dep, id));
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, can be written like this:

const requireReference =
  loader.__aliases && loader.__aliases.require
    ? window[loader.__aliases.require]
    : require;

return requireReference(resolve(dep, id));

};
r['default'] = r;
r.moduleId = id;
Expand Down
15 changes: 15 additions & 0 deletions tests/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ QUnit.module('loader.js api', function(hooks) {
this._define = define;
this._loader = loader;
this._require = require;
this._requirejs = requirejs;
heimdall._session.reset();
tree = new Heimdall.Tree(heimdall);
});
Expand All @@ -46,6 +47,7 @@ QUnit.module('loader.js api', function(hooks) {
define = this._define;
loader = this._loader;
require = this._require;
requirejs = this._requirejs;

requirejs.clear();
});
Expand Down Expand Up @@ -1842,4 +1844,17 @@ QUnit.module('loader.js api', function(hooks) {
define.alias('foo', 'foo/bar');
assert.equal(require('foo/bar'), 'foo');
});

QUnit.test('no conflict aliases', function(assert) {
var aliases = {
require: 'newRequire',
requirejs: 'newRequireJS'
};

loader.noConflict(aliases);

assert.strictEqual(aliases, loader.__aliases);
});


});