Skip to content

Support lazy CSS generation in Input - #2165

Open
Kenoia wants to merge 1 commit into
postcss:mainfrom
Kenoia:lazy-input-css
Open

Kenoia wants to merge 1 commit into
postcss:mainfrom
Kenoia:lazy-input-css

Conversation

@Kenoia

@Kenoia Kenoia commented Oct 7, 2026 •

Copy link
Copy Markdown

Closes #1945

Allow Input to receive a function that generates its CSS on demand:

const input = new Input(() => generateCSS(), { from: 'a.scss' })
input.from // Does not generate the source
input.css // Generates and caches it

The lazy path reuses the existing constructor for BOM handling, documents, and source maps. Successful initialization replaces the accessors with ordinary writable properties. If initialization throws, the input can be retried. String and Buffer inputs keep their existing eager behavior.

Without from, resolving the input identifier may generate the source because a previous source map can supply the filename. The constructor documentation describes this distinction.

Added tests for deferred generation, caching, BOMs, source maps, writable properties, failed initialization, JSON round-tripping, and error positions. The new tests fail against the original implementation.

pnpm test passes on Arch Linux with Node.js 26.10.0 and pnpm 11.28.4: 731 unit tests, 100% line coverage, lint, types, version, integration, and size checks. Other Node versions and Windows have not been run locally.

Summary by CodeRabbit

  • New Features
    • Input sources can now be provided as a function that generates CSS. Generation is deferred until the source or its source map is needed, then cached after successful initialization.
    • Inputs with a specified source path use that path immediately, without triggering deferred generation when reading the input’s path or ID.
    • Lazy-generated inputs support source maps, serialization, and writable input properties.

Accept a source callback without generating CSS when only an explicit from path is read. Reuse eager initialization for source maps, BOM handling, documents and error positions, then cache writable source properties.

Add regression coverage for deferred evaluation, source maps, serialization and failed initialization.
@coderabbitai

coderabbitai Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ccf988ae-5ae0-43f3-b39c-7f10ce5eb1fe
📥 Commits

Reviewing files that changed from the base of the PR and between 1fd8f88 and 71a0b61.

📒 Files selected for processing (3)
  • lib/input.d.ts
  • lib/input.js
  • test/input.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

Input now accepts a CSS source function and defers generating CSS until input data is needed. The generated input is cached after successful initialization. Tests cover property access, path and source-map handling, serialization, and error locations.

Changes

Lazy Input CSS

Layer / File(s) Summary
Lazy initialization and input paths
lib/input.d.ts, lib/input.js, test/input.test.ts
The constructor declaration accepts a string or source function. The implementation initializes function-valued CSS on access and centralizes path resolution. Tests check deferred generation, caching, path behavior, and generated IDs.
Lazy input properties and edge cases
test/input.test.ts
Tests cover BOM handling, source maps, writable properties, retries after generation errors, empty sources, and eager conversion of stringifiable objects.
Serialization and error locations
test/input.test.ts
Tests compare lazy-input serialization with string-input serialization and check error source text and position mapping, including previous source maps.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant Input
  participant Source as CSS source function
  Caller->>Input: Read CSS-dependent property
  Input->>Source: Invoke source function
  Source-->>Input: Return CSS string
  Input-->>Caller: Return initialized property
Loading

Merge Risk: ⚪ Minimal · up to 71a0b

The reviewed change has no identified issue that should delay merging after normal checks.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: adding lazy CSS generation to Input.
Linked Issues check ✅ Passed Issue #1945 requests lazy CSS generation so callers can read the input path without reconstructing CSS. lib/input.js accepts a generator and defers initialization; with opts.from, input.from doe…
Out of Scope Changes check ✅ Passed The changes support issue #1945. The resolveFile refactor preserves path resolution while supporting the lazy constructor, and the declaration, documentation, and tests cover the new behavior. No un…
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Kenoia
Kenoia marked this pull request as ready for review October 7, 2026 11:26
Copilot AI balanced review requested due to automatic review settings October 7, 2026 11:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@ai ai added this to the 9.0 milestone Oct 7, 2026
@ai

ai commented Oct 7, 2026

Copy link
Copy Markdown
Member

It looks like a feature for 9.0 release

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lazy Input.css

3 participants