This module allows you to use path aliases in your NodeJS modules and client-side (in browser).
Probably In a big project you will have something like this:
require('../../../packages/user/models/role') - relative paths
Even if you use a root path detector:
var pathFromRoot = require('some-module-to-detect-root')
require(pathFromRoot('app/packages/user/models/role'))
It is a mess! Imagine that you need to rename/move some file or directory, you need to change each of these paths to point to the new location.
Use path-alias!
var pathAlias = require('path-alias');
//setup alias:
pathAlias.setAlias('user', 'app/packages/user');
//require module:
var role = pathAlias('@user/models/role');
You can also use variables in any parts:
pathAlias.setAlias('c', 'client-side', false);
// will require file: app/packages/user/models/some.client-side.js
var clientModel = pathAlias('@user/models/some.@c');
Also you can use this module to require root-related files:
// will require {PROJECT_ROOT}/app/myModule
var someModule = pathAlias('app/myModule');
var pathAlias = require('path-alias');
//with aliases
var myModel = pathAlias('@user/models/myModel');
//just add root prefix
var anotherModel = pathAlias('app/modules/myModule');
var pathAlias = require('path-alias');
//root path will be detected
console.log(pathAlias.getRoot());
The directory with package.json is considered a project's root directory.
You can also set custom root-path:
pathAlias.setRoot('/you/custom/path')
pathAlias.setAlias('alias', 'path or suffix', resolve);
resolve - should path be resolved or not. True by default.
How does it work?
- './path/' - path, related to caller location will be transformed to absolute
- 'some/path' - root path will be added to non absolute path
If you want to add file suffix you will not want to resolve this alias:
pathAlias.setAlias('c', 'client-suffix', false);
setAliases/getAliases
pathAlias.setAliases({
'user' : 'app/packages/user'
});
Resolve path and return it:
var path = pathAlias.resolve('@user/models/myModel');
It needs to export aliases for client-side module.
- Add pathAlias loader to your webpack config. It replaces all
pathAlias('@some/path.@c')
entries torequire('/resolved/some/path.client')
. Path will be resolved before passing it torequire()
.
const aliasLoader = {
loader: path.resolve('node_modules/path-alias/webpack/loader.js'),
options: {
test: (filePath) => {
//some custom logick to include/exclude files from loader.
},
}
}
- In your client side, if you want to use
pathAlias
for dynamic require, you need to pass require.context to pathAlias:
const contextRequire = require.context('./app', true, /widgets\/[^\/]+\.client\.(js|coffee)$/i);
const pathAlias = require('path-alias');
pathAlias.setAliases({
'c': 'client',
'widgets': './widgets'
});
pathAlias.setRequireCallback((filePath, resolved) => {
if (!/\.(js|coffee)$/i.test(resolved)) {
let foundExt = null;
let found = contextRequire.keys().find((item) => {
if (`${resolved}.js` == item) {
foundExt = 'js';
return true;
}
if (`${resolved}.coffee` == item) {
foundExt = 'coffee';
return true;
}
});
if (found)
resolved += `.${foundExt}`;
}
return contextRequire(resolved);
});