Plugins
Extend the installer with plugins.
Introduction
Plugins follow the shape of Rollup plugins: an object with a name and hooks.
import { ComponentInstaller, type InstallerPlugin } from "fuma-cli/registry/installer";
const myPlugin: InstallerPlugin = {
name: "my-plugin",
// hooks
};
new ComponentInstaller(connector, {
plugins: [myPlugin],
});| Hook | When | First result wins |
|---|---|---|
resolveId | Deciding the output path of a file. | Yes |
transform | After imports of a file are linked. | No, chained |
writeBundle | After files are written. | No |
Hooks are called with a context as this:
| Property | Description |
|---|---|
cwd | The consumer's project. |
baseDir | Absolute path of outDir.base. |
outDir | Directory of each file type. |
getFramework() | The configured/detected framework. |
resolveId
Return the output path as id, or undefined for the default location.
An external file is not installed: imports of it point to id, and its own imports & dependencies are skipped.
import { existsSync } from "node:fs";
// never touch the files that consumer already has
const keepExisting: InstallerPlugin = {
name: "keep-existing",
resolveId(file) {
if (file.output && existsSync(file.output)) return { id: file.output, external: true };
},
};| Field | Description |
|---|---|
file.id | <registry>:<path> |
file.registry | Name of registry, after registryAliases. |
file.path | Path in registry. |
file.info | The manifest entry: type, target, etc. |
file.output | The default location, undefined for route handlers. |
transform
Transform the code of JavaScript/TypeScript files, useful for codemods, license headers, or framework-specific tweaks.
const banner: InstallerPlugin = {
name: "banner",
transform(code, file) {
return `// installed from ${file.id}\n${code}`;
},
};writeBundle
Receive the files that were written, e.g. to register them somewhere else in the consumer's project.
const log: InstallerPlugin = {
name: "log",
writeBundle(files) {
for (const file of files) console.log(file.output);
},
};Built-in
reuseUI
Files of ui type follow the API of Shadcn UI. Use the ones that consumer already has, instead of installing another copy:
import { ComponentInstaller, reuseUI } from "fuma-cli/registry/installer";
new ComponentInstaller(connector, {
plugins: [
reuseUI({
// file in registry -> the consumer's file, relative to `outDir.base`
files: { "utils/cn.ts": "lib/utils" },
}),
],
});Last updated on