Compiler
More details about compiler.
Compile Options
You can pass additional options to the compile() function.
import { compile } from "fuma-cli/compiler";
import { registry } from "../registry";
const out = await compile({
root: registry,
// compile options
});onUnknownFile
When component imported a file that is not defined in files, this function is called.
- Return a component file descriptor to include that file.
- Return
falseto treat the import as external (leave the import statement as-is). - Throw error if
undefined.
Component File Descriptor
The object that describes how the file should be installed.
import { compile } from "fuma-cli/compiler";
import { registry } from "../registry";
const out = await compile({
root: registry,
onUnknownFile(absolutePath) {
if (absolutePath.endsWith(".css")) return false;
return {
type: "ui",
path: absolutePath,
};
},
});isExternal
Accept a ref object. Will keep the import statement as-is if true is returned, otherwise continue to resolve the imported file.
import { compile } from "fuma-cli/compiler";
import { registry } from "../registry";
const out = await compile({
root: registry,
isExternal(ref) {
// externalize CSS files
if (ref.type === "file" && ref.file.endsWith(".css")) return true;
return false;
},
});onParseReference
Accept a ref object, allow you to rewrite the resolved file before further resolution.
import { compile } from "fuma-cli/compiler";
import { registry } from "../registry";
const out = await compile({
root: registry,
onParseReference(ref) {
if (ref.type === "file") {
return {
...ref,
file: "<my-custom-path>",
};
}
return ref;
},
});beforeTransform
Accept file content, absolute file path and a context object. The returned string will be used as content before the default file transformation.
- Return
undefinedto keep the original content. - Return a string to replace the content.
import { compile } from "fuma-cli/compiler";
import { registry } from "../registry";
const out = await compile({
root: registry,
beforeTransform(content, file, ctx) {
if (!file.endsWith(".tsx")) return;
return content.replaceAll("@/components", "@/registry/components");
},
});afterTransform
Accept file content, absolute file path and a context object. The returned string will be used as content after the default file transformation.
- Return
undefinedto keep the transformed content. - Return a string to replace the content.
import { compile } from "fuma-cli/compiler";
import { registry } from "../registry";
const out = await compile({
root: registry,
afterTransform(content, file, ctx) {
if (ctx.component.name !== "button") return;
return `/* generated code */\n${content}`;
},
});