Read and transform path aliases using JavaScript
The Alias HQ is quite simple, just three methods:
get(name, options?)
orget(callback)
load(path)
add(name, callback)
Grab and convert paths in a particular format
To grab paths using available plugins call get(...)
with the plugin name:
import hq from 'alias-hq'
const config = hq.get('webpack')
To provide custom options, pass an additional hash:
const config = hq.get('rollup', { format: 'object' })
If you need custom transformation:
- pass a function with the following signature
- transform the loaded config
- return the result
function fooify (config, options) {
const { rootUrl, baseUrl, paths } = config
// transform config.paths ...
return ...
}
const config = hq.get(fooify)
See the plugins document for detailed information on writing custom transforms
Load a custom
tsconfig.json
file
Alias HQ attempts to load either js/tsconfig.json
in the project root.
However, you can load any custom config directly:
import hq from 'alias-hq'
const config = hq.load('path/to/tsconfig.custom.json').get(...)
Note when using extends
that TypeScript (and Alias HQ) will use the top-most paths
block – so don't place paths in separate files thinking they will be combined, as they won't.
Add a plugin to be called by name
You can package custom code via plugins.add()
, passing aname
and callback
function:
hq.plugins.add('foo', fooify)
Then simply get()
the paths by passing the plugin name
:
const config = hq.get('foo')
If you want to submit a custom format as a plugin to the repository, post an issue or PR.