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
| Name | Description |
|---|---|
| Specify documents to compile. Passing no value will compile all documents. | |
| Defines the strategy to zipping assets. | |
Fetch remote asset references at compile time and bundle them into each document's assets.zip. Generally not recommend. | |
| Inline the contents of any remote CSS files at compile time. | |
Sets sensible defaults for --target, dependency strategy, and package export resolution. | |
| Sets which JavaScript environment your code will be run in — node, browser or worker. | |
| Packages to leave out of the bundle for browser and worker. These must be available where you call render. | |
| Packages to include into the bundle for node and worker targets. | |
| Additional export conditions used when resolving bundled dependencies. Appended to any preset defaults. | |
| Additional export conditions used when resolving externalized packages. Appended to any preset defaults. | |
| What compile emits. A JavaScript bundle or plain HTML. | |
Limit --output html to specific data variants. Defaults to every variant each document has. Ignored with --output renderer. | |
| Output a CommonJS compatible bundle, as opposed to the default ESM. | |
| The package name for the compiled renderer, output in the package.json. Useful if you plan to publish your renderer to a package repository. | |
| Version 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. | |
| Disables data validation against your schema when calling render | |
Override the base output directory for the compiled Format bundle. Value must not be the same as rootDir. | |
| Disabled removing the output directory before compiling. |
Details
--documents <documents...>
string[]# Compile all documents
npx format compile
# Compile select documents
npx format compile --documents invoice monthly-updateDocuments match the folder name of the document.
--assets <mode>
"static" | "dynamic" | "none"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.
--remote-assets
assets.zip. Generally not recommend.booleanfalse--inline-remote-css
booleanfalse--preset <preset>
--target, dependency strategy, and package export resolution."node" | "browser" | "edge"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.
--target <target>
"node" | "browser" | "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--external <lib...>
string[]--bundle <lib...>
string[]--conditions <conditions...>
string[]--external-conditions <conditions...>
string[]--output <mode>
"renderer" | "html"rendererWe 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.
--variants <variants...>
--output html to specific data variants. Defaults to every variant each document has. Ignored with --output renderer.string[]--cjs
booleanfalse--bundle-name <name>
string--version <version>
string--no-validate-schema
booleantrue--no-validate-schema sets this to false.--out-dir <path>
rootDir.string<rootDir>/_generated--no-clean
booleantrue--no-clean sets this to false.