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 / compiler-pipeline

Less compiler pipeline

The Less compiler turns Less source into CSS through a staged flow. Render options and host services shape one compilation session, parsing builds a source-aware tree, import loading adds referenced sources, evaluation applies Less meaning, and CSS generation emits the final text with optional source-map data.

Pipeline stages

The shared compiler service exposes parse for the tree result and render for the complete source-to-CSS path. A render call copies the active options, parses the input, transforms the returned root tree, and returns CSS plus optional map and import information. packages/…/index.js• L23-58 packages/…/render.js• L3-40

The full path is shown below. The runtime supplies resource access, while the shared compiler controls language meaning and output.

flowchart LR
    Input[Less source and render options] --> Session[Compilation session]
    Session --> Pre[Pre-process source]
    Pre --> Parse[Parse into source-aware tree]
    Parse --> Imports[Resolve and parse imports]
    Imports --> Eval[Evaluate Less semantics]
    Eval --> Visitors[Transform selectors and tree]
    Visitors --> CSS[Generate CSS]
    CSS --> Post[Post-process CSS]
    Post --> Result[CSS, imports, and optional source map]
    Parse -. error .-> Error[LessError]
    Imports -. error .-> Error
    Eval -. error .-> Error
    CSS -. error .-> Error

References: packages/…/index.js• L23-58 packages/…/render.js• L3-40 packages/…/parse.js• L7-84 packages/…/parse-tree.js• L12-119

Render session

The render session is the shared state for one compilation. It combines caller options with the runtime environment, then creates the parser, import manager, plugin manager, and source-map services that the later stages use. packages/…/index.js• L23-33 packages/…/parse.js• L30-58

Options affect different points in the flow rather than one single output switch:

Option concernPipeline effect
Input identity and pathsEstablishes file context for imports, URLs, diagnostics, and maps.
Variable injectionAdds values before or after source declarations during parsing and evaluation.
Math and unitsControls which operations evaluate and whether their units must be valid.
URL behaviorControls URL rewriting, root paths, and URL arguments during evaluation.
CompressionChanges CSS output formatting and is passed to the CSS generator.
PluginsAdds source processors, functions, visitors, file managers, and CSS processors.
Source mapsAdds location tracking to CSS output and returns map data.

The session can use callback or Promise forms. Both forms report the same result or error; the Promise form wraps the callback path rather than creating a separate compiler flow. packages/…/parse.js• L8-27 packages/…/render.js• L4-23

References: packages/…/index.js• L23-33 packages/…/parse.js• L8-58 packages/…/render.js• L4-23 packages/…/contexts.js• L15-65 packages/…/default-options.js• L2-88

Source preparation

Source preparation makes input consistent before the parser reads it. Pre-processors can change the source first. Injected global variables and a banner are placed before the source. Modified variables are placed after it, so the resulting text reflects their intended precedence. packages/…/parser.js• L290-321

The compiler records how much text it inserted at the start of each file. That count lets diagnostics and source maps ignore injected text when they point back to the user's source. packages/…/parser.js• L301-321 packages/…/source-map-output.js• L57-80

References: packages/…/parser.js• L290-321 packages/…/source-map-output.js• L57-80

Parsing and tree construction

Parsing converts the prepared text into a root ruleset made of typed nodes. The tree preserves nested rules, selectors, declarations, values, Less references, and source locations so later stages can apply meaning without reparsing raw CSS-like text. See Parser and syntax tree for the tree model. packages/…/parser.js• L323-335

The parser does not finish the compilation. It returns a structured root and passes it to import processing when imports are enabled. This handoff keeps syntax recognition separate from file loading and semantic evaluation. packages/…/parser.js• L341-394

References: packages/…/parser.js• L323-394 packages/…/node.js• L69-127 packages/…/ruleset.js• L39-89

Import resolution

Imports connect the root tree to other Less or CSS resources. The import stage evaluates an import's mode, asks the runtime for a matching file manager, loads the resource, and parses Less content into another tree. It then attaches or replaces the import node so later stages see the resolved source. See Imports and file resolution for the import lifecycle. packages/…/import-visitor.js• L42-100 packages/…/import-manager.js• L40-79

The import manager also tracks loaded contents and files. This record supports reuse rules, optional and inline imports, dependency reporting, URL context, diagnostics, and source maps. CSS imports can remain CSS imports, while Less imports become tree content for evaluation. packages/…/import-manager.js• L17-30 packages/…/import-visitor.js• L100-140

