Private betav0.1.0
Docs
Format developer documentation

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 node

Reference

NameDescription
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...>

Description
Specify documents to compile. Passing no value will compile all documents.
Type
string[]
Note
Supports space or comma-separated input.
Further details
# Compile all documents
npx format compile

# Compile select documents
npx format compile --documents invoice monthly-update

Documents match the folder name of the document.

--assets <mode>

Description
Defines the strategy to zipping assets.
Type
"static" | "dynamic" | "none"
Further details

There are three options for assets:

  • Static: bundles every known asset into a per-document assets.zip at 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/zip to 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

Description
Fetch remote asset references at compile time and bundle them into each document's assets.zip. Generally not recommend.
Type
boolean
Default value
false

--inline-remote-css

Description
Inline the contents of any remote CSS files at compile time. Makes PDF generation more performant, with the trade-off of caching all CSS at the point of bundling the renderer.
Type
boolean
Default value
false

--preset <preset>

Description
Sets sensible defaults for --target, dependency strategy, and package export resolution.
Type
"node" | "browser" | "edge"
Further details

The --preset flag is a shorthand that sets --target along with sensible defaults for bundling and module resolution conditions.

PresetTargetBundlingConditions
nodenodeAll dependencies externalizedDefault
browserbrowserAll dependencies bundledDefault
edgeworkerAll dependencies bundledworkerd, 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-lib

In most cases, --preset is all you need. Use --target directly if you need pinpoint control over bundling and conditions yourself.

--target <target>

Description
Sets which JavaScript environment your code will be run in — node, browser or worker.
Type
"node" | "browser" | "worker"
Further details

Choose the JavaScript environment your renderer will run in. --target sets the runtime assumptions only. In most cases you should prefer --preset.

TargetRuns inFramework dependenciesOther dependencies
node (default)Node.js servers, AWS Lambda, etcExternalizedExternalized by default
browserBrowsersBundledBundled by default
workerEdge / Web Worker runtimesBundledExternalized 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 lodash

browser

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-fns

worker

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...>

Description
Packages to leave out of the bundle for browser and worker. These must be available where you call render.
Type
string[]
Note
Supports space or comma-separated input.

--bundle <lib...>

Description
Packages to include into the bundle for node and worker targets.
Type
string[]
Note
Supports space or comma-separated input.

--conditions <conditions...>

Description
Additional export conditions used when resolving bundled dependencies. Appended to any preset defaults.
Type
string[]
Note
Supports space or comma-separated input.

--external-conditions <conditions...>

Description
Additional export conditions used when resolving externalized packages. Appended to any preset defaults.
Type
string[]
Note
Supports space or comma-separated input.

--output <mode>

Description
What compile emits. A JavaScript bundle or plain HTML.
Type
"renderer" | "html"
Default value
renderer
Further details

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.

OutputYou getUse it when
renderer (default)A JavaScript renderer you call with render(data) at runtimeYour production environment runs JavaScript
htmlThe rendered HTML for every data variant, written to disk with its assetsYou 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 html

You would get the following example output:

_generated
manifest.json
invoice
assets.zip
default
index.html
alternative
index.html
monthly-report
assets.zip
default
index.html

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...>

Description
Limit --output html to specific data variants. Defaults to every variant each document has. Ignored with --output renderer.
Type
string[]
Note
Supports space or comma-separated input.

--cjs

Description
Output a CommonJS compatible bundle, as opposed to the default ESM.
Type
boolean
Default value
false

--bundle-name <name>

Description
The package name for the compiled renderer, output in the package.json. Useful if you plan to publish your renderer to a package repository.
Type
string

--version <version>

Description
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.
Type
string

--no-validate-schema

Description
Disables data validation against your schema when calling render. Switch this off if you plan to validate yourself or are prone to taking risks. Read more about data and schema.
Type
boolean
Default value
true
Note
Negation flag: default is true. Passing --no-validate-schema sets this to false.

--out-dir <path>

Description
Override the base output directory for the compiled Format bundle. Value must not be the same as rootDir.
Type
string
Default value
<rootDir>/_generated

--no-clean

Description
Disabled removing the output directory before compiling.
Type
boolean
Default value
true
Note
Negation flag: default is true. Passing --no-clean sets this to false.
Was this page helpful?