@xarc/resolve-alias
v1.0.0
Published
set virtual module aliases for resolve and require
Downloads
2
Readme
resolve alias
In some lib/subdir1/subdir2/subdir3/foo.js
file:
Instead of:
const blah = require("../../../bar.js");
you can do:
const blah = require("$lib/bar.js");
By registering an alias $lib
that points to the lib directory.
For example, you might do that in lib/index.js
:
const resolveAlias = require("@xarc/resolve-alias");
resolveAlias.add("$lib", __dirname); // make $lib an alias that point to the lib directory
alias convention:
- cannot start with
.
or be an absolute path. - cannot contain
/
except if it starts with@
, then it can contain one/
(ie: npm scope). - cannot contain any space.
- cannot start with
default alias
_cwd
points toprocess.cwd()
alias can provide a function:
resolveAlias.add("_woo", (alias, requestPath, fullRequest) => {
return "lib";
});
For example: require("@alias/blah/lib/api.js")
would call the function with:
alias
:"@alias/blah"
requestPath
:"lib/api.js"
fullRequest
:"@alias/blah/lib/api.js"
enable/disable
resolveAlias.enable = false;
remove
resolveAlias.remove("@foo");
License
Licensed under the Apache License, Version 2.0