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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x | // src/astro/index.js
//
// Public entry point for @coherent.js/integrations/astro. Provides
// server-side rendering of Coherent.js components within Astro projects.
//
// Astro must be installed as a peer dependency to use this integration.
//
// Usage:
// import { createAstroIntegration } from '@coherent.js/integrations/astro';
import { render } from '@coherent.js/core';
/**
* Create an Astro integration for Coherent.js
*
* @param {Object} [options] - Integration options
* @param {boolean} [options.hydrate] - Enable client-side hydration
* @param {string} [options.hydrateScript] - Custom hydration script path
* @returns {Object} Astro integration configuration
*
* @example
* // astro.config.mjs
* import { createAstroIntegration } from '@coherent.js/integrations/astro';
* export default { integrations: [createAstroIntegration()] };
*/
export function createAstroIntegration(options = {}) {
return {
name: 'coherent',
hooks: {
'astro:config:setup': ({ addRenderer, updateConfig }) => {
addRenderer({
name: '@coherent.js/integrations/astro',
// Astro imports this module's default export as the SSR renderer.
serverEntrypoint: '@coherent.js/integrations/astro/server',
});
Iif (options.hydrate) {
updateConfig({
vite: {
optimizeDeps: {
include: ['@coherent.js/client']
}
}
});
}
},
'astro:build:done': ({ logger }) => {
logger?.info('Coherent.js components built successfully');
}
}
};
}
/**
* Render a Coherent.js component to HTML string
*
* @param {Function|Object} Component - Coherent.js component
* @param {Object} [props] - Component props
* @param {Object} [renderOptions] - Rendering options
* @returns {string} Rendered HTML
*/
export function renderComponent(Component, props = {}, renderOptions = {}) {
const componentDef = typeof Component === 'function'
? Component(props)
: Component;
return render(componentDef, renderOptions);
}
/**
* Create a server-side component renderer for Astro
*
* @param {Object} [options] - Renderer options
* @returns {Object} Astro renderer configuration
*/
export function createRenderer(options = {}) {
return {
name: '@coherent.js/astro-renderer',
check(Component) {
// Check if this looks like a Coherent.js component (function or plain object with tag keys)
return typeof Component === 'function' ||
(typeof Component === 'object' && Component !== null && !Array.isArray(Component));
},
renderToStaticMarkup(Component, props) {
const html = renderComponent(Component, props, options);
return { html };
}
};
}
|