Add svelte vite plugin if not found

This commit is contained in:
Ian VanSchooten 2022-09-26 09:13:01 -04:00
parent b81a5b1261
commit 1b16274e36
2 changed files with 24 additions and 12 deletions

View File

@ -1,6 +1,5 @@
import path from 'path';
import fs from 'fs';
import type { StorybookConfig } from '@storybook/builder-vite';
import { hasPlugin } from './utils';
import { svelteDocgen } from './plugins/svelte-docgen';
export const addons: StorybookConfig['addons'] = ['@storybook/svelte'];
@ -9,19 +8,16 @@ export const core: StorybookConfig['core'] = {
builder: '@storybook/builder-vite',
};
export function readPackageJson(): Record<string, any> | false {
const packageJsonPath = path.resolve('package.json');
if (!fs.existsSync(packageJsonPath)) {
return false;
}
const jsonContent = fs.readFileSync(packageJsonPath, 'utf8');
return JSON.parse(jsonContent);
}
export const viteFinal: StorybookConfig['viteFinal'] = async (config, { presets }) => {
const { plugins = [] } = config;
// Add svelte plugin if not present
if (!hasPlugin(plugins, 'vite-plugin-svelte')) {
const { svelte } = await import('@sveltejs/vite-plugin-svelte');
plugins.push(svelte());
}
// Add docgen plugin
plugins.push(svelteDocgen(config));
return {

View File

@ -0,0 +1,16 @@
import { PluginOption } from 'vite';
function checkName(plugin: PluginOption, name: string) {
return typeof plugin === 'object' && 'name' in plugin && plugin.name === name;
}
export function hasPlugin(plugins: PluginOption[], name: string) {
return Boolean(
plugins.find((p): boolean => {
if (Array.isArray(p)) {
return Boolean(hasPlugin(p, name));
}
return checkName(p, name);
})
);
}