Lua module path shortener for require function. Any call to require will be intercepted and if normal require does not yield results, index will search in added paths for modules.
- index.addPath(path1, path2, ...)
- Adds path to index. Multiple paths can be added as different parameters.
- index.setNamespace(newNamespace)
- Will set the namespace to newNamespace. Adding this means that requires will ignore the namespace if found, on requires.
- index.getRequirePath(moduleName)
- Will return the real, full require path, if the module has been loaded by index. Will return moduleName otherwise.
Supposing you have multiple libraries deep in folders:
- "path.to.libs.numbers"
- "path.to.libs.strings"
Simply do: require("index")("path.to.libs")
and now you can require your libs require("numbers")
and require("strings")
.
Required modules will receive a table via ...
with the properties:
- .name: The real path where the module was found.
- .path: The path that was added by index.
A small demo is included in the source code.
Adding require path require("index").addPath("path.to.libs")
and require("index")("path.to.libs")
have the same effect.
Setting namespace is needed for large dependencies which require internal files. For example, if your dependency folder and file combo is screen and inside screen.lua you need to require other file, for example, helper.lua inside the same project, you will need to add the project namespace, which in this case is screen, so you would need to do require("screen.helper")
. Setting namespace can also be done by doing: require("index")("path.to.libs")("namespace")
Copyright (c) 2014-2020, Basilio Germán All rights reserved.