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

79.68% Statements 102/128
71.42% Branches 5/7
42.85% Functions 3/7
79.68% Lines 102/128

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 1281x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x     2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x                     1x 1x 1x 1x 1x 1x 1x 1x                     1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x
import { createCompiler } from './compiler.js';
import { handleAvenxHotUpdate } from './hmr.js';
import { loadStyle } from './css.js';
import pkg from '../package.json' with { type: 'json' };
 
import {
  isComponentFile,
  isPageFile,
  isComponentStyle,
  isPageStyle,
} from './utils.js';
 
const version = pkg.version;
 
/**
 * Creates the Avenx Vite plugin.
 * @param {object} options - Plugin configuration.
 * @param {boolean} [options.debug] - Enables debug logging.
 * @returns {import('vite').Plugin}
 */
export function avenxPlugin(options = {}) {
  const compiler = createCompiler(options);
  const debug = options.debug ?? false;
 
  /**
   * Logs debug information when debug mode is enabled.
   * @param {...any} args - Values to log.
   * @returns {void}
   */
  function log(...args) {
    if (debug) {
      console.log('[avenx-vite]', ...args);
    }
  }
 
  return {
    name: 'avenx-vite',
 
    enforce: 'pre',
 
    /**
     * Logs the plugin version when the Vite configuration has been resolved.
     * @returns {void}
     */
    configResolved() {
      console.log(`[avenx-vite] v${version} initialized`);
    },
 
    /**
     * Resolves Avenx source files.
     * @param {string} source
     * @returns {null}
     */
    resolveId(source) {
      if (
        isComponentFile(source) ||
        isPageFile(source) ||
        isComponentStyle(source) ||
        isPageStyle(source)
      ) {
        log('Resolve:', source);
      }

      return null;
    },
 
    /**
     * Loads Avenx source files.
     * @param {string} id
     * @returns {string|null}
     */
    load(id) {
      if (isComponentStyle(id) || isPageStyle(id)) {
        log('Load Style:', id);
        return loadStyle(id, compiler);
      }

      if (isComponentFile(id) || isPageFile(id)) {
        log('Load:', id);
      }

      return null;
    },
 
    /**
     * Compiles Avenx components and pages.
     * @param {string} code
     * @param {string} id
     * @returns {{code: string, map: null}|null}
     */
    transform(code, id) {
      if (isComponentFile(id)) {
        log('Compile Component:', id);
 
        const result = compiler.compileComponent(id, code);
 
        return {
          code: result.code,
          map: result.map,
        };
      }
 
      if (isPageFile(id)) {
        log('Compile Page:', id);
 
        const result = compiler.compilePage(id, code);
 
        return {
          code: result.code,
          map: result.map,
        };
      }

      return null;
    },
 
    /**
     * Handles Hot Module Replacement.
     * @param {import('vite').HmrContext} ctx
     * @returns {Array<never>|undefined}
     */
    handleHotUpdate(ctx) {
      return handleAvenxHotUpdate(ctx);
    },
  };
}
 
export { avenxPlugin as avenxVite, avenxPlugin as default };