The environment selects file managers by asking them whether they support the requested path. Node and browser runtimes provide different resource access, but the compiler receives the same kind of loaded content and resolved filename. See Runtime integrations for host boundaries. packages/…/environment.js• L28-47 packages/…/file-manager.js• L18-53 packages/…/file-manager.js• L67-100

References: packages/…/import-visitor.js• L42-140 packages/…/import-manager.js• L17-79 packages/…/environment.js• L28-47 packages/…/file-manager.js• L18-53 packages/…/file-manager.js• L67-100

Evaluation and CSS generation

Evaluation gives the parsed tree its Less meaning. It resolves variables and mixins, evaluates guards, functions, operations, URLs, and imports, then produces a tree that represents the CSS-ready result. See Evaluation and CSS generation for the semantic stages. packages/…/transform-tree.js• L10-43 packages/…/transform-tree.js• L85-101

Visitors apply ordered tree changes around evaluation. The core sequence joins nested selectors, applies extend, marks output visibility, and prepares CSS generation. Plugin visitors can run before or after these core visitors. packages/…/transform-tree.js• L44-83 packages/…/index.js• L1-15

CSS generation walks the evaluated tree and emits declarations, selectors, at-rules, comments, and formatting into a CSS result. The root tree is not itself a CSS wrapper, so its children become the top-level output. Post-processors can then change the generated CSS and receive source-map context when a map is active. See Functions and value processing and Plugin architecture for related extension behavior. packages/…/parse-tree.js• L22-35 packages/…/parse-tree.js• L103-107

References: packages/…/transform-tree.js• L44-101 packages/…/index.js• L1-15 packages/…/parse-tree.js• L12-35 packages/…/parse-tree.js• L103-107 packages/…/to-css-visitor.js• L86-90

Extension stages

Plugins add controlled work at several points in the pipeline. A pre-processor changes source before parsing. A function changes value processing during evaluation. A visitor changes the tree before or after evaluation. A file manager changes resource access. A post-processor changes CSS after generation. See Plugin architecture for the extension contract. packages/…/plugin-manager.js• L4-15 packages/…/plugin-manager.js• L53-97

The plugin manager keeps these hooks in the compilation session. Their order is part of the result: source processors run before parsing, pre-evaluation visitors run before tree evaluation, normal visitors run around CSS preparation, and post-processors run after CSS generation. packages/…/parser.js• L304-308 packages/…/transform-tree.js• L60-99 packages/…/parse-tree.js• L103-107

References: packages/…/plugin-manager.js• L4-15 packages/…/plugin-manager.js• L53-97 packages/…/parser.js• L304-308 packages/…/transform-tree.js• L60-99 packages/…/parse-tree.js• L103-107

Errors and diagnostics

Errors can occur while preparing source, parsing syntax, loading imports, evaluating Less constructs, running extensions, or generating CSS. The pipeline wraps internal failures as LessError objects and carries import state so callers can see the source file, location, type, and nearby source lines. packages/…/parser.js• L73-82 packages/…/parse-tree.js• L17-20 packages/…/less-error.js• L27-88

Parsing reports an error when the input cannot be consumed as a complete Less source. Import failures retain the import location when the original file manager error has no source location. Evaluation and output errors are wrapped at the tree boundary, so both parse and render callers receive a consistent error shape. packages/…/parser.js• L341-381 packages/…/import-visitor.js• L100-106 packages/…/parse-tree.js• L17-20

Warnings use the same source context but do not stop the compile. Quiet options and deprecation settings control warning output. The error formatter can show a marked source extract for a human reader. packages/…/parser.js• L97-117 packages/…/less-error.js• L109-163

References: packages/…/less-error.js• L27-88 packages/…/less-error.js• L109-163 packages/…/parser.js• L73-117 packages/…/parser.js• L341-381 packages/…/import-visitor.js• L100-106 packages/…/parse-tree.js• L17-20

Source maps

Source maps connect generated CSS positions to the Less files that produced them. When enabled, CSS generation writes output through a source-map writer instead of directly to a string. The writer uses imported file contents and node source positions to add mappings, then the result includes map data and a CSS annotation when configured. packages/…/parse-tree.js• L36-110 packages/…/source-map-builder.js• L7-34

The map can be external or inline. Its input and output names, base paths, root path, source contents, and annotation URL determine how tools find the original Less source. The compiler derives useful names from the input or output file when callers do not provide them. packages/…/parse-tree.js• L41-94 packages/…/source-map-builder.js• L36-58

