Browser runtime
The browser runtime turns Less into a page service. It finds Less stylesheets in the document, loads their source and imports through browser requests, compiles them in the shared Less engine, and places the resulting CSS back into the page. It also manages browser options, refresh and watch behavior, cache use, plugins, logs, and user-facing errors.
Runtime model
The runtime connects three browser concerns around the shared compiler: page discovery, resource access, and DOM output. A linked stylesheet or inline style becomes compiler input. The compiler returns CSS or an error. The browser adapter then updates page state rather than returning a file or stream result.
This diagram shows the main browser path from page markup to rendered CSS.
flowchart LR
Page[Page markup and runtime options] --> Discover[Find Less styles]
Discover --> Load[Load source and imports]
Load --> Compile[Shared Less compiler]
Compile --> Result{CSS or error}
Result -->|CSS| DOM[Insert or update page styles]
Result -->|Error| Report[Show or log error]
Page --> Refresh[Refresh, watch, or variable change]
Refresh --> Load
References: packages/…/index.js• L20-40 packages/…/index.js• L54-82 packages/…/index.js• L84-144 packages/…/browser.js• L3-57
Browser bootstrap
Bootstrap prepares one browser-scoped Less API and starts page compilation when the page is ready. The bootstrap code is the entry path for the distributed browser build. It makes the API available as window.less so page scripts can control it after startup.
Option assembly
Options come from the browser build defaults, an existing window.less object, and data attributes on the script that loaded Less. The runtime then supplies browser defaults for asynchronous loading, polling, environment, file caching, and automatic page processing. A page can add browser plugins through window.LESS_PLUGINS.
The environment is normally development on local hosts or file-like protocols. Other hosts use production unless the page sets a value. This choice affects watch behavior, logging defaults, and persistent CSS caching.
References: packages/…/bootstrap.js• L6-30 packages/…/add-default-options.js• L8-53 packages/…/utils.js• L12-29
Page load
When onReady is enabled, bootstrap registers the Less links and starts a refresh. In synchronous mode it temporarily hides the page body so uncompiled content does not appear before the first render. The pageLoadFinished promise marks the end of the initial refresh, whether the refresh succeeds or fails, and the temporary style is removed when synchronous loading ends.
The URL hash can start watch mode during bootstrap. Watch mode is a development aid that checks source changes on a timer and replaces generated CSS when a later render succeeds.
References: packages/…/bootstrap.js• L36-67 packages/…/index.js• L152-185
Stylesheet discovery
The runtime treats linked Less sheets and inline Less style blocks as two forms of page input. It keeps the linked sheets in a list for refresh and treats inline blocks as source that can be compiled directly in place.
Linked Less stylesheets
The runtime discovers a link when its rel is stylesheet/less, or when it has a stylesheet relation and a Less MIME type. The supported type pattern accepts text/less and text/x-less. Registration is synchronous so the initial refresh works from a stable list of sheets.
Each linked sheet is loaded with options copied from the runtime. Data attributes on that link can add sheet-specific settings. The loaded source becomes the root file for one render, so its directory and URL guide import lookup and URL handling.
References: packages/…/index.js• L84-107 packages/…/index.js• L188-210 packages/…/utils.js• L12-29
Inline Less styles
An inline style is selected when its type is text/less or text/x-less. The runtime renders its text with the page URL as the source filename. On success it changes the block to CSS and replaces its contents with the generated CSS.
Inline styles are compiled during a refresh while they still have a Less MIME type. After a successful render the runtime changes that block to CSS, so later refreshes do not treat it as new Less input. Inline styles do not use the linked-sheet CSS cache path. Their output stays in the original style element rather than creating a separate generated node.
References: packages/…/index.js• L42-82 packages/…/index.js• L218-282
Browser resource loading
The browser resource layer gives the shared compiler a way to read Less files and plugin files without direct filesystem access. It resolves a request against the current file and page URL, then uses a browser request to return source text and last-modified metadata.
File access
The browser file manager uses XMLHttpRequest for linked sheets and imports. Normal requests are asynchronous. File-like protocols can use the configured file mode, which allows pages opened from local or browser extension resources to select synchronous or asynchronous loading.
The manager keeps a short-lived in-memory file cache for requests when useFileCache is enabled. Refresh can clear this cache before loading so watch mode and explicit reloads can see changed source. A failed request becomes a file error with the requested URL and response status.
References: packages/…/file-manager.js• L21-65 packages/…/file-manager.js• L67-105 packages/…/add-default-options.js• L13-27
Plugin access
Browser plugins use the same browser file access path as stylesheet imports. A plugin named by an @plugin rule is fetched from the page resource space, then passed to the shared plugin lifecycle. Plugins supplied in window.LESS_PLUGINS are added during bootstrap before the browser API starts its page work.
This means browser plugins must be available as browser-loadable resources. They cannot depend on Node's module loader as their normal loading path. See Plugin architecture for the extension lifecycle and Imports and file resolution for the shared import rules.
References: packages/…/plugin-loader.js• L4-21 packages/…/abstract-plugin-loader.js• L12-115 packages/…/import-manager.js• L40-179 packages/…/bootstrap.js• L21-27
CSS cache and refresh
The browser runtime uses two cache layers for different goals. The file cache avoids repeated source requests during one runtime session. The CSS cache can avoid a full client-side compile across page loads when browser storage contains matching source metadata and variable values.
Cache layers
The in-memory file cache is keyed by the resolved request URL. It stores source text for later reads and is cleared by refresh when reload or explicit cache clearing is requested.
The persistent CSS cache is active outside development when localStorage is available. It stores generated CSS with the source path, the source last-modified value, and the modifyVars record. The runtime uses cached CSS only when both the source timestamp and the variable record still match. Storage failures are logged and do not replace compilation as the source of truth.
References: packages/…/file-manager.js• L3-5 packages/…/file-manager.js• L63-65 packages/…/cache.js• L3-41 packages/…/index.js• L109-131
Refresh and watch
refresh processes all registered linked sheets and waits until each sheet has either produced CSS or reported an error. It can reload source, reuse valid cached CSS, or render with a new modifyVars record. modifyVars is the page-facing shortcut for changing variables without changing the Less source.
After a successful linked-sheet render, the runtime creates a CSS style node near the source link. The node has a stable Less-specific identity, so a later refresh replaces the old output instead of adding duplicate styles. Watch mode clears the in-memory file cache on each poll and repeats the linked-sheet load while watch mode remains active.
This diagram shows how a refresh chooses between cached output and a new compilation.
flowchart TD
Start[Refresh request] --> Clear{Reload or clear cache?}
Clear -->|Yes| Drop[Clear in-memory source cache]
Clear -->|No| Read[Read source metadata]
Drop --> Read
Read --> CSSCache{Matching CSS cache?}
CSSCache -->|Yes| Cached[Use cached CSS]
CSSCache -->|No| Render[Compile Less source]
Render --> Save[Save CSS and metadata]
Cached --> Insert[Insert or replace style node]
Save --> Insert
References: packages/…/index.js• L112-131 packages/…/index.js• L146-185 packages/…/index.js• L213-280 packages/…/browser.js• L3-57
Error reporting
The browser adapter turns loading and compilation failures into page diagnostics or logger events. It removes a prior diagnostic when a later render begins for the same source, so a fixed sheet does not leave stale error text on the page.
Error surfaces
HTML reporting is the default surface. In development it adds a styled error block near the start of the page body with the error type, message, source file, line, column, and source extract when available. Console reporting sends the same useful source context to the Less logger instead. A function supplied as errorReporting receives add and remove events for application-defined reporting.
A missing or unreadable source is reported through the same path as a compiler error. The refresh promise rejects on a linked-sheet failure, while the browser can still use the error reporter to show which sheet caused the failure. Logging level defaults are more verbose in development than in production.
References: packages/…/error-reporting.js• L4-39 packages/…/error-reporting.js• L93-169 packages/…/index.js• L121-141 packages/…/index.js• L244-275 packages/…/log-listener.js• L1-41
Browser capability boundary
The browser runtime supports client-side Less compilation with browser resource access, but it does not provide every host feature available to Node. The shared compiler still performs parsing, import processing, evaluation, and CSS generation. The browser adapter supplies the page-facing services around those stages.
Image dimension functions are not supported in the browser runtime. A Less source that asks for image size, width, or height receives a runtime error rather than a guessed value. For the shared transformation stages, see Compiler pipeline. For the Node counterpart, see Node API and lessc.
References: packages/…/index.js• L20-40 packages/…/image-size.js• L4-28 packages/…/render.js• L3-37
Related knowledge
- Node, browser, and CLI integrations explains the shared runtime boundary.
- Node API and lessc explains the Node and command-line counterpart.
- Imports and file resolution explains import sequencing and path rules.
- Plugin architecture explains plugin loading and extension stages.
- Compiler pipeline explains the shared Less-to-CSS transformation.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep stylesheet matching rules aligned with the browser test page setup so a new link type is not silently skipped.
- When option sources or defaults change, check the precedence between window.less, the loading script, and each stylesheet's data attributes.
- When file or CSS cache keys change, check source timestamps,
modifyVars, reload behavior, and development versus production storage. - When generated style-node identity or placement changes, check refresh replacement and media behavior so duplicate or misplaced CSS does not remain in the page.
- When plugin loading changes, check both global browser plugins and
@pluginimports through the browser file manager. - When error reporting changes, check HTML, console, and callback surfaces as well as removal of stale errors after a successful reload.
- When synchronous page loading changes, check that the temporary visibility style is always removed after both success and failure.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A Less link does not match the supported relation and MIME type | The runtime never registers or compiles the sheet | Use rel="stylesheet/less" or a stylesheet relation with a Less MIME type |
| A browser request returns a non-success status | The sheet or import fails with a file error and refresh rejects | Check the resource URL, server response, and browser access policy |
| A browser storage entry has stale source metadata or different modified variables | The runtime recompiles instead of using cached CSS | Treat persistent cache as an optimization and keep source metadata available |
| A refresh adds output without the expected generated-style identity | Old CSS can remain beside new CSS and produce duplicate rules | Preserve the stable style identity and replacement position for each sheet |
| A plugin is available only through a Node module path | Browser plugin loading fails before compilation can use the plugin | Publish or serve a browser-loadable plugin resource |
| A source uses image-size functions in the browser | Compilation stops with an unsupported runtime error | Compute image dimensions before browser compilation or use a supported host |
| Synchronous loading fails before cleanup runs | The page can remain hidden by the temporary loading style | Keep cleanup attached to both refresh outcomes |
Key files
| File | Why to read it |
|---|---|
| packages/…/bootstrap.js | Shows how the browser API starts, combines options, registers stylesheets, and begins the first refresh. |
| packages/…/add-default-options.js | Defines browser defaults and how page location affects environment and loading behavior. |
| packages/…/index.js | Defines stylesheet discovery, inline compilation, refresh, watch, variable changes, and browser service assembly. |
| packages/…/file-manager.js | Defines browser requests, URL resolution, source metadata, and the in-memory file cache. |
| packages/…/browser.js | Shows how generated CSS becomes or replaces a style element in the document. |
| packages/…/cache.js | Defines persistent CSS cache validity and storage behavior. |
| packages/…/plugin-loader.js | Shows how browser plugin source enters the shared plugin loader. |
| packages/…/error-reporting.js | Defines HTML, console, and callback error surfaces. |
| packages/…/log-listener.js | Defines browser log levels and default console listeners. |
| packages/…/image-size.js | Defines the browser capability boundary for image dimension functions. |
| packages/…/common.js | Shows how browser tests wait for page compilation and compare generated CSS and errors. |