Imports and file resolution
Less imports connect one source file to other Less, CSS, and plugin resources. The import flow evaluates each import in its source context, asks the active runtime for a file manager, loads the resource, and incorporates the result into the compilation before CSS generation. This page expands the import stage in the Less compiler pipeline and complements Parser and syntax tree.
Import lifecycle
An import starts as an import node in the syntax tree. The compiler later resolves its path and mode, loads the resource, parses Less content when needed, and makes the result available to evaluation. The parser does not treat file loading as a separate user-visible compile; import completion is part of the parse result.
The main flow is:
flowchart TD
Source[Less source] --> Node[Import node]
Node --> Mode[Evaluate import mode and path]
Mode --> Manager[Select file manager]
Manager --> Load[Load resource]
Load --> Kind{Resource mode}
Kind -->|Less| Parse[Parse imported Less]
Kind -->|CSS| Keep[Keep CSS import]
Kind -->|Inline| Text[Keep loaded text]
Kind -->|Plugin| Plugin[Load plugin]
Parse --> Attach[Attach result to import node]
Keep --> Attach
Text --> Attach
Plugin --> Attach
Attach --> Eval[Evaluate imports and Less rules]
Eval --> CSS[Generate CSS]
The parser creates the root tree and starts import processing when processImports is enabled. The import visitor walks that tree, while the import manager owns resource loading and the records needed for source identity, errors, and later output. See Compiler pipeline for the stages before and after this flow.
References: packages/…/parser.js• L323-394 packages/…/import-visitor.js• L22-41 packages/…/import-manager.js• L17-30
Import sequencing
Import sequencing preserves Less source order even when file reads finish in a different order. The sequencer holds each import callback until the preceding callback is ready, so later imports cannot change the meaning of an earlier import.
When the visitor finds an import, it follows this sequence:
- It evaluates the import path and options in the current Less scope.
- It queues the load callback in source order.
- It lets the file manager load and parse the resource.
- It runs the callback only when all earlier import callbacks are ready.
- It visits the imported tree so nested imports join the same sequence.
- It finishes parsing only when the sequence has no pending work.
Variable imports use a second queue. The compiler delays them until the imports already found in the current pass have completed. This allows a variable defined by an earlier import to control a later import path without allowing asynchronous timing to decide the result.
References: packages/…/import-sequencer.js• L1-53 packages/…/import-visitor.js• L42-55 packages/…/import-visitor.js• L59-99 packages/…/import-visitor.js• L132-154 packages/…/parser.js• L373-394
Import options
Import options select whether the resource becomes Less input, remains a CSS import, is included as text, or may be omitted. The parser accepts these options inside the import declaration. The css and once spellings are translated to the internal forms less: false and multiple: false.
| Option | Compilation effect |
|---|---|
less | Treats the loaded resource as Less source. |
css | Keeps the resource as a CSS @import instead of evaluating its Less rules. |
inline | Places the loaded text into the output as content. Media features can wrap that content. |
multiple | Allows the same resolved resource to be incorporated more than once. |
once | Uses the normal single-incorporation behavior. |
reference | Makes the resource available for use without making its ordinary definitions direct output. |
optional | Skips the resource when it cannot be found instead of failing the compile. |
Less infers a CSS import from a .css path when no explicit mode is given. An explicit mode or inline option takes priority over that inference. A path whose mode is still unknown can cause the loader to try the Less extension; plugin loading uses the JavaScript extension instead.
References: packages/…/parser.js• L2015-2048 packages/…/parser.js• L2052-2088 packages/…/import.js• L44-75 packages/…/import-manager.js• L143-155
File manager selection
The environment is the resource boundary between the shared compiler and its host. A file manager declares whether it supports a requested path and then returns loaded text with the resolved filename. The environment checks managers from the most recently added to the oldest, so a later manager can override an earlier one.
Plugin-provided managers join the session for that compilation. They are checked before the base managers because the environment appends them and searches in reverse order. This lets a plugin handle a special path without changing the shared import flow. See Plugin architecture for the wider extension model.
| Runtime or source | Resource access |
|---|---|
| Node file manager | Reads local files and searches the current directory, configured paths, and the local module path. It supports both async and sync reads. |
| Node URL file manager | Reads HTTP or HTTPS resources when the import path or current directory is a URL. |
| Browser file manager | Reads resources with XHR and can reuse a per-session file cache. Browser paths are made absolute for URL output. |
| Plugin file manager | Handles paths selected by its supports contract and can take precedence over built-in managers. |
The import manager selects the manager before loading. The syncImport option then chooses the synchronous or asynchronous load method supported by that manager. If no manager supports a path, the import fails unless it is optional.
References: packages/…/environment.js• L8-57 packages/…/file-manager-api.ts• L3-76 packages/…/import-manager.js• L65-79 packages/…/index.js• L15-23 packages/…/file-manager.js• L8-18 packages/…/url-file-manager.js• L10-23 packages/…/file-manager.js• L8-19
Path context
Each source has a file context that gives imports a stable identity and gives URLs a base. The entry file establishes the root filename and entry path. Each loaded import then receives its resolved filename and its own current directory, while the root filename and entry path remain tied to the compilation.
Relative import lookup and URL output use related but different rules:
currentDirectorymakes an import relative to the file that declares it.pathsadds configured search locations when a relative resource is not found at its first location.filenameidentifies the resolved source for diagnostics, caches, recursion checks, and source maps.rootpathprefixes emitted URLs and CSS imports. It does not change a Less import that the compiler evaluates.rewriteUrlsadjusts relative URLs from imported files so they resolve from the entry output location.
The path helpers recognize protocol, absolute, root-relative, and local-relative forms. They normalize . and .. segments. In Node, module-style names can also resolve through the local module system. In the browser, the current document URL supplies the base for relative resources.
References: packages/…/parse.js• L36-57 packages/…/import-manager.js• L65-103 packages/…/contexts.js• L108-163 packages/…/abstract-file-manager.js• L1-71 packages/…/file-manager.js• L18-35 packages/…/file-manager.js• L67-83
Imported content in compilation
Loaded Less content is parsed into a source-aware tree and attached to its import node. During evaluation, the compiler copies the imported rules into the current ruleset, so variables, mixins, functions, and selectors can take part in the same Less scope and output flow. Nested imports are handled as part of this tree walk.
The incorporation rules depend on the import mode:
| Imported result | Later compilation behavior |
|---|---|
| Less tree | Evaluated as part of the surrounding ruleset. |
| CSS import | Emitted as a CSS @import with its path and media features. |
| Inline text | Emitted as anonymous content, with media features applied when present. |
| Plugin | Evaluated for its side effects and function registrations rather than emitted as CSS. |
The import manager records loaded contents and resolved files. It may reuse a parsed tree for a single import mode, but inline imports and imports marked multiple must not share that tree. The import visitor also prevents ordinary duplicate imports and recursive imports from expanding the same resolved file indefinitely.
References: packages/…/import-manager.js• L45-61 packages/…/import-manager.js• L104-137 packages/…/import-visitor.js• L100-150 packages/…/import.js• L178-295 packages/…/ruleset.js• L290-312
Runtime handoff
The shared import flow returns CSS or a structured error to the caller. Node callers receive the render result through the normal API or CLI path. Browser callers first load a stylesheet resource, build its root file context, and then render the loaded source before placing the resulting CSS in the document. See Node API and lessc and Browser runtime for those host flows.
The browser can serve cached source or CSS during refresh. A reload or explicit cache clear causes the file manager to fetch source again. This cache changes resource access only; it does not change how the shared compiler sequences or incorporates imports.
References: packages/…/render.js• L24-37 packages/…/index.js• L84-143 packages/…/index.js• L218-279 packages/…/file-manager.js• L63-105
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the file manager search order aligned with plugin precedence. A new manager can silently capture paths that built-in managers handled before.
- Keep resolved filenames stable across Node and browser managers. They feed duplicate checks, recursive import checks, diagnostics, caches, and source maps.
- Keep the import sequencer aligned with parser completion. A callback or variable import that bypasses the sequence can make output depend on load timing.
- Keep import option translations aligned with
Importevaluation. Changes tocss,once,inline,reference, oroptionalcan alter both loading and emitted CSS. - Keep
rootpathandrewriteUrlsaligned with file-manager path helpers. A mismatch can leave imported asset URLs valid in one runtime but broken in another. - Keep cached and uncached imports equivalent. Cache behavior must not change whether a resource is parsed as Less, kept as CSS, or incorporated more than once.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| No file manager supports an import path | The compile stops with a file error unless the import is optional | Register a manager whose support rule covers the path, or use optional only when omission is valid |
| A later import runs before an earlier import is ready | Variable and mixin visibility can depend on network or disk timing | Route every import completion through the sequencer |
| A variable import runs before its defining import completes | The path can resolve to the wrong file or fail | Keep variable imports behind the current import queue |
| The same resource is resolved to different filenames | Duplicate suppression, recursion checks, caches, and source maps become inconsistent | Return a stable resolved filename from each file manager |
rewriteUrls uses the wrong entry or current directory | Assets referenced by imported files resolve from the wrong location | Preserve the entry path and update current directory for every loaded file |
| A non-optional import cannot be read or parsed | No CSS result is returned | Check resource availability and preserve the import source in the error |
| An inline import is reused as a parsed tree | Inline content can leak state or produce the wrong output on a later use | Do not cache inline imports |
Key files
| File | Why to read it |
|---|---|
| packages/…/parser.js | Defines import syntax and starts import processing after the root tree is built. |
| packages/…/import-visitor.js | Shows import ordering, duplicate handling, recursion control, and nested tree visits. |
| packages/…/import-sequencer.js | Defines the ordering contract for async and variable imports. |
| packages/…/import-manager.js | Connects import nodes to file managers and tracks loaded source. |
| packages/…/import.js | Defines how CSS, Less, inline, reference, and plugin results enter evaluation. |
| packages/…/environment.js | Defines file-manager selection and precedence. |
| packages/…/file-manager-api.ts | States the contract that custom file managers must provide. |
| packages/…/abstract-file-manager.js | Defines shared path and extension behavior. |
| packages/…/file-manager.js | Shows local file and module lookup in Node. |
| packages/…/file-manager.js | Shows browser URL loading, absolute path behavior, and source caching. |
| packages/…/ruleset.js | Shows how resolved import results replace import nodes during evaluation. |