UNPKG

histoire

Version:

A fast story book powered by Vite

63 lines (62 loc) • 3.18 kB
import { resolve } from 'pathe'; import { resolveConfig as resolveViteConfig } from 'vite'; import { createBrowserEntryResolver, resolveCommandClientSetupFiles } from './config/browser-entries.js'; import { processConfig, resolveConfig } from './config/index.js'; import { resolvePluginMetadata } from './config/plugin-metadata.js'; import { createPluginInventory } from './config/plugin-toggle-inventory.js'; import { resolveWorkbenchPlugins } from './config/workbench-plugins.js'; import { closeContext, getContextRegistry } from './runtime/registry.js'; import { mergeHistoireViteConfig } from './vite/index.js'; export { closeContext } from './runtime/registry.js'; /** Resolves a private project capture without changing process cwd or environment. */ export async function createContext(options) { const root = resolve(options.root ?? process.cwd()); // Scope exists before configuration: defaultConfig may acquire Nuxt resources. const ctx = { root, configFile: options.configFile ? resolve(root, options.configFile) : undefined, config: undefined, // Filled in right below: merging the Histoire Vite config needs the context // itself (plugins receive it), so the field cannot be built before it. resolvedViteConfig: undefined, mode: options.mode, storyFiles: [], supportPlugins: [], markdownFiles: [], registeredCommands: [], workbenchPlugins: [], commandClientSetupFiles: new Map(), }; const { pluginContext } = getContextRegistry(ctx); try { ctx.config = await resolveConfig(root, options.mode, options.configFile, pluginContext, options.onConfigurationInputs); const command = options.mode === 'dev' ? 'serve' : 'build'; const viteConfig = await resolveViteConfig({ root }, command); ctx.supportPlugins = ctx.config.plugins.map(plugin => plugin.supportPlugin).filter(Boolean); ctx.resolvedViteConfig = await mergeHistoireViteConfig(viteConfig, ctx); await processConfig(ctx); for (const plugin of ctx.config.plugins) { await plugin.configResolved?.(ctx.config, pluginContext); if (plugin.commands?.length) ctx.registeredCommands.push(...plugin.commands); } ctx.pluginMetadata = await resolvePluginMetadata(root, ctx.config.plugins); const inventory = await createPluginInventory(ctx); ctx.pluginInventory = inventory.rows; ctx.pluginToggle = inventory.toggle; // Browser modules named by plugins resolve once here, so a bad entry fails startup. const resolveEntry = createBrowserEntryResolver(root, viteConfig); ctx.workbenchPlugins = await resolveWorkbenchPlugins(ctx.config.plugins, resolveEntry, options.mode); ctx.commandClientSetupFiles = await resolveCommandClientSetupFiles(ctx.registeredCommands, resolveEntry); return ctx; } catch (error) { try { await closeContext(ctx); } catch (cleanupError) { throw new AggregateError([error, cleanupError], String(error)); } throw error; } }