Skip to content

Commit

Permalink
Fix for non-local main modules
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckdumont committed May 28, 2019
1 parent 1c4afe4 commit 7723a0a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
32 changes: 23 additions & 9 deletions runtime/DojoLoaderNonLocalMainPatch.runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
* the package directory (e.g. ../../something), then toAbsMid returns an
* irrational path. To work around this issue, we do the following:
*
* 1. Replace the main path used by Dojo with 'main', saving the real
* 1. Replace the main path used by Dojo with an empty string, saving the real
* main path in the 'realMain' property. The causes Dojo's toAbsMid to return
* an absMid like 'packageName/main'.
* an absMid like 'packageName/'.
*
* 2. In our patched implementation of toUrl, we call the Dojo implementation
* 2. In our patched implementation of toAbsMid, we remove the traling slash
* before returning the result to the user so that the absMid for a package
* is just the package name.
*
* 3. In our patched implementation of toUrl, we call the Dojo implementation
* and fixup the url using the path saved in the 'realMain' property.
*/
module.exports = function() {
Expand All @@ -40,28 +44,38 @@ module.exports = function() {
&& typeof pkg.realMain === 'undefined' // hasn't already been adjusted
) {
pkg.realMain = pkg.main;
pkg.main = 'main';
pkg.main = '';
}
});
function toAbsMid(name, referenceModule) {
var absMid = loaderScope.require.originalToAbsMid(name, referenceModule);
if (absMid.indexOf('/') === absMid.length-1) {
var pkgName = absMid.substring(0, absMid.length-1);
var pkg = loaderScope.require.packs[pkgName];
if (pkg && pkg.realMain) {
absMid = pkgName;
}
}
return absMid;
}
function toUrl(name, referenceModule) {
var absMid = loaderScope.require.toAbsMid(name, referenceModule);
var url = loaderScope.require.originalToUrl(absMid, referenceModule);
var match = /^([^/]*)\/main$/.exec(absMid);
var pkg = match && match[1] && loaderScope.require.packs[match[1]];
var url = loaderScope.require.originalToUrl(name, referenceModule);
var pkg = loaderScope.require.packs[name];
if (pkg && pkg.realMain) {
var parts = url.split('?');
if (/(^\/)|(\:)/.test(pkg.realMain)) {
// absolute URL
parts[0] = pkg.realMain;
} else {
// relative URL
parts[0] = parts[0].replace(/main$/, '') + pkg.realMain;
parts[0] = parts[0] + '/' + pkg.realMain;
}
url = parts.join('?');
}
return url;
}
loaderScope.require.originalToAbsMid = loaderScope.require.toAbsMid;
loaderScope.require.originalToUrl = loaderScope.require.toUrl;
loaderScope.require.toAbsMid = toAbsMid;
loaderScope.require.toUrl = toUrl;
};
8 changes: 6 additions & 2 deletions test/TestCases/misc/nonLocalMain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ define(['dojo/has', 'foo', 'bar', 'foo/relPathTests', 'bar/relPathTests'], funct
});

it("should return correct value from toUrl", function() {
require.toUrl("foo/main").should.be.eql('sub/foo/../../sub/bar/main');
require.toUrl("bar/main").should.be.eql('sub/bar/main');
var fooAbsMid = require.toAbsMid("foo");
var barAbsMid = require.toAbsMid("bar");
fooAbsMid.should.be.eql("foo");
barAbsMid.should.be.eql("bar/main");
require.toUrl(fooAbsMid).should.be.eql('sub/foo/../../sub/bar/main');
require.toUrl(barAbsMid).should.be.eql('sub/bar/main');
});

it ("should run relPath tests successfully", function() {
Expand Down
2 changes: 1 addition & 1 deletion test/TestCases/misc/nonLocalMain/sub/foo/relPathTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ define(['require', '.'], function(require, main) {
return function() {
main.should.be.eql("bar/main");
require('.').should.be.eql("bar/main");
require.toUrl('foo/main').should.be.eql('sub/foo/../../sub/bar/main');
require.toUrl('foo').should.be.eql('sub/foo/../../sub/bar/main');
};
});

0 comments on commit 7723a0a

Please sign in to comment.