1.0.0 • Published 2 years ago

amashi-imports v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

amashi-imports

dynamic commonjs importing

docs

examples

for these examples we'll be using this to set the value

function setValue(amount){
    var fs = require('fs');
    fs.writeFileSync("./tmp/test.js", `module.exports = ${amount};`);
}
const Require = require("amashi-imports");

setValue(0);

const req = new Require("./tmp/test.js");
req.Load(); // this will load your module
console.log(req.value); // 0
setValue(1)
console.log(req.value); // 0

ok cool so you could already do that but here's the fun part

const req = new Require("./tmp/test.js");
req.Load(); // this will load your module
console.log(req.value); // 0

req.Reload(); // this will reload the module

setValue(1)
console.log(req.value); // 1

you can reload the module that way if you make a change to a file you can tell the script to update it to the current without having to update everything else to share a value

actual documentation

this will create a basic require instance

const req = new Require("file path");
console.log(req); // Require("file path")

this will create a require instance but verify the file path to make sure we aren't going to cause future errors.

const req = Require.Import("file path");
console.log(req); // Require("file path")

this will do the same thing except it'll be asynchronous

const req = await Require.AsyncImport("file path");
console.log(req); // Require("file path")
req.Load(); // this will load a module
req.Unload(); // this will unload a module
req.Reload(); // this will reload a module

we also offer asynchronous options

await req.AsyncLoad(); // this will load a module
await req.AsyncUnload(); // this will unload a module
await req.AsyncReload(); // this will reload a module

any | req.value the value of the module.exports. boolean | req.inCache this will tell you if it's in cache. NodeModule | req.cacheValue this will get the value of the module in the cache. object | req.path this will tell you a lot of pathing info across different systems ex: linux & microsoft. string | req.require_resolved this will return the value of require.resolve(filepath). boolean | req.fileExists this will tell you if the file exists