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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7x 7x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 293x 293x 293x 293x 291x 293x 2x 2x 293x 293x 2x 2x 2x 2x 2x 2x 2x 2x 89x 1x 1x 1x 88x 89x 1x 1x 88x 88x 2x 2x 2x 2x 2x 2x 2x 2x 2x 91x 2x 2x 2x 2x 2x 2x 2x 2x 2x 89x 90x 1x 1x 1x 1x 1x 1x 1x 1x 1x 88x 88x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | 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.
* @param {{strict?: boolean}} [options.csp] - Content-Security-Policy posture.
* With `strict: true` a module whose expressions did not all compile to
* functions fails the transform, instead of being left to the runtime
* interpreter -- which is what would need `'unsafe-eval'`. Equivalent to
* `csp.strict` in avenx.config.json, and read from `options.config` too.
* @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 and configures production posture when the Vite configuration has been resolved.
* @param {object} [config] - Resolved Vite configuration.
* @returns {void}
*/
configResolved(config) {
if (config) {
if (config.isProduction !== undefined) {
compiler.setProduction(config.isProduction);
} else if (config.mode === 'production') {
compiler.setProduction(true);
}
}
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 }; |