Compiler API

@marko/compiler compiles .marko templates into JavaScript modules. Bundler integrations invoke it automatically; use this API directly when building bundler plugins, test transforms, or custom tooling.

import * as compiler from "@marko/compiler";

const { code, map } = await compiler.compileFile("./src/card.marko", {
  output: "dom",
  sourceMaps: true,
});

Compile Functions

Each compile function provides an async form and a synchronous *Sync counterpart with identical arguments.

compile(src, filename, options)

ParameterDetails
srcTemplate source string
filenameTemplate path used to resolve tags, relative imports, and its ID
optionsOptional options overriding configured defaults

Returns a Promise of the compile result; compileSync(src, filename, options) returns it synchronously. filename does not need to exist on disk.

import { compileSync } from "@marko/compiler";

const { code } = compileSync(
  "<p>Order #${input.orderId} has shipped</p>",
  "/app/src/tags/shipping-notice.marko",
);

compileFile(filename, options)

Reads filename via fileSystem and compiles it. compileFileSync(filename, options) is the synchronous form.

Compile Result

PropertyDetails
codeCompiled JavaScript, or null when code is false
mapSource map when sourceMaps is true or "both" (and code is enabled without entry), else null
astFinal Babel AST when ast is true, otherwise null
metaMetadata gathered during compilation

Metadata

PropertyDetails
idTemplate ID shared between server and browser outputs
watchFilesNon-template files affecting output (such as marko.json) for bundler file watching
analyzedTagsOptional list of templates whose analysis informed this compile and invalidate it on change
diagnosticsErrors, warnings, deprecations, and suggestions with type, label, loc, and optional fix
apiComponent API ("tags" or "class"), or undefined for custom translators without preferAPI

Errors

Failed compiles throw a CompileError (check err.name === "CompileError" or "CompileErrors", as these classes are not exported). Its message includes the file location and code frame; integrations formatting their own diagnostics read:

PropertyDetails
labelError text without location or code frame
locOptional source range (start, optional end lines and columns)
frameFormatted code frame
filenameTemplate path

Multiple errors throw a CompileErrors object with an errors array of CompileError instances.

Configuration

configure(options) replaces the default options for all subsequent compiles. Per-call options override defaults for that call only. Initial defaults can also be supplied as a JSON string in the MARKO_CONFIG environment variable.

import * as compiler from "@marko/compiler";

compiler.configure({ sourceMaps: true, modules: "cjs" });

// Compiled with `sourceMaps: true` and `modules: "esm"`
const result = await compiler.compileFile("./src/receipt.marko", {
  modules: "esm",
});

Require Hook

@marko/compiler/register installs a Node.js CommonJS require hook for .marko files (always compiling with modules: "cjs"):

const register = require("@marko/compiler/register");

register({
  output: "html",
  sourceMaps: "inline",
  extensions: [".marko"],
});

Options

output

ValueOutput
"html"Server output that renders HTML (default)
"dom"Browser output that renders and updates the DOM
"migrate"Template source after running migrations, without translating
"source"Parsed template source, useful with ast

Compiling the same template with "html" and "dom" produces its server and browser halves (see Targeted Compilation).

entry

Compiles the template as an entry point rather than a plain module. Requires linkAssets.

ValueEntry
"page"With output: "html", wraps the page to write its asset tags. With output: "dom", emits the browser entry for that page
"load"With output: "dom", emits the browser entry for a lazily loaded template

linkAssets

Connects server output to browser asset bundles. Pass the same linkAssets configuration to both server and browser compiles, and render pages through their entry: "page" module so asset tags flush into the HTML.

PropertyDetails
onAssetonAsset(kind, file, id) called for each page ("page") or lazy import ("load") needing a matching output: "dom" entry
runtimeAbsolute or virtual module ID imported by server output; exports flush($global, type, id) returning "block" or "defer" HTML tags
build.js
import path from "node:path";
import { compileFile } from "@marko/compiler";

const assets = new Map();
const options = {
  linkAssets: {
    runtime: path.resolve("asset-runtime.js"),
    onAsset(kind, file, id) {
      assets.set(id, { kind, file });
    },
  },
};

