Skip to main content

Technical reference

@brightspot/styleguide provides the styleguide-bundle CLI, a local development server, and the Handlebars rendering pipeline that renders JSON fixtures through Handlebars templates using a Brightspot-specific helper set.

Dependencies​

@brightspot/styleguide depends on:

  • handlebars for template compilation and rendering.
  • chance for generating placeholder data during substitution.
  • lodash and traverse for data manipulation and JSON tree walking.
  • java-props for loading .properties files used by the format helper.
  • @messageformat/core for ICU message formatting in format.
  • express for the local development server.
  • archiver for bundle zip creation.
  • puppeteer / puppeteer-cluster for screenshot generation in styleguide-bundle.

Starting with 5.0.0, webpack and webpack-dev-server are optional peer dependencies. The development server helper configures your project's own webpack-dev-server, so your project installs and controls both versions.

Installation​

Add the package to a project with yarn or npm:

1
yarn add --dev @brightspot/styleguide

Choosing a version​

The 4.9.x and 5.x release lines both include the dependency security fixes from the 4.9.0 release. They differ in the front-end toolchain they expect from your project.

4.9.x5.x
Node.js22.12 or later26 or later
webpack55.101 or later
webpack-dev-server35 or 6
Changes to your projectNoneToolchain and development server configuration
  • 4.9.x is a drop-in upgrade from 4.8.x. You change the version number in package.json and reinstall; no configuration changes are needed.
  • 5.x targets the current toolchain: webpack-dev-server 5 or 6, Express 5, and current versions of every dependency. Choose it when you are ready to upgrade your project's own toolchain.

The styleguide-bundle CLI, its options, and the PROXY_TARGET proxy mode work the same in both lines.

Upgrading from 4.x to 5.x​

To upgrade a project to 5.x:

  1. Use Node.js 26 or later. Update .nvmrc, the engines field in package.json, and any Gradle node.version setting together.
  2. Upgrade webpack to 5.101 or later, webpack-dev-server to 5 or 6 (6 is recommended), and webpack-cli to 7.
  3. Remove before, overlay, and publicPath from your project's devServer configuration, and move any custom middleware into setupMiddlewares. To change the error overlay, set client.overlay.
  4. Remove HotModuleReplacementPlugin if your project adds it.
  5. Add any package your project uses directly to its own package.json. In 4.x, the styleguide installed the following packages as dependencies, so a project could use them without declaring them. 5.x no longer installs them: stylelint, stylelint-config-prettier, stylelint-config-recommended, stylelint-order, stylelint-prettier, archiver, node-fetch, messageformat, capital-case, and isbinaryfile. glob and chalk are still installed, but at new major versions (14 and 6); declare your own versions if your scripts use the old APIs. stylelint-config-prettier has no release for stylelint 15 or later, so remove it from your stylelint configuration when you upgrade stylelint.

The styleguide.webpack(rootDir, webpack, config) helper keeps the same signature. In 5.x it sets setupMiddlewares and devMiddleware.publicPath instead of the webpack-dev-server 3 options, defaults client.overlay to compile errors only (matching overlay: true in webpack-dev-server 3) unless your project sets it, and it no longer adds HotModuleReplacementPlugin, because webpack-dev-server adds it when hot is on.

The styleguide does not require these upgrades, but making them alongside 5.x brings the rest of a project current. The default and light bundles in the Brightspot starter packs are a working reference for all of them.

  • Babel 8. Requires Node.js ^22.18.0 || >=24.11.0. The useBuiltIns and corejs options are gone from @babel/preset-env. Use babel-plugin-polyfill-corejs3 with method: "usage-global", and set targets at the top level of the Babel options, because the polyfill plugin ignores targets passed to the preset. Babel 8 includes class properties and dynamic import, so remove @babel/plugin-proposal-class-properties and @babel/plugin-syntax-dynamic-import.
  • ESLint 10. ESLint 10 supports only flat config. Replace .eslintrc with eslint.config.mjs, and use the typescript-eslint package in place of @typescript-eslint/eslint-plugin and @typescript-eslint/parser.
  • Prettier 3. The default trailingComma changed to all. Set "trailingComma": "es5" to keep the previous formatting.
  • TypeScript 6. The current typescript-eslint and fork-ts-checker-webpack-plugin versions rely on TypeScript's existing JavaScript compiler API. TypeScript 7 uses a different native API and is not a drop-in replacement for these tools.
  • Asset loaders. webpack 5 asset modules replace file-loader and url-loader.

API reference​

styleguide-bundle CLI​

@brightspot/styleguide installs a styleguide-bundle executable that packages a styleguide project into a zip:

FlagDescription
-r, --root-dirDirectory containing the styleguide project files (_config.json, templates, JSON fixtures). Defaults to styleguide.
-b, --build-dirDirectory containing the compiled front-end assets to include in the bundle. Required.
-o, --output-filePath to write the output zip. Required.
-sSkip generating Puppeteer screenshots of each style and template.
-g, --generate-htmlPre-render each JSON fixture to a static HTML file and include it in the zip.
-c, --puppeteer-cluster-options-filePath to a JSON file of custom puppeteer-cluster options.
-p, --puppeteer-options-filePath to a JSON file of custom Puppeteer options.
note

The --no-screenshots long form for -s doesn't currently work—it throws Unknown argument: screenshots under the CLI's strict argument parsing, because yargs' built-in --no--prefix negation intercepts it before the alias resolves. styleguide-bundle --help still lists -s, --no-screenshots together, since yargs prints declared aliases regardless. Use -s until this is fixed.

The local development server (used during template development) serves the styleguide UI and, for any requested HTML path, resolves and renders the matching JSON fixture on the fly.

