Introducing a new way to review your PRs.

Read the announcement

less / less.js

Less. The dynamic stylesheet language.

View on GitHub

17 documents · Updated Sep 7, 2026

docs / plugin-architecture

Plugin architecture

Less plugins extend one render session with new functions, tree visitors, source processors, CSS processors, or file managers. The plugin system loads these extensions at the point where they are needed and connects them to parsing, evaluation, file lookup, and CSS output. This page sits below the compiler pipeline; see imports and file resolution, evaluation and CSS generation, Node API and lessc, and browser runtime for the surrounding flows.

Plugin session

Plugin registration

Each parse or render request receives a plugin session. The session holds installed plugins and the extension points that they register. A new session is the default; the reUsePluginManager option allows state to carry into a later request. Programmatic plugins are registered before the main Less source is parsed. packages/…/parse.js• L30-83

The system accepts either plugin objects or plugin source. A source plugin can expose its object through module.exports or through registerPlugin. The loader checks the plugin version before it allows the plugin to affect the session. packages/…/abstract-plugin-loader.js• L50-76

References: packages/…/parse.js• L30-83 packages/…/plugin-manager.js• L4-41 packages/…/abstract-plugin-loader.js• L50-76

Plugin loading

Plugin loading follows the same file lookup path as other imports, but it requests JavaScript content and then evaluates that content as a plugin. A plugin may enter through the programmatic plugin list or through the deprecated @plugin Less directive. The directive remains supported, but the parser recommends the programmatic API or the CLI plugin option for new use. packages/…/parser.js• L2277-2314

The loader caches a source plugin by its resolved file name. A later use of the same plugin does not install it again. Instead, the loader applies the new options and runs the per-use hook. This lets one plugin support more than one @plugin use without repeating its session setup. packages/…/abstract-plugin-loader.js• L18-46

The main loading sequence is:

flowchart LR
    Request[Render request] --> Session[Plugin session]
    Session --> Initial[Programmatic plugins]
    Source[Less source] --> Directive[@plugin directive]
    Directive --> Lookup[File lookup]
    Initial --> Install[Install plugin]
    Lookup --> Evaluate[Evaluate plugin source]
    Evaluate --> Install
    Install --> Extensions[Registered extensions]
    Extensions --> Parse[Parse and evaluate Less]
    Parse --> CSS[CSS output]

References: packages/…/import-manager.js• L40-177 packages/…/abstract-plugin-loader.js• L12-115 packages/…/parser.js• L2279-2314

Plugin activation

Installation is the session-level setup step. The plugin receives the Less API, the plugin session, and a function registry. It can use these objects to register any combination of extension types. The loader then runs the plugin's per-use hook for a source plugin. packages/…/plugin-manager.js• L34-41

Plugin options have a version-aware order for compatibility. Older or unspecified plugins receive options before installation, while plugins that require Less 3 or later receive them after installation. The loader applies options again after registration so functions created during installation are available when options are processed. packages/…/abstract-plugin-loader.js• L76-107

References: packages/…/plugin-manager.js• L34-41 packages/…/abstract-plugin-loader.js• L76-115 packages/…/import.js• L184-205

Extension points

Custom functions

A custom function is a named operation that Less can call while it evaluates an expression. Plugins add one function with add or a group with addMultiple; the function can return a Less value or tree node for later output. The function registry compares names in lower case and can inherit functions from an outer scope. packages/…/function-registry.js• L1-33

Function scope follows the plugin's loading context. Programmatic registration can add functions to the shared registry, while functions declared by an imported plugin can be added to the registry for that import scope. A child scope can shadow a parent function without changing the parent. Function calls consult this registry during evaluation. packages/…/import.js• L184-205 packages/…/call.js• L35-70

References: packages/…/function-registry.js• L1-33 packages/…/plugin-manager.js• L34-41 packages/…/import.js• L184-205 packages/…/plugin-simple.js• L1-7

Tree visitors

A visitor is a plugin extension that walks the parsed Less tree. It can inspect nodes, change nodes, remove nodes, or stop descent into a node. A replacing visitor returns the node that should continue through the pipeline. packages/…/visitor.js• L33-99

Visitors have a place in the evaluation and output passes. A pre-evaluation visitor runs before Less evaluates the tree. A normal pre-visitor runs before the built-in visitors, while a normal visitor runs after them. The visitor list also supports visitors added during the pass, so a plugin can register work in response to an earlier stage. packages/…/transform-tree.js• L44-99

References: packages/…/transform-tree.js• L44-99 packages/…/visitor.js• L33-129 packages/…/plugin-preeval.js• L1-27

Source preprocessors

A source preprocessor changes Less text before the parser turns it into a tree. The plugin session keeps preprocessors in priority order, then the parser applies them to each source file before it normalizes and parses the text. This extension point is suited to source-wide syntax or text changes that must happen before parsing. packages/…/parser.js• L304-321

Priority values define order from low to high. The API uses low values for work before imports, a middle value for import-related work, and high values for work after imports. The exact priority contract belongs to the processors that share a session. packages/…/plugin-manager.js• L61-74

