Skip to content

Commit c825fba

Browse files
committed
better URI resolution for when loader is bundled
1 parent 5ff48f7 commit c825fba

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

loader.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@ var sourcemint = null;
3535

3636
// @credit https://github.com/unscriptable/curl/blob/62caf808a8fd358ec782693399670be6806f1845/src/curl.js#L319-360
3737
function loadInBrowser(uri, loadedCallback) {
38-
if (/^\//.test(uri)) {
39-
uri = document.location.protocol + "/" + uri;
40-
}
4138
// See if we are in a web worker.
4239
if (typeof importScripts !== "undefined") {
43-
importScripts(uri);
40+
importScripts(uri.replace(/^\{host\}/, ""));
4441
loadedCallback();
4542
return;
4643
}
44+
if (/^\{host\}\//.test(uri)) {
45+
uri = document.location.protocol + "//" + document.location.host + uri.substring(6);
46+
} else
47+
if (/^\//.test(uri)) {
48+
uri = document.location.protocol + "/" + uri;
49+
}
4750
if (!headTag) {
4851
headTag = document.getElementsByTagName("head")[0];
4952
}
@@ -208,6 +211,13 @@ var sourcemint = null;
208211
{
209212
arguments[2].load = arguments[2].load || sandboxOptions.load;
210213
}
214+
// If the `programIdentifier` (first argument) is relative it is resolved against the URI of the owning sandbox (not the owning page).
215+
if (/^\./.test(arguments[0]))
216+
{
217+
arguments[0] = sandboxIdentifier.replace(/\/[^\/]*$/, "") + "/" + arguments[0];
218+
// HACK: Temporary hack as zombie (https://github.com/assaf/zombie) does not normalize path before sending to server.
219+
arguments[0] = arguments[0].replace(/\/\.\//g, "/");
220+
}
211221
return sourcemint.sandbox.apply(null, arguments);
212222
}
213223
module.require.sandbox.id = sandboxIdentifier;
@@ -392,6 +402,7 @@ var sourcemint = null;
392402
];
393403

394404
// Create a new environment to memoize modules to.
405+
// If relative, the `programIdentifier` is resolved against the URI of the owning page (this is only for the global require).
395406
require.sandbox = function(programIdentifier, loadedCallback, options) {
396407
var sandboxIdentifier = programIdentifier.replace(/\.js$/, "");
397408
return sandboxes[sandboxIdentifier] = Sandbox(sandboxIdentifier, loadedCallback, options || {});

0 commit comments

Comments
 (0)