All files / plugins/avenx-vite/src css.js

100% Statements 35/35
100% Branches 7/7
100% Functions 1/1
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 362x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 18x 1x 1x 17x 18x 17x 17x 16x 16x 17x 1x 1x 1x 1x 1x 1x 1x 1x 18x  
import fs from 'node:fs';
 
/**
 * Loads an Avenx stylesheet as the scoped CSS the runtime expects.
 *
 * Avenx stylesheets are not plain CSS: rules live in `<@css>` blocks keyed by
 * block name, and the compiler rewrites those names into hashed scope classes
 * that it also injects into the component's template. Returning the block
 * bodies verbatim produced selectors like `box { ... }`, which match nothing,
 * so component styles silently did not apply under Vite while the same source
 * worked under `avenx build`.
 * @param {string} filePath - Absolute path to the stylesheet.
 * @param {object} [compiler] - Compiler instance from createCompiler().
 * @returns {string|null} The scoped CSS, or null when the file does not exist.
 */
export function loadStyle(filePath, compiler) {
  if (!fs.existsSync(filePath)) {
    return null;
  }
 
  if (compiler && typeof compiler.compileStyle === 'function') {
    const scoped = compiler.compileStyle(filePath);
    if (typeof scoped === 'string') {
      return scoped;
    }
  }
 
  // No owning component or page could be resolved, so there is no scope to
  // apply. Fall back to the raw declarations with the block markers removed.
  return fs
    .readFileSync(filePath, 'utf8')
    .replace(/<@css>/g, '')
    .replace(/<\/@css>/g, '')
    .trim();
}