References: packages/…/plugin-manager.js• L61-74 packages/…/parser.js• L304-321 packages/…/styles.config.cjs• L1-7

CSS postprocessors

A CSS postprocessor changes the completed CSS string after Less has evaluated the tree and produced CSS. The session orders postprocessors by priority and applies them in that order. Each processor receives the CSS plus output context such as source-map data, options, and imports. packages/…/parse-tree.js• L99-119

Postprocessor priorities describe intended placement around output stages. A processor must preserve the output contract expected by the next processor because each result becomes the next input. packages/…/plugin-manager.js• L76-89

References: packages/…/plugin-manager.js• L76-89 packages/…/parse-tree.js• L99-119 packages/…/styles.config.cjs• L1-7

File managers

A file manager is a resource adapter that tells Less whether it can handle a path and then loads that resource. Plugins use this extension point to support file types, storage systems, or import sources that the built-in managers do not cover. The manager contract supports asynchronous loading and optional synchronous loading. packages/…/file-manager-api.ts• L3-77

For each request, Less checks plugin file managers before the built-in managers because it searches the combined list from the end. The first manager that supports the path owns the load. This makes a plugin manager a deliberate override for a matching import or plugin path. packages/…/environment.js• L28-47

References: packages/…/file-manager-api.ts• L3-77 packages/…/environment.js• L28-52 packages/…/plugin-manager.js• L91-155 packages/…/filemanager.less• L1-4

Runtime loaders

Node loader

The Node loader resolves local plugin paths and package names, then uses the selected file manager to read the plugin. A bare name can use the Less plugin package prefix, while an explicit path or JavaScript file keeps its stated form. Node also supports synchronous plugin loading for synchronous import flows. packages/…/plugin-loader.js• L10-59

The Node runtime combines a local file manager with an optional HTTP file manager. This lets the same plugin lifecycle handle local source, package-based plugins, and supported URL imports while leaving path policy to the selected manager. packages/…/index.js• L15-23

References: packages/…/plugin-loader.js• L10-59 packages/…/index.js• L15-23 packages/…/file-manager.js• L8-18 packages/…/url-file-manager.js• L16-23

Browser loader

The browser loader fetches plugin source through the browser file manager and then passes the source to the common plugin evaluator. It does not provide Node's module resolution behavior, so browser plugin availability depends on resources that the browser can fetch. packages/…/plugin-loader.js• L4-21

The browser file manager uses URLs and XHR to load Less and plugin resources. The browser runtime installs this manager into the shared environment before rendering sheets. packages/…/file-manager.js• L8-112 packages/…/index.js• L20-35

References: packages/…/plugin-loader.js• L4-21 packages/…/file-manager.js• L8-112 packages/…/index.js• L20-35

Change checklist

When these files or sections are changed, remember to consider these:

  • Changes to plugin option timing must preserve both older plugins that expect options before install and newer plugins that expect them after installation.
  • Changes to file-manager registration order must preserve the override rule for plugin managers and the sync-versus-async selection contract.
  • Changes to visitor insertion or evaluation order must preserve the distinction between pre-evaluation, pre-output, and post-output visitors.
  • Changes to function registry scope must preserve parent lookup and local shadowing across imported plugin files.
  • Changes to plugin caching must preserve the difference between one-time installation and per-use activation.
  • Changes to browser or Node loading must keep both loaders aligned with the shared plugin evaluator and file-manager contract.

Important failure modes

TriggerConsequenceGuard
A plugin declares options but has no setOptions hookPlugin loading returns a Less error instead of activating the pluginProvide setOptions when callers can pass plugin options
A plugin requires a newer Less version than the current runtimeThe loader rejects the plugin before it can register extensionsSet a supported minVersion and check it against the runtime
A custom file manager claims a path but cannot load itThe matching import or plugin fails even if a later manager could have loaded itMake supports narrow and keep async and sync load behavior consistent
A custom function returns an invalid or missing Less valueEvaluation can fail or the function call can remain unusable in outputReturn a valid Less value or tree node for every supported call
A postprocessor returns output that does not match the next processor's input contractLater processors or CSS consumers receive broken CSS or source-map stateReturn the transformed CSS and preserve the output context when required
A reused plugin session keeps state from an earlier renderA later render can see old plugins, functions, visitors, or managersUse a fresh session unless cross-render state is an explicit requirement

Key files

FileWhy to read it
packages/…/plugin-manager.jsDefines the shared session and its extension points.
packages/…/abstract-plugin-loader.jsDefines plugin source evaluation, validation, caching, options, and activation.
packages/…/import-manager.jsConnects plugin imports to file lookup and the plugin loader.
packages/…/transform-tree.jsPlaces custom visitors around evaluation and built-in tree passes.
packages/…/parse-tree.jsPlaces CSS postprocessors after CSS generation.
packages/…/environment.jsSelects the file manager that handles each path.
packages/…/function-registry.jsDefines function lookup, inheritance, and local scope.
packages/…/plugin-loader.jsShows the Node loading boundary.
packages/…/plugin-loader.jsShows the browser loading boundary.