Source maps depend on source identity as well as source position. Imports must record their loaded contents, and inserted variables must record ignored leading characters. A missing runtime source-map generator can produce a fallback map implementation with no useful mappings. packages/…/source-map-output.js• L48-75 packages/…/source-map-output.js• L114-146 packages/…/environment.js• L5-11

References: packages/…/parse-tree.js• L36-110 packages/…/source-map-builder.js• L7-78 packages/…/source-map-output.js• L42-75 packages/…/source-map-output.js• L114-146 packages/…/environment.js• L5-11

Runtime handoff

The compiler core does not decide how a file is read or where CSS is placed. The environment supplies file managers and helper services. Node returns the result to the caller or CLI. The browser loads Less resources and inserts the resulting CSS into the page. See Runtime integrations for the runtime life cycles. packages/…/environment.js• L8-47 packages/…/index.js• L15-29 packages/…/index.js• L54-82

This boundary lets the same pipeline compile source in different settings. The compiler asks for a supported resource service and returns CSS or a structured error. The runtime then decides whether to write a file, print output, or update a document. packages/…/environment.js• L28-52 packages/…/browser.js• L3-57

References: packages/…/environment.js• L8-52 packages/…/index.js• L15-29 packages/…/index.js• L54-82 packages/…/browser.js• L3-57

Change checklist

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

  • Keep option ownership aligned with the stage that consumes it. An option copied only into parse context or only into evaluation context can silently change import, URL, math, or output behavior.
  • Keep source preparation counts aligned with source contents. Injected text must not shift diagnostics or source-map locations.
  • Keep import completion aligned with parse completion. Async and optional imports must finish before the render result is returned.
  • Keep visitor order aligned with tree state. A visitor that runs too early can see unresolved nodes. A visitor that runs too late can miss the structure it must change.
  • Keep file-manager results aligned with source identity. Resolved filenames and loaded contents feed imports, errors, dependency lists, and source maps.
  • Keep source-map option derivation aligned with CSS output names. A map URL or output filename that changes in one stage can produce an unusable annotation.
  • Keep post-processor source-map behavior aligned with the chosen output. A processor that changes CSS positions may need to update or replace the map.

Important failure modes

TriggerConsequenceGuard
A file manager cannot load an importRendering stops with a file error or an optional import is skippedProvide a manager for the resource and mark imports optional only when omission is valid
An import completes after parsing reports successThe render stage can produce CSS before all source is attachedKeep import completion in the parser's finish path
A plugin visitor runs before the tree has the nodes it expectsThe plugin has no effect or changes incomplete stateSet the visitor phase and order to match its input contract
Injected variables are counted as source textError extracts and source-map positions point into generated setup textTrack ignored leading characters for each source file
A Less evaluation error reaches CSS generationNo CSS result is returned and the caller receives a wrapped Less errorPreserve node file information and wrap errors at the parse-tree boundary
A source-map annotation does not match the map locationDeveloper tools cannot find the original Less sourceKeep map filename, URL, base path, and output filename consistent
The host lacks a source-map generatorCSS may be returned with no useful source mappingsSupply a source-map generator in the runtime environment

Key files

FileWhy to read it
packages/…/index.jsDefines the shared compiler service and its public entry points.
packages/…/render.jsShows the end-to-end render handoff from parse to CSS result.
packages/…/parse.jsShows session setup, options, plugins, and parser entry.
packages/…/parser.jsShows source preparation, tree creation, import completion, and parse errors.
packages/…/parse-tree.jsConnects evaluation, CSS generation, post-processing, source maps, and imports.
packages/…/transform-tree.jsShows evaluation and the ordered core visitor stages.
packages/…/import-manager.jsShows the resource handoff and imported-source tracking.
packages/…/import-visitor.jsShows how import nodes become resolved tree content.
packages/…/plugin-manager.jsShows the extension points that can change each pipeline stage.
packages/…/less-error.jsDefines the shared diagnostic shape and source extracts.
packages/…/source-map-builder.js and packages/…/source-map-output.jsShow how CSS output receives mappings and annotations.
packages/…/environment.jsDefines the boundary between compiler stages and runtime resource access.
packages/…/index.jsShows the Node services connected to the shared compiler.
packages/…/index.jsShows how browser resources enter the compiler and how CSS returns to the page.