const page = "./src/pages/checkout.marko";
const entry = await compileFile(page, {
  ...options,
  output: "html",
  entry: "page",
});
const template = await compileFile(page, { ...options, output: "html" });

for (const [id, { kind, file }] of assets) {
  const browser = await compileFile(file, {
    ...options,
    output: "dom",
    entry: kind,
  });
  // Bundle `browser.code`, then record the tags that load it under `id`.
}
asset-runtime.js
import manifest from "./dist/asset-manifest.json" with { type: "json" };

export function flush($global, type, id) {
  return manifest[id]?.[type] ?? "";
}

resolveVirtualDependency

Templates can emit auxiliary virtual modules for <style> blocks and browser registration helpers. The compiler calls resolveVirtualDependency(filename, dep) for each virtual module and imports the returned specifier:

ParameterDetails
filenameTemplate that produced the virtual module
dep.virtualPathSuggested path (such as ./card.marko.css)
dep.codeVirtual module source
dep.mapSource map when sourceMaps is true or "both"

Preserving virtualPath's extension lets the bundler apply standard CSS/JS handling. Without this option, <style> blocks are omitted from output; browser builds with cross-template registered imports require it.

import path from "node:path";

const virtualModules = new Map();

function resolveVirtualDependency(filename, { virtualPath, code, map }) {
  const id = path.join(path.dirname(filename), "__virtual__", virtualPath);
  virtualModules.set(id, { code, map });
  return id;
}

modules

Output module format: "esm" (default) or "cjs".

optimize

Enables production output: imports the production runtime and shortens internal identifiers. Defaults to true when NODE_ENV is set to anything other than "development". MARKO_DEBUG overrides this, disabling optimization unless set to false or 0.

optimizeKnownTemplates

Ordered list of template paths assigned short sequential IDs when optimize is enabled. Server and browser builds must share the same list.

runtimeId

Isolates multiple Marko runtimes on one page. Must start with a letter or underscore and contain only letters, numbers, and underscores.

sourceMaps

ValueOutput
falseNo source map (default)
trueSource map in result map
"inline"Source map comment inlined into code
"both"Both map and inline comment

code

Defaults to true. Set to false to skip code generation when only meta or ast is needed.

ast

Defaults to false. Set to true to include the final Babel AST in the result.

stripTypes

Strips TypeScript syntax from output. Defaults to true for every output except "source" and "migrate".

translator

Translator module ID or object. Defaults to the installed marko package's translator (or a @marko/runtime-* dependency in package.json).

fileSystem

An fs-compatible object (readFileSync, statSync, readdirSync, plus readFile for compileFile, and optional realpathSync to resolve symlinked packages such as pnpm stores). Defaults to Node's fs.

cache

A Map caching analyzed templates across compiles until a template or dependency changes. Compiles sharing a cache must use identical options apart from output and entry ("source" and "migrate" bypass the cache). Defaults to a shared process-wide Map.

babelConfig

Babel options merged into the compiler's parse and generate configuration.

getTemplateId

A (filename) => id hook overriding the default top-level template ID (percent-encoded root-relative path, or short hash/sequential ID when optimize is enabled). Unsafe characters in the returned ID are percent-encoded.

errorRecovery

When true, records recoverable errors in meta.diagnostics instead of throwing (skipping translation and returning empty code when errors occur), useful for editor tooling.

applyFixes

A Map selecting which migration fixes apply by diagnostic index and passing each mapped value to its fix. Defaults to applying all fixes.

Tag Libraries

The taglib namespace controls tag discovery:

FunctionDetails
register(id, props)Registers a tag library. Without props, loads a .json file path directly or <id>/marko.json for a package name
excludeDir(dirname)Excludes dirname from tag discovery
excludePackage(name)Excludes package name from tag discovery
buildLookup(dirname, translator?, onError?, fileSystem?)Builds the tag lookup for dirname using configured defaults; onError handles failed tag libraries without throwing
clearCaches()Clears cached tag libraries after editing marko.json, marko-tag.json, or package.json (also clear cache to recompile dependent templates)

Runtime Information

ExportDetails
getRuntimeEntryFiles(output, translator?)Runtime modules imported by compiled templates for output, for bundler prebundling
getRuntimeVersion(translator?)Runtime version targeted by the translator
versionInstalled @marko/compiler version

Contributors

Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.