|
| 1 | +import type {StorybookConfig} from '@storybook/vue3-vite'; |
| 2 | +import {dirname, join} from 'path'; |
| 3 | +import vue from '@vitejs/plugin-vue'; |
| 4 | +import tailwindcss from '@tailwindcss/vite'; |
| 5 | + |
| 6 | +/** |
| 7 | + * This function is used to resolve the absolute path of a package. |
| 8 | + * It is needed in projects that use Yarn PnP or are set up within a monorepo. |
| 9 | + */ |
| 10 | +function getAbsolutePath(value: string): string { |
| 11 | + return dirname(require.resolve(join(value, 'package.json'))); |
| 12 | +} |
| 13 | + |
| 14 | +const config: StorybookConfig = { |
| 15 | + stories: ['../resources/js/**/*.mdx', '../resources/js/**/*.stories.@(js|jsx|mjs|ts|tsx)'], |
| 16 | + addons: [ |
| 17 | + getAbsolutePath('@storybook/addon-themes'), |
| 18 | + getAbsolutePath('@storybook/addon-docs'), |
| 19 | + getAbsolutePath('@storybook/addon-a11y'), |
| 20 | + ], |
| 21 | + framework: { |
| 22 | + name: getAbsolutePath('@storybook/vue3-vite') as '@storybook/vue3-vite', |
| 23 | + options: { |
| 24 | + docgen: 'vue-component-meta', |
| 25 | + }, |
| 26 | + }, |
| 27 | + viteFinal(config) { |
| 28 | + // Storybook's vue3-vite framework adds its own Vue plugin with default options. |
| 29 | + // We need to configure `isCustomElement` so Vue treats `craft-*` tags as web |
| 30 | + // components (from @craftcms/cp) rather than trying to resolve them as Vue |
| 31 | + // components. Since Vite's mergeConfig doesn't deep-merge plugin options, |
| 32 | + // we remove Storybook's Vue plugin and add our own with the correct config. |
| 33 | + const filteredPlugins = (config.plugins || []).flat().filter((plugin) => { |
| 34 | + if (plugin && typeof plugin === 'object' && 'name' in plugin) { |
| 35 | + return plugin.name !== 'vite:vue'; |
| 36 | + } |
| 37 | + return true; |
| 38 | + }); |
| 39 | + |
| 40 | + return { |
| 41 | + ...config, |
| 42 | + plugins: [ |
| 43 | + ...filteredPlugins, |
| 44 | + tailwindcss(), |
| 45 | + vue({ |
| 46 | + template: { |
| 47 | + compilerOptions: { |
| 48 | + isCustomElement: (tag) => tag.startsWith('craft-'), |
| 49 | + }, |
| 50 | + }, |
| 51 | + }), |
| 52 | + ], |
| 53 | + resolve: { |
| 54 | + ...config.resolve, |
| 55 | + alias: { |
| 56 | + ...(config.resolve?.alias || {}), |
| 57 | + '@': join(__dirname, '../resources/js'), |
| 58 | + vue: 'vue/dist/vue.esm-bundler.js', |
| 59 | + // Mock Inertia for Storybook |
| 60 | + '@inertiajs/vue3': join(__dirname, 'inertia-mock.ts'), |
| 61 | + }, |
| 62 | + }, |
| 63 | + }; |
| 64 | + }, |
| 65 | +}; |
| 66 | + |
| 67 | +export default config; |
0 commit comments