@xarc/resolve-alias

1.0.0 • Public • Published

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:

    1. cannot start with . or be an absolute path.
    2. cannot contain / except if it starts with @, then it can contain one / (ie: npm scope).
    3. cannot contain any space.
  • default alias _cwd points to process.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:

  1. alias: "@alias/blah"
  2. requestPath: "lib/api.js"
  3. fullRequest: "@alias/blah/lib/api.js"

enable/disable

resolveAlias.enable = false;

remove

resolveAlias.remove("@foo");

License

Licensed under the Apache License, Version 2.0


Package Sidebar

Install

npm i @xarc/resolve-alias

Weekly Downloads

0

Version

1.0.0

License

Apache-2.0

Unpacked Size

6.73 kB

Total Files

5

Last publish

Collaborators

  • kyle.jeske
  • arunvishnun
  • srinathm85
  • chandub
  • divyakarippath
  • jchip