eWizard.js Path Alias Resolver
Description
This module is used to resolve aliases in monorepo projects. There are three entities in monorepo approach: master template, layout, ewizard template.
The ^
alias can refer to the following locations:
- the root of the current project;
- the master template installed as a dependency in node_modules;
- a specific layout located in the layouts folder in the master template project.
The path to the master template and the layout name are taken from the corresponding fields in the system settings.
The module exports:
-
ALIAS
- constant with alias character; -
AliasResolver
- main module class; -
DynamicAliasResolvePlugin
- webpack resolve plugin based on enhanced-resolve; -
traverseAsync
- helper function for asynchronous recursive traversal of non-cyclic objects or arrays.
Usage examples
AliasResolver
const layoutOptions = {
masterTemplatePath: settings.path.masterTemplatePath ?? '',
layoutsDir: settings.path.layouts ?? 'layouts',
layoutName: settings.masterTemplateLayout ?? '',
};
async function resolveAliases(baseDir, paths, layoutOptions) {
const resolver = new AliasResolver(baseDir, layoutOptions);
return resolver.resolve(paths);
}
DynamicAliasResolvePlugin
const webpackConfig = {
resolve: {
plugins: [new DynamicAliasResolvePlugin(baseDir, layoutOptions)],
},
};
traverseAsync
async function getModifiedSource(source) {
return traverseAsync(source, traverseCb);
}
function traverseCb(context) {
const { parent, key, value, keyPath } = context;
// do sime action
}