diff --git a/browser.js b/browser.js index 8a521a4..83b1198 100644 --- a/browser.js +++ b/browser.js @@ -133,7 +133,7 @@ const Mimicry = (function privateMimicryClosure () { for (let i = 0; i < deps.length; ++i) { const element = deps[i]; const contentType = element.contentType; - const elName = element.name; + const elName = element.name = fromRelative(element.name, moduleName); this.load(elName, loadedFileForModule.bind(this, module, elName), contentType); } @@ -626,12 +626,41 @@ const Mimicry = (function privateMimicryClosure () { } } - const src = launcher.src.split("/"); - src.pop(); - let pathPrefix = src.join('/'); + let pathPrefix = directory(launcher.src); if (pathPrefix !== "") pathPrefix += "/"; + function directory (path) { + const hops = path.split("/"); + hops.pop(); + return hops.join("/"); + } + + function fromRelative (path, reference) { + let indexSame = path.indexOf("./"); + let indexParent = path.indexOf("../"); + if (indexSame === 0 || indexParent === 0) { + reference = directory(reference); + while (indexSame === 0 || indexParent === 0) { + if (indexSame === 0) { + path = path.slice(2); + } else { + reference = directory(reference); + path = path.slice(3); + } + indexSame = path.indexOf("./"); + indexParent = path.indexOf("../"); + } + let result = reference; + if (result !== "") + result += "/"; + + result += path + return result + } + return path; + } + getScriptWithTag(pathPrefix + "errors.js", function (event) { cleanTag(event.target); BasicError = window.__MimicryErrors__.BasicError; diff --git a/node.js b/node.js index 04f3304..8c16766 100644 --- a/node.js +++ b/node.js @@ -125,7 +125,7 @@ class Mimicry { for (let i = 0; i < deps.length; ++i) { const element = deps[i]; const contentType = element.contentType; - const elName = element.name; + const elName = element.name = fromRelative(element.name, moduleName); this.load(elName, loadedFileForModule.bind(this, module, elName), contentType); } @@ -508,6 +508,15 @@ function getCurrentHref(caller = "") { return path.relative(caller || __dirname, _getCallerFile()); } +function fromRelative (request, reference) { + let indexSame = request.indexOf("./"); + let indexParent = request.indexOf("../"); + if (indexSame === 0 || indexParent === 0) + return path.join(path.dirname(reference), request); + + return request; +} + function _getCallerFile() { let caller; try { diff --git a/test/inner/dependency.js b/test/inner/dependency.js new file mode 100644 index 0000000..39cf202 --- /dev/null +++ b/test/inner/dependency.js @@ -0,0 +1,3 @@ +mimicry.module(function () { + return "Dep"; +}) \ No newline at end of file diff --git a/test/inner/inner.js b/test/inner/inner.js new file mode 100644 index 0000000..2c8b747 --- /dev/null +++ b/test/inner/inner.js @@ -0,0 +1,6 @@ +mimicry.module([ + "./dependency", + "../outer" +], function (global, [dependency, outer]) { + return dependency === "Dep" && outer === "Outer"; +}) \ No newline at end of file diff --git a/test/module.js b/test/module.js index 2e06d95..64a331c 100644 --- a/test/module.js +++ b/test/module.js @@ -4,8 +4,9 @@ mimicry.module([ "simple", Mimicry.Type.json, "background", Mimicry.Type.css, "text", Mimicry.Type.text, - "binary", Mimicry.Type.binary -], function (global, [terminalModule, simple, background, text, binary]) { + "binary", Mimicry.Type.binary, + "inner/inner" +], function (global, [terminalModule, simple, background, text, binary, inner]) { let bnr = false; if (binary instanceof ArrayBuffer) { const view = new Uint8Array(binary); @@ -27,6 +28,7 @@ mimicry.module([ terminalModule === "terminal", simple.a[4] === 115, text === "Lorem ipsum, I guess", - bnr + bnr, + inner === true ]; }); \ No newline at end of file diff --git a/test/outer.js b/test/outer.js new file mode 100644 index 0000000..0789e46 --- /dev/null +++ b/test/outer.js @@ -0,0 +1,3 @@ +mimicry.module(function () { + return "Outer"; +}); \ No newline at end of file