an ability to require modules using "./" and "../" syntax

This commit is contained in:
Blue 2023-10-04 17:09:44 -03:00
parent 7d0c2d079b
commit 26edf60654
Signed by: blue
GPG Key ID: 9B203B252A63EE38
6 changed files with 60 additions and 8 deletions

View File

@ -133,7 +133,7 @@ const Mimicry = (function privateMimicryClosure () {
for (let i = 0; i < deps.length; ++i) { for (let i = 0; i < deps.length; ++i) {
const element = deps[i]; const element = deps[i];
const contentType = element.contentType; 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); this.load(elName, loadedFileForModule.bind(this, module, elName), contentType);
} }
@ -626,12 +626,41 @@ const Mimicry = (function privateMimicryClosure () {
} }
} }
const src = launcher.src.split("/"); let pathPrefix = directory(launcher.src);
src.pop();
let pathPrefix = src.join('/');
if (pathPrefix !== "") if (pathPrefix !== "")
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) { getScriptWithTag(pathPrefix + "errors.js", function (event) {
cleanTag(event.target); cleanTag(event.target);
BasicError = window.__MimicryErrors__.BasicError; BasicError = window.__MimicryErrors__.BasicError;

11
node.js
View File

@ -125,7 +125,7 @@ class Mimicry {
for (let i = 0; i < deps.length; ++i) { for (let i = 0; i < deps.length; ++i) {
const element = deps[i]; const element = deps[i];
const contentType = element.contentType; 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); this.load(elName, loadedFileForModule.bind(this, module, elName), contentType);
} }
@ -508,6 +508,15 @@ function getCurrentHref(caller = "") {
return path.relative(caller || __dirname, _getCallerFile()); 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() { function _getCallerFile() {
let caller; let caller;
try { try {

3
test/inner/dependency.js Normal file
View File

@ -0,0 +1,3 @@
mimicry.module(function () {
return "Dep";
})

6
test/inner/inner.js Normal file
View File

@ -0,0 +1,6 @@
mimicry.module([
"./dependency",
"../outer"
], function (global, [dependency, outer]) {
return dependency === "Dep" && outer === "Outer";
})

View File

@ -4,8 +4,9 @@ mimicry.module([
"simple", Mimicry.Type.json, "simple", Mimicry.Type.json,
"background", Mimicry.Type.css, "background", Mimicry.Type.css,
"text", Mimicry.Type.text, "text", Mimicry.Type.text,
"binary", Mimicry.Type.binary "binary", Mimicry.Type.binary,
], function (global, [terminalModule, simple, background, text, binary]) { "inner/inner"
], function (global, [terminalModule, simple, background, text, binary, inner]) {
let bnr = false; let bnr = false;
if (binary instanceof ArrayBuffer) { if (binary instanceof ArrayBuffer) {
const view = new Uint8Array(binary); const view = new Uint8Array(binary);
@ -27,6 +28,7 @@ mimicry.module([
terminalModule === "terminal", terminalModule === "terminal",
simple.a[4] === 115, simple.a[4] === 115,
text === "Lorem ipsum, I guess", text === "Lorem ipsum, I guess",
bnr bnr,
inner === true
]; ];
}); });

3
test/outer.js Normal file
View File

@ -0,0 +1,3 @@
mimicry.module(function () {
return "Outer";
});