Skip to content
MocklyDocs

Reference

CLI

Check and build a site from the command line.

The mockly command checks and builds a site from a folder on your computer, with the same compiler the studio runs. Use it to check a site before you publish, to look at the HTML each variant produces, or to stop a broken change in CI.

It has two commands:

Text
mockly check <dir> [--json]mockly build <dir> --out <dir> [--base-path /s/acme/demo] [--site-url https://example.com]

The site folder#

A site folder has the same layout as a site in the studio: pages in pages/, components in components/, and images, fonts, and other files in public/. The CLI reads every .tsx file in the folder and its subfolders, and every file in public/. When it looks for .tsx files, it skips node_modules and anything whose name starts with a dot.

Check a site#

mockly check compiles and type checks every file, then prints each diagnostic and a summary. It doesn't fetch data or write any files.

Terminal
$ mockly check ./site2 file(s): 0 error(s), 0 warning(s)

When something is wrong, each diagnostic shows its place, its severity, its code, and its message, then the lines around it with the problem underlined, and a hint when there is one:

Terminal
$ mockly check ./sitepages/index.tsx:6:8  error  MK4005  Experiment "hero" needs exactly one variant named "control".  5 |     <Page title="Acme">> 6 |       <Experiment name="hero" goal="signup">    |        ^^^^^^^^^^  7 |         <Variant name="original">  hint: Name the baseline variant "control".1 file(s): 1 error(s), 0 warning(s)

In a terminal, errors are red and warnings are yellow. Output sent to a file or another program has no color, and setting the NO_COLOR environment variable turns it off in the terminal too.

JSON output#

--json prints the result as JSON instead, for scripts, editors, and agents:

Terminal
$ mockly check ./site --json
JSON
{  "ok": false,  "diagnostics": [    {      "code": "MK4005",      "severity": "error",      "message": "Experiment \"hero\" needs exactly one variant named \"control\".",      "file": "pages/index.tsx",      "range": {        "start": {          "line": 6,          "column": 8,          "offset": 128        },        "end": {          "line": 6,          "column": 18,          "offset": 138        }      },      "hint": "Name the baseline variant \"control\"."    }  ]}

ok is false when there's at least one error. Each diagnostic has these fields:

FieldMeaning
codeA stable code, such as MK4005 or TS2367
severity"error" or "warning"
messageOne sentence that names what's wrong
fileThe file's path in the site, such as pages/index.tsx
rangeWhere the problem is: a start and an end, each with a line and a column counted from 1, and an offset in UTF-16 code units counted from 0
hintHow to fix it, when there's a suggestion
fixEdits that fix it, when the fix is unambiguous: a list of a range and the newText to put there
relatedOther places that matter, such as the first declaration of a duplicate, each with a message, a file, and a range

Codes don't change meaning, so a script can rely on them. See Diagnostics for every code.

Exit codes#

Codemockly checkmockly build
0No errors. Warnings don't count.The site was built.
1At least one error.The build failed.
2The command couldn't run.The command couldn't run.

A command can't run when the command is missing or unknown, the folder doesn't exist, or a file in public/ can't be hosted, such as a file whose contents don't match its extension. For mockly build, it's also a missing --out, or a --site-url that isn't an http or https URL. The reason and the usage go to standard error.

Build a site#

mockly build does what publishing does: it checks the site, fetches its data, renders every variant of every page, and builds the stylesheet and scripts. Instead of putting the result live, it writes it to a folder.

Terminal
$ mockly build ./site --out ./outwrote out/index.htmlwrote out/index.hero=bold.htmlwrote out/signup.htmlBuilt 3 HTML file(s) · 04e347dd8117645b · style.css 5.1 KB, 1 script file(s), at most 1.8 KB per page

The last line gives the build's hash, the size of the stylesheet, the number of script files, and the most script any one page loads. Warnings found while building, such as MK4018 for a variant that looks exactly like control, print before it. When there are errors, it prints them and "Build failed.", and writes nothing.

What it writes#

PathWhat it is
index.html, signup.html, …Each page, with every experiment showing control. A page at /docs/setup is docs/setup.html.
index.hero=bold.htmlThe page with one variant showing. There's one file for each variant other than control, of each experiment on the page.
404.htmlThe not-found page, when the site has one
_mk/The stylesheet and scripts, in a folder named after the build's hash
Files from public/Copied at their own paths, with resized copies of images
robots.txt and sitemap.xmlrobots.txt always, and sitemap.xml when you pass --site-url
manifest.jsonThe build's pages, experiments, and scripts, as JSON

Each page is rendered the way a first-time visitor gets it. The files are for looking at, comparing between builds, and testing. There's no server behind them, so nothing assigns variants or records goals. To run experiments, publish the site from the studio. See Publishing and history.

Options#

OptionMeaning
--out <dir>Where to write the build. Required.
--base-path <path>A path the site is served under, such as /s/acme/demo. Links to its pages and files, and its stylesheet and scripts, start with it.
--site-url <url>Where visitors reach the site, such as https://example.com: an http or https URL. It's used for canonical links, share previews, and the sitemap.

The site's name, which pages carry for share previews, is the name of the folder.

Secrets#

A page that fetches data with fetchJson() can send a secret header value with secret("NAME"). In the studio, you set secrets in Data, in the editor's sidebar. The CLI reads each one from an environment variable named MOCKLY_SECRET_ followed by the secret's name:

Terminal
$ export MOCKLY_SECRET_CMS_TOKEN="your-token"$ mockly build ./site --out ./out

An empty variable counts as unset, and a secret that isn't set fails the build with MK6003. mockly check doesn't fetch data, so it doesn't need secrets. See Data from APIs.

In CI#

Run mockly check on every change. It exits with 1 when the site has an error, which fails the step:

Terminal
$ mockly check ./site

Some problems only show up when the pages render: a page that throws while rendering (MK1014), data that can't be fetched (MK6002), or a variant that looks exactly like control (MK4018, a warning). Run mockly build to catch those too:

Terminal
$ mockly build ./site --out ./out

Warnings don't fail either command. To fail on them as well, read the JSON. For example, with jq, which exits with 1 when the expression is false:

Terminal
$ mockly check ./site --json | jq -e '.diagnostics | length == 0'