Currently, I'm trying to develop a tool, that makes it possible to pack a JS-Module into tar-archive and then load it into Node.js dynamically and in-memory.
It does work perfectly, when importing a single JS-file that has no dependencies to other files. But when there are dependencies, I can't glue them together yet.
An example structure in the archive could look like this:
someArchive.tar.gz
- foo.js
- package.json
- lib
- bar.js
The relationship between the two modules could look like this (foo.js):
import { something } from "./lib/bar.js";
To make it possible to use an archived JS-file in general, I'm using the "adm-zip"-npm-package, which allows me to deal with archives in-memory.
Important: I cannot use the file-system in any way for this project, unfortunately.
While the "adm-zip"-package enables me to handle the data itself, I am constructing the module-object on my own using the Module-Constructor:
const Module = module.constructor;
const m = new Module();
const moduleName = "<Filename relative to archive root>";
m.children = [ <the module-object from bar.js> ];
m._compile(<fooContentFromArchive>, moduleName);
m.path = <moduleName without filename>;
m.id = moduleName;
m.filename = moduleName;
return m;
As you can see, I already put the dependent module-object into the children-array of the superior module-object. As far as I understood, the children-array is containing dependent modules.
But unfortunately, it doesn't work.
I know, that what I am doing is very special and hacky, and especially using the file-system would make everything much easier, but as mentioned, I have the limitation, that I don't have access to the filesystem.
Also, I haven't found any npm-packages, that are enabling me to do solve the problem (If I get it working, I could create one).
Do you have any idea, what I could try?
Thank you in advance!