ADR-0012: Plugins load from local files or npm, resolved from the project with ESM conditions
Status: Accepted
Context
Companies need to add their own rules (see ADR-0004). Some will keep a rule in a single file next to the project, others will publish a package to an internal npm registry.
Resolving npm plugins went wrong in a first version: it used createRequire().resolve, which applies the CommonJS conditions. Packages whose exports only declare an import condition (and scoped or subpath ones) failed with a misleading "could not be resolved" message. The built-in import.meta.resolve(spec, parent) is not an option either, because it needs an experimental Node.js flag.
Decision
- Plugins are listed in
plugins:in the config. An entry that starts with./,../, or/is a local file, resolved relative to the config file. Anything else is an npm package. - npm packages are resolved from the project directory with the config file as parent, not from the directory of the audit tool, with
import-meta-resolveand the ESM conditions (import,default). The module is then loaded with dynamicimport(). - A package that only declares
requirefalls back tocreateRequire(configPath).resolve(spec)and loads as CommonJS. - Plugins compiled to CommonJS arrive as
{ default: plugin }, so the loader unwrapsexports.default. - Local plugins are
.mjs,.js, or.cjs. Local.tsplugins are supported only if a TypeScript loader such asjitiis built in. - The loader reports three distinct errors for npm packages: not installed (
plugin.npmResolveFailed), installed but not exported (plugin.npmNotExported), and any other failure (plugin.npmResolveError). - Plugin contract: a plugin exports
{ name, apiVersion, rules, configs?, locales? }. Each rule hasmeta(defaultSeverity,descriptionKey,projectTypes) and acheck(ctx)function that reports withctx.report(...)and translates withctx.t(key, params?). - API version:
apiVersiondeclares the plugin API version, currently"1". If it does not match, the tool reports it and does not load the plugin. - The shape of each rule (
metaandcheck) is validated when the plugin loads.
Consequences
- A package installed next to the project is found, no matter where the audit tool is installed (global,
npx, or local). - Publishing a plugin to an internal registry needs nothing special from the tool. It resolves from the project's
node_modules. import-meta-resolveis a runtime dependency of the CLI package.- Plugins read project data through
ctx.projectContextonly (see ADR-0006). - The
configsfield is part of the contract, but the engine does not use it yet.