format compile
Compile your Format documents into a renderer bundle you can deploy anywhere.
Compiles documents into output you can pass to the Format API client. For more information on compiling a renderer, read the docs on bundling.
npx format compile --documents invoice receipt --preset nodeReference
List options accept space or comma-separated values.
--documentsSpecify documents to compile. Passing no value will compile all documents.
# Compile all documents
npx format compile
# Compile select documents
npx format compile --documents invoice monthly-updateDocuments match the folder name of the document.
Each compile replaces the output directory, including documents from an earlier compile. To include several documents in one bundle, name them all in one command.
string[]--assetsDefines the strategy to zipping assets.
There are three options for assets:
- Static: bundles every known asset into a per-document
assets.zipat build time (default). - Dynamic: outputs the raw assets and embeds a URL map in the bundle and builds the zip at render time, allowing you to replace dynamic assets before rendering.
- None: outputs no assets. You should use
@format.dev/zipto create an assets zip.
--assets is Ignored with --output html, which always bundles assets at build time.
Read more about how to work with assets.
"static" | "dynamic" | "none"--remote-assetsFetch remote asset references at compile time and bundle them into each document's assets.zip. Generally not recommend.
booleanfalse--inline-remote-cssInline the contents of any remote CSS files at compile time.
booleanfalse--presetSets sensible defaults for --target, dependency strategy, and package export resolution.
The --preset flag is a shorthand that sets --target along with sensible defaults for bundling and module resolution conditions.
| Preset | Target | Bundling | Conditions |
|---|---|---|---|
node | node | All dependencies externalized | Default |
browser | browser | All dependencies bundled | Default |
edge | worker | All dependencies bundled | workerd, worker, browser, module |
You can combine a preset with explicit overrides. For example, use the edge preset but exclude a package:
npx format compile --preset edge --external some-large-libIn most cases, --preset is all you need. Use --target directly if you need pinpoint control over bundling and conditions yourself.
"node" | "browser" | "edge"--targetSets which JavaScript environment your code will be run in — node, browser or worker.
Choose the JavaScript environment your renderer will run in. --target sets the runtime assumptions only. In most cases you should prefer --preset.
| Target | Runs in | Framework dependencies | Other dependencies |
|---|---|---|---|
node (default) | Node.js servers, AWS Lambda, etc | Externalized | Externalized by default |
browser | Browsers | Bundled | Bundled by default |
worker | Edge / Web Worker runtimes | Bundled | Externalized by default — use --preset edge for a typical edge deploy |
node (default)
Framework packages (react, react-dom, vue) are left out of the bundle and must be installed where the renderer runs. Most other packages too. Use --bundle to include specific ones:
npx format compile --target node --bundle date-fns lodashbrowser
All dependencies are bundled in by default, producing a self-contained module. No Node.js APIs are assumed. Use --external to exclude packages you load separately:
npx format compile --target browser --external date-fnsworker
For edge or worker runtimes. Like browser, no Node.js or DOM APIs are assumed. The framework is bundled, but other dependencies are externalized by default. Opt them in with --bundle, or switch to --preset edge which bundles everything and applies worker-friendly module resolution.
npx format compile --preset edge"node" | "browser" | "worker"--externalPackages to leave out of the bundle for browser and worker. These must be available where you call render.
string[]--bundlePackages to include into the bundle for node and worker targets.
string[]--conditionsAdditional export conditions used when resolving bundled dependencies. Appended to any preset defaults.
string[]--external-conditionsAdditional export conditions used when resolving externalized packages. Appended to any preset defaults.
string[]--outputWhat compile emits. A JavaScript bundle or plain HTML.
We generally recommend using a JavaScript output bundle, so it can be invoked dynamically at runtime. If your templates contain any sort of dynamic data, this really should be the preferred route.
However, if either want pre-rendered HTML based on your studio variants, or plan to pipe the HTML into your own existing (likely non-JS) ecosystem — i.e. .NET, the HTML output mode can be used.
| Output | You get | Use it when |
|---|---|---|
renderer (default) | A JavaScript renderer you call with render(data) at runtime | Your production environment runs JavaScript |
html | The rendered HTML for every data variant, written to disk with its assets | You have no JavaScript runtime in production, or you want to ship the HTML as-is |
Compiling to HTML
With the following command:
npx format compile --output htmlYou would get the following example output:
_generated
invoice
default
alternative
monthly-report
default
Each document has an assets.zip, which holds the document's full asset set, across all variants. A document with no assets gets no zip.
default and alternative are variants in the above example.
manifest.json lists what was generated, for programmatic consumers:
{
"output": "html",
"engine": "1.2.3",
"documents": [
{
"document": "invoice",
"assets": "invoice/assets.zip",
"variants": [
{ "variant": "default", "html": "invoice/default/index.html" },
{ "variant": "alternative", "html": "invoice/alternative/index.html" }
]
},
{
"document": "monthly-report",
"assets": "monthly-report/assets.zip",
"variants": [
{ "variant": "default", "html": "monthly-report/default/index.html" },
]
}
]
}Choosing variants
By default, the HTML output mode renders every variant a document has. Use --variants to narrow it:
The following flags are not compatible with the HTML output mode: --target, --preset, --external, --bundle, and --cjs.
Remote assets
--remote-assets works with the HTML output mode, and it applies to every variant. Any remote assets referenced across any variants will be folded into the document's assets ZIP.
"renderer" | "html"renderer--variantsLimit --output html to specific data variants. Defaults to every variant each document has. Ignored with --output renderer.
string[]--cjsOutput a CommonJS compatible bundle, as opposed to the default ESM.
booleanfalse--bundle-nameThe package name for the compiled renderer, output in the package.json. Useful if you plan to publish your renderer to a package repository.
string--versionVersion to set in the compiled bundle's package.json. When set, the bundle is ready to publish. When omitted, private: true is included instead as a publishing guard.
string--no-validate-schemaDisables data validation against your schema when calling render
booleantrue--no-validate-schema sets this to false.--out-dirOverride the base output directory for the compiled Format bundle. Value must not be the same as rootDir.
string<rootDir>/_generated--no-cleanLeaves files already in the output directory in place instead of deleting them. Files from an earlier compile stay on disk, but the new bundle only includes the documents you compile now.
booleantrue--no-clean sets this to false.