UNPKG

@histoire/plugin-svelte

Version:

Histoire plugin for Svelte 4/5 and SvelteKit 2/3 support

112 lines (111 loc) • 3.95 kB
import { registerStoryExecutionTargetResolver, withStoryExecution } from '@histoire/shared'; import * as svelte from 'svelte'; /** Svelte descendants inherit their actual root target, including later conditional children. */ const actorContext = '__HST_STORY_MOUNT_TARGET__'; registerStoryExecutionTargetResolver('svelte', () => svelte.getContext(actorContext)); const setupHookNames = [ 'setupSvelte3', 'setupSvelte4', 'setupSvelte5', ]; function loadSvelteModule(moduleId) { return import(/* @vite-ignore */ moduleId); } export async function mountSvelteComponent(component, options, mode = 'auto') { options = { ...options, context: new Map(options.context ?? []) }; options.context.set(actorContext, options.target); // Each branch below runs the story's setup code synchronously, so wrapping it // attributes the `onTest(...)` calls it emits to THIS mount — several mounts of // the same story coexist in one page and share the ambient test registry. if (mode !== 'server-compat') { if (typeof svelte?.mount === 'function') { const app = withStoryExecution(() => svelte.mount(component, options), options.target); return { app, destroy: () => { if (typeof svelte.unmount === 'function') { ; svelte.unmount(app); } else { app?.$destroy?.(); } }, }; } } try { // eslint-disable-next-line new-cap const app = withStoryExecution(() => new component(options), options.target); return { app, destroy: () => { app?.$destroy?.(); }, }; } catch (error) { const legacyModuleId = ['svelte', 'legacy'].join('/'); const legacy = await loadSvelteModule(legacyModuleId).catch(() => null); if (typeof legacy?.createClassComponent === 'function') { const app = withStoryExecution(() => legacy.createClassComponent({ component, ...options, }), options.target); return { app, destroy: () => { app?.$destroy?.(); }, }; } throw error; } } export function getLegacyStateApi(app) { if (typeof app?.$capture_state !== 'function' || typeof app?.$inject_state !== 'function') { return null; } return { captureState: () => app.$capture_state(), injectState: (state) => { app.$inject_state(state); }, }; } export async function callSetupFunctions(generatedSetup, setup, setupApi, variantSetupApp, target) { for (const hookName of setupHookNames) { const generatedHook = generatedSetup[hookName]; if (typeof generatedHook === 'function') { await withStoryExecution(() => generatedHook(setupApi), target); } const setupHook = setup[hookName]; if (typeof setupHook === 'function') { await withStoryExecution(() => setupHook(setupApi), target); } } if (typeof variantSetupApp === 'function') { await withStoryExecution(() => variantSetupApp(setupApi), target); } } export function createWrappedComponent(Wrap, controlComponent) { function ProxyWrap(anchorOrOptions, props) { if (new.target) { return new Wrap({ ...anchorOrOptions, props: { ...anchorOrOptions?.props, controlComponent, }, }); } return Wrap(anchorOrOptions, { ...props, controlComponent, }); } if (Wrap?.element) { ProxyWrap.element = Wrap.element; } return ProxyWrap; }