Node, browser, and CLI integrations
Less uses one shared compiler service and adapts it to three execution settings. Node.js supplies access to local files and network resources. The browser supplies access to web stylesheets and the page DOM. The lessc command-line interface turns the Node.js service into a file and stream based build tool.
Shared compiler service
The shared compiler service accepts Less text and options, resolves imports through the active environment, and returns CSS or a compile error. Each integration creates this service with the resource access and user-facing behavior that fit its setting. See Compiler pipeline for the language processing stages that all three settings share.
The service exposes a common public API for parsing, rendering, compiler data, plugins, functions, and errors. The integration layer keeps that API stable while supplying different file managers and environment services. This separation lets a Node process and a browser compile the same Less input without sharing the same resource or page-control rules.
flowchart LR
Input[Less input and options] --> Adapter[Runtime adapter]
Adapter --> Service[Shared compiler service]
Service --> Resolve[Runtime resource access]
Resolve --> Service
Service --> Output[CSS or compile error]
Output --> Node[Node API]
Output --> Browser[Browser page]
Output --> CLI[lessc output]
References: packages/…/index.js• L23-58 packages/…/render.js• L3-40 packages/…/environment.js• L8-57
Node.js integration
The Node.js integration provides the compiler as a programmatic service for server tools, build systems, and other JavaScript processes. It gives the shared service local file access, optional HTTP(S) import access, source-map support, plugin loading, and image-size support. See Node API and lessc for the Node-facing API and command behavior.
Node import resolution can use the current file, configured search paths, and Node module resolution. A separate URL resource path handles HTTP(S) imports when its optional request support is available. These choices make the Node service useful for both local project trees and selected remote resources.
References: packages/…/index.js• L1-31 packages/…/file-manager.js• L8-154 packages/…/url-file-manager.js• L10-57 packages/…/environment.js• L13-43
Browser integration
The browser integration turns the shared compiler into a page service. It reads Less from linked stylesheets and inline style blocks, fetches imports with browser requests, and writes the resulting CSS into the document. See Browser runtime for browser loading, refresh, watch, and error behavior.
The browser adapter also owns page concerns that do not belong in the compiler. It can cache compiled CSS in browser storage, report errors in the page or through a logger, and replace generated style nodes during refresh. Runtime options can come from the page and from the script that starts Less, so a page can select its loading, watch, cache, and variable behavior.
References: packages/…/index.js• L20-40 packages/…/index.js• L54-82 packages/…/index.js• L146-185 packages/…/index.js• L191-283 packages/…/file-manager.js• L21-105 packages/…/browser.js• L3-57 packages/…/cache.js• L3-41 packages/…/error-reporting.js• L4-169
Browser page lifecycle
The browser lifecycle starts by collecting Less stylesheets. It then renders each source and places the CSS beside its source stylesheet or in the page head. A refresh can reload resources, reuse valid cached CSS, and apply changed variables without changing the Less source.
sequenceDiagram
participant Page as Browser page
participant Runtime as Browser runtime
participant Loader as Browser resource loader
participant Compiler as Shared compiler
Page->>Runtime: Register Less links and style blocks
Runtime->>Loader: Load source and imports
Loader-->>Runtime: Less text and resource metadata
Runtime->>Compiler: Render source with options
Compiler-->>Runtime: CSS or error
Runtime->>Page: Insert or replace CSS
Page->>Runtime: Refresh, watch, or modify variables
Runtime->>Compiler: Render again
References: packages/…/bootstrap.js• L6-30 packages/…/bootstrap.js• L36-67 packages/…/index.js• L146-185 packages/…/index.js• L191-283 packages/…/browser.js• L3-57
lessc command-line interface
lessc is the process-facing adapter for Node.js compilation. It converts command-line arguments into compiler options, loads plugins before rendering, reads a source file or standard input, and sends CSS, source maps, dependency data, diagnostics, or lint results to the matching output channel.
The CLI treats a missing or invalid input, option, plugin, import, or output write as a process failure. It can write CSS to a destination or standard output. It can also report imported files for build dependency tracking and create external or inline source maps. See Node API and lessc for the supported command surface.
flowchart TD
Args[Command-line arguments] --> Options[Compiler options]
Options --> Plugins[Plugin loading]
Plugins --> Source[File or standard input]
Source --> Render[Node compiler render]
Render --> CSS[CSS output]
Render --> Map[Optional source map]
Render --> Deps[Optional import dependencies]
Render --> Error[Diagnostic and non-zero exit]
References: packages/…/lessc• L23-35 packages/…/lessc• L69-74 packages/…/lessc• L76-151 packages/…/lessc• L173-265 packages/…/lessc• L268-339 packages/…/lessc• L342-369 packages/…/lessc-helper.js• L21-94
Runtime boundaries
The main boundary between the shared service and an integration is resource access. The compiler asks the environment for a file manager. The active runtime selects the manager that can handle the source location. Plugins can add managers for a request, so an integration can extend import behavior without changing the compiler core.
The output boundary is also runtime-specific. Node callers receive a result through the API. The browser turns CSS into page state. The CLI turns it into process output and an exit status. This keeps page mutation and process I/O outside the shared language service.
| Runtime | Main input | Resource access | Main result |
|---|---|---|---|
| Node.js | Less text and API options | Local files, Node modules, and supported HTTP(S) URLs | Promise or callback result |
| Browser | Linked or inline page stylesheets | Browser requests and page metadata | CSS inserted into the document |
lessc | Arguments plus file or standard input | Node.js file and plugin services | CSS, maps, dependencies, or process error |
References: packages/…/environment.js• L28-52 packages/…/index.js• L15-29 packages/…/index.js• L20-35 packages/…/lessc• L268-339
Related knowledge
- Node API and lessc covers the Node service and CLI contract.
- Browser runtime covers page loading, refresh, watch, caching, and browser errors.
- Compiler pipeline covers the shared transformation from Less source to CSS.
- Packaging and builds covers how runtime forms are assembled and distributed.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the runtime entry points aligned with the package export conditions and the
lesscexecutable target. - When a file manager changes, check both import resolution and the runtime-specific error or metadata returned to callers.
- When browser stylesheet or cache behavior changes, check the relationship between refresh completion, generated style nodes, and modified variables.
- When CLI option handling changes, check its effect on plugin loading, source-map paths, dependency output, and process exit status.
- When shared compiler API construction changes, check that Node and browser adapters still attach their runtime services to the same public API.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A Node import has no matching file manager or optional HTTP support is absent | Rendering fails with a file error | Use a supported local path or provide the required runtime support |
| Browser source or import loading returns a non-success response | The sheet does not render and the browser reports the source error | Check browser request access and resource URLs |
| Browser refresh cannot use matching cache metadata | The runtime renders the source again | Treat cache as an optimization, not as the source of truth |
lessc receives no input or a required option is missing | The command prints usage or an error and exits with failure | Supply the source and required destination or option values |
lessc cannot write CSS or a source map | The output is incomplete and the process exit status is non-zero | Ensure the destination is writable and its parent can be created |
| A CLI plugin fails to load or conflicts with plugin-rule settings | Compilation does not start | Install the plugin and use compatible plugin options |
Key files
| File | Why to read it |
|---|---|
| packages/…/index.js | Defines the shared public compiler service that each runtime adapts. |
| packages/…/environment.js | Shows how runtime resource managers are selected. |
| packages/…/index.js | Defines the Node.js compiler entry point and its runtime services. |
| packages/…/file-manager.js | Explains the Node local-file and module import boundary. |
| packages/…/url-file-manager.js | Explains supported remote imports in Node.js. |
| packages/…/index.js | Defines browser setup, stylesheet loading, refresh, and watch behavior. |
| packages/…/file-manager.js | Explains browser resource loading and file cache input. |
| packages/…/browser.js | Shows how compiled CSS becomes page style state. |
| packages/…/bootstrap.js | Shows how the browser runtime starts during page load. |
packages/less/bin/lessc | Defines the command-line input, render, output, diagnostics, and exit flow. |
| packages/…/lessc-helper.js | Defines the CLI usage surface presented to users. |
| packages/…/package.json | Maps package consumers to Node, browser, distribution, and CLI entry points. |