Skip to content

Commit f8c04cb

Browse files
author
Chuck Dumont
authored
Merge pull request #243 from chuckdumont/work
Fix for non-local main modules
2 parents 9683d0b + 7723a0a commit f8c04cb

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

runtime/DojoLoaderNonLocalMainPatch.runtime.js

+23-9
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
* the package directory (e.g. ../../something), then toAbsMid returns an
2323
* irrational path. To work around this issue, we do the following:
2424
*
25-
* 1. Replace the main path used by Dojo with 'main', saving the real
25+
* 1. Replace the main path used by Dojo with an empty string, saving the real
2626
* main path in the 'realMain' property. The causes Dojo's toAbsMid to return
27-
* an absMid like 'packageName/main'.
27+
* an absMid like 'packageName/'.
2828
*
29-
* 2. In our patched implementation of toUrl, we call the Dojo implementation
29+
* 2. In our patched implementation of toAbsMid, we remove the traling slash
30+
* before returning the result to the user so that the absMid for a package
31+
* is just the package name.
32+
*
33+
* 3. In our patched implementation of toUrl, we call the Dojo implementation
3034
* and fixup the url using the path saved in the 'realMain' property.
3135
*/
3236
module.exports = function() {
@@ -40,28 +44,38 @@ module.exports = function() {
4044
&& typeof pkg.realMain === 'undefined' // hasn't already been adjusted
4145
) {
4246
pkg.realMain = pkg.main;
43-
pkg.main = 'main';
47+
pkg.main = '';
4448
}
4549
});
50+
function toAbsMid(name, referenceModule) {
51+
var absMid = loaderScope.require.originalToAbsMid(name, referenceModule);
52+
if (absMid.indexOf('/') === absMid.length-1) {
53+
var pkgName = absMid.substring(0, absMid.length-1);
54+
var pkg = loaderScope.require.packs[pkgName];
55+
if (pkg && pkg.realMain) {
56+
absMid = pkgName;
57+
}
58+
}
59+
return absMid;
60+
}
4661
function toUrl(name, referenceModule) {
47-
var absMid = loaderScope.require.toAbsMid(name, referenceModule);
48-
var url = loaderScope.require.originalToUrl(absMid, referenceModule);
49-
var match = /^([^/]*)\/main$/.exec(absMid);
50-
var pkg = match && match[1] && loaderScope.require.packs[match[1]];
62+
var url = loaderScope.require.originalToUrl(name, referenceModule);
63+
var pkg = loaderScope.require.packs[name];
5164
if (pkg && pkg.realMain) {
5265
var parts = url.split('?');
5366
if (/(^\/)|(\:)/.test(pkg.realMain)) {
5467
// absolute URL
5568
parts[0] = pkg.realMain;
5669
} else {
5770
// relative URL
58-
parts[0] = parts[0].replace(/main$/, '') + pkg.realMain;
71+
parts[0] = parts[0] + '/' + pkg.realMain;
5972
}
6073
url = parts.join('?');
6174
}
6275
return url;
6376
}
6477
loaderScope.require.originalToAbsMid = loaderScope.require.toAbsMid;
6578
loaderScope.require.originalToUrl = loaderScope.require.toUrl;
79+
loaderScope.require.toAbsMid = toAbsMid;
6680
loaderScope.require.toUrl = toUrl;
6781
};

test/TestCases/misc/nonLocalMain/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ define(['dojo/has', 'foo', 'bar', 'foo/relPathTests', 'bar/relPathTests'], funct
55
});
66

77
it("should return correct value from toUrl", function() {
8-
require.toUrl("foo/main").should.be.eql('sub/foo/../../sub/bar/main');
9-
require.toUrl("bar/main").should.be.eql('sub/bar/main');
8+
var fooAbsMid = require.toAbsMid("foo");
9+
var barAbsMid = require.toAbsMid("bar");
10+
fooAbsMid.should.be.eql("foo");
11+
barAbsMid.should.be.eql("bar/main");
12+
require.toUrl(fooAbsMid).should.be.eql('sub/foo/../../sub/bar/main');
13+
require.toUrl(barAbsMid).should.be.eql('sub/bar/main');
1014
});
1115

1216
it ("should run relPath tests successfully", function() {

test/TestCases/misc/nonLocalMain/sub/foo/relPathTests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ define(['require', '.'], function(require, main) {
22
return function() {
33
main.should.be.eql("bar/main");
44
require('.').should.be.eql("bar/main");
5-
require.toUrl('foo/main').should.be.eql('sub/foo/../../sub/bar/main');
5+
require.toUrl('foo').should.be.eql('sub/foo/../../sub/bar/main');
66
};
77
});

0 commit comments

Comments
 (0)