Handlebars helpers​

Every helper below is available in .hbs templates in a styleguide project.

HelperDescription
eq, ne, lt, le, gt, geComparison helpers. Usable as block helpers ({{#eq a b}}...{{/eq}}) or inline with then/otherwise hash arguments.
fallbackReturns the first argument that is not null or undefined.
and, or, notBoolean logic helpers, usable as block or inline helpers the same way as the comparison helpers.
add, subtract, multiply, divide, remainderArithmetic helpers.
set, getset is a block helper that assigns shared key/value pairs for the duration of the block; get reads a value set by an enclosing set.
resizeLooks up a named crop from a field's resolved image sizes (see Configuration) and renders the block against that resized image.
cycleRound-robins through a fixed list of positional values by iteration index. Without repeatLast, the whole list repeats in order. With a repeatLast count, the first pass through the list still returns each value in order; every iteration after that cycles only through the last repeatLast values (clamped to the list length) instead of restarting the full list.
partitionSplits an array into fixed-size chunks and iterates over the chunks, similar to {{#each}}.
cdnPasses a path through unchanged; a theming hook for projects that rewrite asset paths.
extraAttributesRenders a root-level extraAttributes map as an escaped HTML attribute string.
jsonObjectRenders root.jsonObject as a JSON string when called with no argument, or the passed value when one is given. Returns a SafeString, so the JSON is emitted unescaped.
truncateTruncates text to a word or character count, with an optional suffix.
escapeHtmlEscapes a string for safe inclusion as text.
formatLooks up an internationalized message from the project's .properties files and formats it with @messageformat/core. Throws on a missing key.
includeRenders a named partial template, merging the current context with any hash arguments.
renderRenders a field's value using the same rules the CMS uses. See below.
styledToggles a BEM nesting flag around its block, letting a nested render call know it is inside a styled wrapper.
styledTemplateRenders its block if the given template file matches the current style override set by render (or if default=true is passed), otherwise renders the inverse block.

render​

{{render <field>}} and {{render <field> "<ViewClassName>"}} render a field's value:

  • If the field's value is falsy, render outputs nothing.
  • If the value is a single-element array, render unwraps it automatically. For arrays with more than one element, render logs a console warning and falls back to concatenating each element's own rendered output; use {{#each array}}{{render this}}{{/each}} instead for clearer results.
  • If the value is an object with a nested view (it has a _template), render recurses into that nested view's own template and wraps the result as raw HTML.
    • Any hash pair passed to render maps a base template path to a style override path, for example {{render this [/page/list/PageList.hbs]="/page/list/PageListStandardD.hbs"}}: if the object's own _template matches the hash key, render substitutes the hash value as _styledTemplate before recursing. The styledTemplate helper checks that value to decide which block to render.
  • If the value is a plain string or other primitive:
    • Passing a second, positional argument—a Brightspot View class name, for example {{render description "...RichTextArticleBodyViewBodyField"}}—tells render that the CMS would parse this value as a SafeString, so it outputs the value as raw, unescaped HTML.
    • Without that second argument, render outputs the value as plain text, which Handlebars escapes normally.

Because the second positional argument to render is always a view class name and never a hash pair, checking that a string was passed there is enough to detect this case—there is no need to check the class name's spelling. Values that come from a fixture's {{html(...)}} or {{paragraphs(...)}} substitution (see Configuration) already arrive as a SafeString and are output as raw HTML regardless of whether a view argument is passed.

note

Passing the view-class-name argument disables HTML escaping for that value. Only pass it for fields the CMS itself would render as a SafeString (rich text and similar HTML-producing fields)—the argument exists to reproduce CMS escaping behavior, not to force raw HTML for arbitrary fields. For a plain-text field with no markup the escaped and unescaped output are identical, but passing the argument speculatively on a field that later receives markup would let that markup through unescaped.

Configuration​

A styleguide project's root directory (the CLI's --root-dir, styleguide by default) must contain a _config.json file, validated against the following fields:

FieldDescription
name, versionProject name and version. If omitted, fall back to the nearest package.json found by walking up from the current working directory (not --root-dir), or to the directory name and unversioned if none is found.
colorPaletteNamed colors available to templates and the styleguide UI.
stylesThe project's styles, each with an example fixture, one or more templates, and optional per-style fields.
themeFieldsTheme-wide fields available across all styles.
imageSizesNamed image size and crop definitions used to resolve _image placeholders and the resize helper.
imageProviderOptional identifier for the image provider used to resolve image URLs.
navigationThe styleguide UI's navigation structure, grouping styles into named pages.
variablesTheme variables available to fixtures through the var() substitution method.
randomSeedOptional seed for the placeholder-data generator, for reproducible fixture output.
staticFilesOptional glob patterns for additional files to copy into a bundle.
disableInspectorOptional. When true, disables the inspector overlay entirely: it skips the comment injection the overlay reads from, and skips the AST rewrite during template compilation that annotates templates for it in the first place.

A project can also define _helpers.js in its root directory: raw JavaScript, evaluated against the project's Handlebars instance, for registering additional custom helpers alongside the built-in set. It executes with full Node privileges at bundle and serve time, and is bundled into the delivery zip.

Fixture substitution​

JSON fixture files support embedded {{methodName(...)}} expressions inside string values, evaluated before Handlebars rendering. Common methods include date(), name(), number(), words(), sentences(), paragraphs(), var() (reads a value from variables in _config.json), and html(...), which wraps its argument as a SafeString so it renders as raw HTML. Fixtures also support _include (pulling in another fixture file, optionally selecting a random or keyed entry from an array) and _template/_wrapper (nesting a fixture's data inside a parent fixture).

Was this page helpful?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.