@cather/kwc-module-resolver
v1.0.0
Published
Resolves paths for KWC components
Downloads
4
Readme
@cather/kwc-module-resolver
Implements the KWC module resolver algorithm.
Installation
npm install --save-dev @cather/kwc-module-resolverAPIs
resolveModule(specifier, importer, options)
Synchronously resolves an KWC module specifier from an import path.
import { resolveModule } from '@cather/kwc-module-resolver';
const result = resolveModule('x/foo', './index.js');
console.log(result);If the resolver processes an invalid configuration, it throws an error with the KWC_CONFIG_ERROR error code. If the resolver can't locate the module, it throws an error with the NO_KWC_MODULE_FOUND error code.
import { resolveModule } from '@cather/kwc-module-resolver';
let result;
try {
result = resolveModule('x/foo', './index.js');
} catch (err) {
if (err.code === 'KWC_CONFIG_ERROR') {
console.error(`The request module can't be resolved due to an invalid configuration`, err);
} else if (err.code === 'NO_KWC_MODULE_FOUND') {
console.error(`The requested module doesn't exists. `, err);
} else {
throw err;
}
}
console.log(result);Parameters:
specifier(string, required): The module specifier to resolve.importer(string, required): The file from where the resolution starts.options(object, optional):modules(ModuleRecord[], optional, default:[]): Injects module records to the resolved configuration.rootDir(string, optional, default:process.cwd()): Use only when themodulesoption is set. Modules overrides are resolved from this directory.
Return value:
A RegistryEntry representing the resolved module with the following properties:
entry(string): The absolute path of the module entry point.specifier(string): The resolved module specifier.scope(string): The absolute path from where the module has been resolved.
Module resolution
The KWC compiler uses a custom resolution algorithm to resolve KWC modules. To configure module resolution, use the kwc.config.json file or the kwc key in the package.json file. The modules key accepts an array of module records. The resolver iterates through the modules array and returns the first module that matches the requested module specifier. There are three types of module record:
- Alias module record: A file path where an KWC module can be resolved.
- Directory module record: A folder path where KWC modules can be resolved.
- NPM package module record: An NPM package that exposes one or more KWC modules.
// kwc.config.json
{
"modules": [
{
"name": "ui/button",
"path": "src/modules/ui/button/button.js"
},
{
"dir": "src/modules"
},
{
"npm": "@ui/components"
}
]
}Module record types
Alias module record
An alias module record maps a module specifier to a file path. An alias module record is defined by two keys:
name(string, required): The KWC module specifier.path(string, required): The file path to resolve.
In this example, the ui/button KWC module specifier is resolved from the src/modules/ui/button/button.js path.
{
"modules": [
{
"name": "ui/button",
"path": "src/modules/ui/button/button.js"
}
]
}Directory module record
A directory module record specifies a folder path where KWC modules are resolved. A directory module record is defined by one key:
dir(string, required): The directory path containing the modules.
{
"modules": [
{
"dir": "src/modules"
}
]
}The directory module record uses an opinionated folder structure to resolve KWC modules. The directory path can contain one or multiple folders representing the KWC modules namespace. Each of those namespace folders can contain one or multiple folders representing the different KWC modules in the namespace. The name of the folder defines the KWC module name. For a module to be resolved, the KWC module folder must have a file matching the KWC module name, this file is the KWC module entry point.
In this example, if the dir key is set src/modules, the following KWC modules can be resolved: ui/button, ui/icons, shared/utils.
src
└── modules/
├── ui/
│ ├── button/
│ │ ├── button.js
│ │ └── button.html
│ └── icon/
│ └── icon.js
└── shared/
└── utils/
└── utils.jsNPM package module record
An NPM package module record tells the resolver that a given NPM package exposes resolvable KWC modules. More details about how to expose KWC modules out of an NPM package can be found in this section. An NPM package module record is defined by one key:
npm(string, required): The NPM package name exposing the KWC modules.
In this example, the resolver is told to look into the @ui/components NPM package to look up KWC modules.
{
"modules": [
{
"npm": "@ui/components"
}
]
}Exposing KWC modules via NPM packages
To distribute an KWC module publicly via an NPM package, the package should follow the same the KWC module resolution rules. The NPM package should either have the kwc.config.json file in its root directory or the kwc key in its package.json describing how KWC modules are resolved relative to this package.
By default, an KWC module is not exposed outside of an NPM package. The KWC configuration must explicitly list the public KWC modules on the expose key.
In this example, the package makes the ui/button and ui/icon KWC modules public.
{
"modules": [
{
"dir": "src/modules"
}
],
"expose": ["ui/button", "ui/icon"]
}