UNPKG

histoire

Version:

A fast story book powered by Vite

106 lines (105 loc) • 5.23 kB
import { loadConfigFromFile as loadViteConfigFromFile, mergeConfig as mergeViteConfig, } from 'vite'; import { createMarkdownPlugins } from '../markdown.js'; import { collectionEligibilityTransformPlugin } from '../test/eligibility/transforms.js'; import { hasProjectVitest } from '../util/has-vitest.js'; import { createVirtualFilesPlugin } from '../virtual/vite-plugin.js'; import { createCollectionOptimizerPolicy } from './collection-optimizer.js'; import { createHistoireVitePlugin } from './core-plugin.js'; import { createDevCommandsPlugin, createFileNamePlugin, createFlagsPlugin, createHistoireDevPlugin, } from './extra-plugins.js'; import { mergeHistoireViteConfig } from './merge-config.js'; import { createMockerPlugins } from './mocker.js'; import { createModuleVersionPlugin } from './module-version.js'; import { resolveBrowserRuntimePaths } from './resolve-paths.js'; import { createStoryVitestShimPlugin } from './story-vitest-shim.js'; import { createBrowserCollectVitestStubPlugin, createNodeCollectVitestStubPlugin } from './vitest-collect-stub.js'; /** * Builds the full Vite config Histoire runs on, for every mode: app dev server, * production build, node-side collection and Vitest browser runtimes. * @param isServer Whether the config drives the node-side (collecting) server. * @param ctx The histoire context. * @param options Config options (see {@link GetViteConfigWithPluginsOptions}). */ export async function getViteConfigWithPlugins(isServer, ctx, options = {}) { const userViteConfigFile = await loadViteConfigFromFile({ command: ctx.mode === 'dev' ? 'serve' : 'build', mode: ctx.mode }, undefined, ctx.root); const userViteConfig = mergeViteConfig(userViteConfigFile?.config ?? {}, { root: ctx.root, server: { port: 6006 } }); let inlineConfig = await mergeHistoireViteConfig(userViteConfig, ctx); for (const plugin of ctx.config.plugins) { const override = await plugin.viteConfig?.(inlineConfig, { mode: ctx.mode, isServer, browserRuntime: options.browserRuntime === true, collecting: options.collecting === true, }); if (override) inlineConfig = mergeViteConfig(inlineConfig, override); } const plugins = []; const projectHasVitest = hasProjectVitest(ctx.root); const browserRuntimePaths = resolveBrowserRuntimePaths(ctx, projectHasVitest); plugins.push(createHistoireVitePlugin(ctx, { isServer, browserRuntime: options.browserRuntime, browserRuntimePaths, })); plugins.push(createVirtualFilesPlugin(ctx, isServer)); // Sandbox documents share Histoire's built modules: serve them cacheable. if (ctx.mode === 'dev' && !isServer && !options.browserRuntime) { plugins.push(createModuleVersionPlugin()); } if (!isServer && options.browserRuntime && options.collecting) { plugins.push(createBrowserCollectVitestStubPlugin()); } if (!isServer && !options.collecting && projectHasVitest && browserRuntimePaths.vitestMockerBrowser && (browserRuntimePaths.vitestHasBundledExpect || (browserRuntimePaths.vitestExpect && browserRuntimePaths.vitestSpy))) { plugins.push(createStoryVitestShimPlugin(ctx, browserRuntimePaths)); } if (isServer && !options.browserRuntime) { plugins.push(createNodeCollectVitestStubPlugin()); } // Skip mocker plugin in browserRuntime mode (collection + test phases). // The mocker rewrites dynamic imports to use __vitest_mocker__.wrapDynamicImport() // but __vitest_mocker__ isn't initialized by vitest in histoire's browser context. // Histoire's own test-harness already wraps dynamic imports safely via // runWithVitestDynamicImport(). if (!isServer && projectHasVitest && !options.browserRuntime) { plugins.push(...await createMockerPlugins(ctx)); } // Replace dev flag plugins.push(createFlagsPlugin(ctx)); if (ctx.mode === 'dev') { // Dev commands plugins.push(createDevCommandsPlugin(ctx)); } // Markdown plugins.push(...await createMarkdownPlugins(ctx)); if (ctx.mode === 'build') { // Add file name in build mode to have components names instead of <Anonymous> plugins.push(createFileNamePlugin(ctx)); } if (process.env.HISTOIRE_DEV && !isServer) { plugins.push(createHistoireDevPlugin()); } for (const plugin of ctx.config.plugins) { if (plugin.vitePlugins) { await plugin.vitePlugins(plugins); } } if (!isServer && !options.browserRuntime) plugins.push(collectionEligibilityTransformPlugin(ctx)); // Append after framework hooks: middleware-mode collection must not start a // second optimized framework runtime beside vite-node's native imports. const collectionOptimizer = createCollectionOptimizerPolicy(isServer && !options.browserRuntime); if (collectionOptimizer) plugins.push(collectionOptimizer); const viteConfig = mergeViteConfig(inlineConfig, { configFile: false, plugins, }); return { viteConfig, viteConfigFile: userViteConfigFile?.path ?? null, }; }