css_cache.ts

Cache infrastructure for incremental CSS class extraction.

Provides per-file caching with content hash validation to avoid re-extracting classes from unchanged files.

view source

Declarations
#

13 declarations

CachedExtraction
#

css_cache.ts view source

CachedExtraction import type {CachedExtraction} from '@fuzdev/fuz_css/css_cache.js';

Cached extraction result for a single file. Uses null instead of empty arrays to avoid allocation overhead.

v

Cache version - invalidates cache when bumped

type number

content_hash

Content hash of the source file (BLAKE3 via hash_blake3)

type string

extraction_key

The configuration part of the key (to_extraction_cache_key), or null for the default

type string | null

classes

Classes as [name, locations] tuples, or null if none

type [string, SourceLocation[]][] | null

explicit_classes

Classes from @fuz-classes comments, or null if none

type string[] | null

diagnostics

Extraction diagnostics, or null if none

type ExtractionDiagnostic[] | null

elements

HTML elements found in the file, or null if none

type string[] | null

explicit_elements

Elements from @fuz-elements comments, or null if none

type string[] | null

explicit_variables

Variables from @fuz-variables comments, or null if none

type string[] | null

create_cache_path_resolver
#

css_cache.ts view source

internal

Shared by the Vite plugin and the Gro generator.

(cache_dir: string, project_root: string): (file_id: string) => string | null import {create_cache_path_resolver} from '@fuzdev/fuz_css/css_cache.js';

Creates the cache-path lookup the generators share: a file's cache path under cache_dir in project_root, or null on CI, where nothing is cached.

cache_dir

the cache directory, relative to project_root

type string

project_root

the project root, with or without a trailing slash

type string

returns

(file_id: string) => string | null

a function from a file's absolute path to its cache path, or null when uncached

CSS_CACHE_VERSION
#

css_cache.ts view source

7 import {CSS_CACHE_VERSION} from '@fuzdev/fuz_css/css_cache.js';

CSS cache version. Bump when any of these change:

v1: Initial version with classes and diagnostics v2: Use null instead of empty arrays, add explicit_classes, elements, css_variables. v3: Add explicit_elements, explicit_variables for @fuz-elements/@fuz-variables comments. v4: Filter incomplete CSS variables in dynamic templates (e.g., var(--prefix_{expr})). v5: Remove css_variables and explicit_variables (now detected via simple regex scan). v6: Re-add explicit_variables for @fuz-variables comments (regex scan misses dynamic templates). v7: Add extraction_key, so a change to acorn_plugins or cache_salt misses.

DEFAULT_CACHE_DIR
#

css_cache.ts view source

".fuz/cache/css" import {DEFAULT_CACHE_DIR} from '@fuzdev/fuz_css/css_cache.js';

Default cache directory relative to project root.

delete_cached_extraction
#

css_cache.ts view source

(deps: CacheDeps, cache_path: string): Promise<void> import {delete_cached_extraction} from '@fuzdev/fuz_css/css_cache.js';

Deletes a cached extraction file. Best-effort: every unlink error is swallowed, not just not_found - the cache is disposable and callers delete fire-and-forget.

deps

filesystem deps for dependency injection

cache_path

absolute path to the cache file

type string

returns

Promise<void>

from_cached_extraction
#

css_cache.ts view source

(cached: CachedExtraction): ExtractionData import {from_cached_extraction} from '@fuzdev/fuz_css/css_cache.js';

Converts a cached extraction back to the runtime format. Preserves null semantics (null = empty).

cached

cached extraction data

returns

ExtractionData

get_cache_path
#

css_cache.ts view source

internal

The inside-the-root case of `get_file_cache_path`.

(source_path: string, cache_dir: string, project_root: string): string import {get_cache_path} from '@fuzdev/fuz_css/css_cache.js';

Computes the cache file path for a source file. Cache structure mirrors source tree: src/lib/Foo.svelte → .fuz/cache/css/src/lib/Foo.svelte.json

source_path

absolute path to the source file

type string

cache_dir

absolute path to the cache directory

type string

project_root

normalized project root (must end with /)

type string

returns

string

get_file_cache_path
#

css_cache.ts view source

(file_id: string, cache_dir: string, project_root: string): string import {get_file_cache_path} from '@fuzdev/fuz_css/css_cache.js';

Computes cache path for a file, handling both internal and external paths. Internal files use relative paths mirroring source tree. External files (outside project root) use hashed absolute paths in _external/.

file_id

absolute path to the source file

type string

cache_dir

absolute path to the cache directory

type string

project_root

normalized project root (must end with /)

type string

returns

string

is_ci
#

css_cache.ts view source

internal

Read by the generators for their cache and `on_error` defaults.

boolean import {is_ci} from '@fuzdev/fuz_css/css_cache.js';

Whether this is a CI run (CI=1, CI=true, or another truthy value): there's no point writing a cache the next run won't reuse.

load_cached_extraction
#

css_cache.ts view source

(deps: CacheDeps, cache_path: string): Promise<CachedExtraction | null> import {load_cached_extraction} from '@fuzdev/fuz_css/css_cache.js';

Loads a cached extraction result from disk. Returns null if the cache is missing, corrupted, or has a version mismatch. This makes the cache self-healing: any error triggers re-extraction.

deps

filesystem deps for dependency injection

cache_path

absolute path to the cache file

type string

returns

Promise<CachedExtraction | null>

save_cached_extraction
#

css_cache.ts view source

(deps: CacheDeps, options: SaveCachedExtractionOptions): Promise<void> import {save_cached_extraction} from '@fuzdev/fuz_css/css_cache.js';

Saves an extraction result to the cache. Uses atomic write (temp file + rename) for crash safety. Normalizes empty collections to null to avoid allocation overhead on load.

deps

filesystem deps for dependency injection

options

the cache file, its key parts, and the extraction to write

returns

Promise<void>

SaveCachedExtractionOptions
#

css_cache.ts view source

SaveCachedExtractionOptions import type {SaveCachedExtractionOptions} from '@fuzdev/fuz_css/css_cache.js';

cache_path

Absolute path to the cache file.

type string

content_hash

Content hash of the source file contents.

type string

extraction_key

The configuration part of the key, from to_extraction_cache_key.

type string | null

extraction

Extraction data to cache.

type ExtractionData

to_extraction_cache_key
#

css_cache.ts view source

internal

Shared by the Vite plugin and the Gro generator.

(acorn_plugins: AcornPlugin[] | undefined, cache_salt: string | undefined): string | null import {to_extraction_cache_key} from '@fuzdev/fuz_css/css_cache.js';

Computes the part of the cache key that comes from configuration rather than file content: a fingerprint of the configured acorn plugins (their source text) and the consumer's cache_salt, or null when neither is set. A plugin's options live in its closure, where the source text can't see them, so a consumer who changes only a plugin's options bumps cache_salt.

acorn_plugins

the extraction's acorn plugins, if any

type (AcornPlugin)[] | undefined

cache_salt

a consumer string folded into the key, if any

type string | undefined

returns

string | null

the key, or null for the default configuration

Depends on
#

Imported by
#