From 26edf606546d796c1c2dc6e8b4266d8d424c6041 Mon Sep 17 00:00:00 2001 From: blue Date: Wed, 4 Oct 2023 17:09:44 -0300 Subject: [PATCH] an ability to require modules using "./" and "../" syntax --- browser.js | 37 +++++++++++++++++++++++++++++++++---- node.js | 11 ++++++++++- test/inner/dependency.js | 3 +++ test/inner/inner.js | 6 ++++++ test/module.js | 8 +++++--- test/outer.js | 3 +++ 6 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 test/inner/dependency.js create mode 100644 test/inner/inner.js create mode 100644 test/outer.js 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