|
1 | | -import type { StorybookConfig } from '@storybook/react-webpack5' |
| 1 | +import type { StorybookConfig } from '@storybook/react-vite' |
2 | 2 | import path from 'path' |
| 3 | +import { mergeConfig } from 'vite' |
| 4 | + |
3 | 5 | const rootDir = process.cwd() |
| 6 | + |
4 | 7 | const config: StorybookConfig = { |
5 | 8 | stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'], |
6 | | - addons: ['@storybook/addon-webpack5-compiler-swc', '@storybook/addon-a11y', '@storybook/addon-docs'], |
| 9 | + addons: [ |
| 10 | + '@storybook/addon-links', // 推荐加上:支持故事间跳转 |
| 11 | + '@storybook/addon-a11y', // 保持 |
| 12 | + '@storybook/addon-docs', // 如果你用了 MDX3,可以去掉(essentials 已包含) |
| 13 | + ], |
7 | 14 | docs: {}, |
8 | | - framework: '@storybook/react-webpack5', |
9 | | - webpackFinal: async (config) => { |
10 | | - config.resolve = config.resolve || {} |
11 | | - config.resolve.alias = { |
12 | | - ...(config.resolve.alias || {}), |
13 | | - '@': path.resolve(rootDir, 'src'), |
14 | | - '@src/*': path.resolve(rootDir, 'src/*'), |
15 | | - '@stateless/*': path.resolve(rootDir, 'src/components/stateless/*'), |
16 | | - '@stateful/*': path.resolve(rootDir, 'src/components/stateful/*'), |
17 | | - '@hooks/*': path.resolve(rootDir, 'src/components/hooks/*'), |
18 | | - '@app-hooks/*': path.resolve(rootDir, 'src/app-hooks/*'), |
19 | | - '@container/*': path.resolve(rootDir, 'src/components/container/*'), |
20 | | - '@assets/*': path.resolve(rootDir, 'src/assets/*'), |
21 | | - '@pages/*': path.resolve(rootDir, 'src/pages/*'), |
22 | | - '@routers/*': path.resolve(rootDir, 'src/routers/*'), |
23 | | - '@utils/*': path.resolve(rootDir, 'src/utils/*'), |
24 | | - '@theme/*': path.resolve(rootDir, 'src/theme/*'), |
25 | | - } |
26 | | - // If building for a subpath (e.g. /pro-react-admin/storybook/) |
27 | | - // you can set the STORYBOOK_BASE_HREF env var when running the build. |
28 | | - // This will make webpack emit assets with the correct publicPath. |
29 | | - // Example: STORYBOOK_BASE_HREF=/pro-react-admin/storybook/ npm run build-storybook |
30 | | - // const baseHref = process.env.STORYBOOK_BASE_HREF; |
31 | | - // if (baseHref) { |
32 | | - // config.output = config.output || {}; |
33 | | - // // webpack expects a string, ensure it ends with a slash |
34 | | - // config.output.publicPath = baseHref.endsWith('/') ? baseHref : baseHref + '/'; |
35 | | - // } |
36 | | - |
37 | | - // add Less and Less Module support for project styles |
38 | | - config.module = config.module || { rules: [] } |
39 | | - |
40 | | - const excludeProjectStyleFromDefaultRules = (rules: any[]) => { |
41 | | - const visit = (rule: any) => { |
42 | | - if (!rule) return |
43 | | - if (Array.isArray(rule.oneOf)) rule.oneOf.forEach(visit) |
44 | | - if (Array.isArray(rule.rules)) rule.rules.forEach(visit) |
45 | | - |
46 | | - const test = rule.test |
47 | | - const testStr = typeof test?.toString === 'function' ? test.toString() : '' |
48 | | - |
49 | | - // Storybook may have implicit style loaders. Exclude project-handled patterns |
50 | | - // to prevent double-processing (which can break CSS Modules output). |
51 | | - if (testStr.includes('\\.css')) { |
52 | | - const moduleCss = /\.module\.css$/ |
53 | | - if (!rule.exclude) { |
54 | | - rule.exclude = moduleCss |
55 | | - } else if (Array.isArray(rule.exclude)) { |
56 | | - rule.exclude = [...rule.exclude, moduleCss] |
57 | | - } else { |
58 | | - rule.exclude = [rule.exclude, moduleCss] |
59 | | - } |
60 | | - } |
61 | | - |
62 | | - if (testStr.includes('\\.less')) { |
63 | | - const allLess = /\.less$/ |
64 | | - if (!rule.exclude) { |
65 | | - rule.exclude = allLess |
66 | | - } else if (Array.isArray(rule.exclude)) { |
67 | | - rule.exclude = [...rule.exclude, allLess] |
68 | | - } else { |
69 | | - rule.exclude = [rule.exclude, allLess] |
70 | | - } |
71 | | - } |
72 | | - } |
73 | | - |
74 | | - rules.forEach(visit) |
75 | | - } |
76 | | - |
77 | | - const cssModuleRule = { |
78 | | - test: /\.module\.css$/, |
79 | | - use: [ |
80 | | - 'style-loader', |
81 | | - { |
82 | | - loader: 'css-loader', |
83 | | - options: { |
84 | | - modules: { localIdentName: '[local]__[hash:base64:5]' }, |
85 | | - importLoaders: 1, |
86 | | - }, |
| 15 | + framework: { |
| 16 | + name: '@storybook/react-vite', |
| 17 | + options: {}, |
| 18 | + }, |
| 19 | + viteFinal: async (viteConfig) => |
| 20 | + mergeConfig(viteConfig, { |
| 21 | + resolve: { |
| 22 | + alias: { |
| 23 | + '@': path.resolve(rootDir, 'src'), |
| 24 | + '@src/*': path.resolve(rootDir, 'src/*'), |
| 25 | + '@stateless/*': path.resolve(rootDir, 'src/components/stateless/*'), |
| 26 | + '@stateful/*': path.resolve(rootDir, 'src/components/stateful/*'), |
| 27 | + '@hooks/*': path.resolve(rootDir, 'src/components/hooks/*'), |
| 28 | + '@app-hooks/*': path.resolve(rootDir, 'src/app-hooks/*'), |
| 29 | + '@container/*': path.resolve(rootDir, 'src/components/container/*'), |
| 30 | + '@assets/*': path.resolve(rootDir, 'src/assets/*'), |
| 31 | + '@pages/*': path.resolve(rootDir, 'src/pages/*'), |
| 32 | + '@routers/*': path.resolve(rootDir, 'src/routers/*'), |
| 33 | + '@utils/*': path.resolve(rootDir, 'src/utils/*'), |
| 34 | + '@theme/*': path.resolve(rootDir, 'src/theme/*'), |
87 | 35 | }, |
88 | | - 'postcss-loader', |
89 | | - ], |
90 | | - } |
91 | | - |
92 | | - const lessModuleRule = { |
93 | | - test: /\.module\.less$/, |
94 | | - use: [ |
95 | | - 'style-loader', |
96 | | - { |
97 | | - loader: 'css-loader', |
98 | | - options: { |
99 | | - modules: { localIdentName: '[local]__[hash:base64:5]' }, |
100 | | - importLoaders: 2, |
| 36 | + }, |
| 37 | + css: { |
| 38 | + preprocessorOptions: { |
| 39 | + less: { |
| 40 | + javascriptEnabled: true, |
101 | 41 | }, |
102 | 42 | }, |
103 | | - 'postcss-loader', |
104 | | - { |
105 | | - loader: 'less-loader', |
106 | | - options: { lessOptions: { javascriptEnabled: true } }, |
107 | | - }, |
108 | | - ], |
109 | | - } |
110 | | - |
111 | | - const lessRule = { |
112 | | - test: /\.less$/, |
113 | | - exclude: /\.module\.less$/, |
114 | | - use: [ |
115 | | - 'style-loader', |
116 | | - 'css-loader', |
117 | | - 'postcss-loader', |
118 | | - { |
119 | | - loader: 'less-loader', |
120 | | - options: { lessOptions: { javascriptEnabled: true } }, |
| 43 | + }, |
| 44 | + cacheDir: path.resolve(rootDir, '.vite-cache/storybook'), |
| 45 | + optimizeDeps: { |
| 46 | + esbuildOptions: { |
| 47 | + target: 'es2020', |
121 | 48 | }, |
122 | | - ], |
123 | | - } |
| 49 | + }, |
124 | 50 |
|
125 | | - // Prevent Storybook's default style rules from also processing project-handled patterns. |
126 | | - // IMPORTANT: only apply these excludes to the existing/default rules, not the custom rules |
127 | | - // we prepend below. |
128 | | - const baseRules = config.module.rules || [] |
129 | | - excludeProjectStyleFromDefaultRules(baseRules) |
130 | | - |
131 | | - // prepend to ensure project rules take precedence |
132 | | - config.module.rules = [cssModuleRule, lessModuleRule, lessRule, ...baseRules] |
133 | | - |
134 | | - return config |
135 | | - }, |
| 51 | + build: { |
| 52 | + chunkSizeWarningLimit: 2000, |
| 53 | + }, |
| 54 | + }), |
136 | 55 | } |
| 56 | + |
137 | 57 | export default config |
0 commit comments