remove vite-plugin-svelte-kit when using SvelteKit setup

This commit is contained in:
Jeppe Reinhold 2022-10-18 14:25:16 +02:00
parent db0b308b2d
commit 4d4e62360a
3 changed files with 23 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import type { StorybookConfig } from '@storybook/builder-vite';
import { StorybookConfig, withoutVitePlugins } from '@storybook/builder-vite';
import { hasPlugin } from './utils';
import { svelteDocgen } from './plugins/svelte-docgen';
@ -9,7 +9,7 @@ export const core: StorybookConfig['core'] = {
};
export const viteFinal: StorybookConfig['viteFinal'] = async (config, options) => {
const { plugins = [] } = config;
let { plugins = [] } = config;
const { svelte, loadSvelteConfig } = await import('@sveltejs/vite-plugin-svelte');
const svelteOptions: Record<string, any> = await options.presets.apply(
'svelteOptions',
@ -26,6 +26,10 @@ export const viteFinal: StorybookConfig['viteFinal'] = async (config, options) =
// Add docgen plugin
plugins.push(svelteDocgen(svelteConfig));
// Remove vite-plugin-svelte-kit from plugins if using SvelteKit
// see https://github.com/storybookjs/storybook/issues/19280#issuecomment-1281204341
plugins = withoutVitePlugins(plugins, ['vite-plugin-svelte-kit']);
// TODO: temporary until/unless https://github.com/storybookjs/addon-svelte-csf/issues/64 is fixed
// Wrapping in try-catch in case `@storybook/addon-svelte-csf is not installed
try {

View File

@ -14,6 +14,7 @@ import { createViteServer } from './vite-server';
import { build as viteBuild } from './build';
import type { ExtendedOptions } from './types';
export { withoutVitePlugins } from './utils/without-vite-plugins';
export type { TypescriptOptions } from '@storybook/core-common';
// Storybook's Stats are optional Webpack related property

View File

@ -0,0 +1,16 @@
import { PluginOption } from 'vite';
// recursively remove all plugins with the given names
export const withoutVitePlugins = (
plugins: PluginOption[] = [],
namesToRemove: string[]
): PluginOption[] =>
plugins.map((plugin) => {
if (Array.isArray(plugin)) {
return withoutVitePlugins(plugin, namesToRemove);
}
if (plugin && 'name' in plugin && namesToRemove.includes(plugin.name)) {
return false;
}
return plugin;
});