Private betav0.3.0
DocsSign in
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

List options accept space or comma-separated values.

--documentsSpecify documents to compile. Passing no value will compile all documents.
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.

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.

Type
string[]
--assetsDefines the strategy to zipping assets.
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.

Type
"static" | "dynamic" | "none"
--remote-assetsFetch 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-cssInline the contents of any remote CSS files at compile time.
Details
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
--presetSets sensible defaults for --target, dependency strategy, and package export resolution.
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.

Type
"node" | "browser" | "edge"
--targetSets which JavaScript environment your code will be run in — node, browser or worker.
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
Type
"node" | "browser" | "worker"
--externalPackages to leave out of the bundle for browser and worker. These must be available where you call render.
Type
string[]
--bundlePackages to include into the bundle for node and worker targets.
Type
string[]
--conditionsAdditional export conditions used when resolving bundled dependencies. Appended to any preset defaults.
Type
string[]
--external-conditionsAdditional export conditions used when resolving externalized packages. Appended to any preset defaults.
Type
string[]
--outputWhat compile emits. A JavaScript bundle or plain HTML.
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.

Type
"renderer" | "html"
Default value
renderer
--variantsLimit --output html to specific data variants. Defaults to every variant each document has. Ignored with --output renderer.
Type
string[]
--cjsOutput a CommonJS compatible bundle, as opposed to the default ESM.
Type
boolean
Default value
false
--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.
--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.
--no-validate-schemaDisables data validation against your schema when calling render
Details
Disables data validation against your schema when calling render. Switch this off if you plan to validate yourself or are prone to taking risks. Documents without a schema are never validated. 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-dirOverride the base output directory for the compiled Format bundle. Value must not be the same as rootDir.
Type
string
Default value
<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.
Type
boolean
Default value
true
Note
Negation flag: default is true. Passing --no-clean sets this to false.
Was this page helpful?