Ian VanSchooten d46a5c4dac Cleanup readPackageJson functions
It's only used in the react framework, and doesn't need to be exported
2022-09-21 23:15:34 -04:00

51 lines
1.5 KiB
TypeScript

/* eslint-disable global-require */
import path from 'path';
import fs from 'fs';
import type { StorybookConfig, TypescriptOptions } from '@storybook/builder-vite';
export const addons: StorybookConfig['addons'] = ['@storybook/react'];
export const core: StorybookConfig['core'] = {
builder: '@storybook/builder-vite',
};
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;
const { reactDocgen, reactDocgenTypescriptOptions } = await presets.apply<any>(
'typescript',
{} as TypescriptOptions
);
let typescriptPresent;
try {
const pkgJson = readPackageJson();
typescriptPresent =
pkgJson && (pkgJson.devDependencies?.typescript || pkgJson.dependencies?.typescript);
} catch (e) {
typescriptPresent = false;
}
if (reactDocgen === 'react-docgen-typescript' && typescriptPresent) {
plugins.push(
require('@joshwooding/vite-plugin-react-docgen-typescript')(reactDocgenTypescriptOptions)
);
} else if (reactDocgen) {
const { reactDocgen: docgenPlugin } = await import('./plugins/react-docgen');
// Needs to run before the react plugin, so add to the front
plugins.unshift(docgenPlugin());
}
return config;
};