1- import type { StorybookConfig } from '@storybook/react-webpack5' ;
2- import type { RuleSetRule } from 'webpack' ;
1+ import type { StorybookConfig } from '@storybook/react-vite' ;
2+ import type { RollupOptions } from 'rollup' ;
3+ import { mergeConfig } from 'vite' ;
4+ import { nodePolyfills } from 'vite-plugin-node-polyfills' ;
5+ import { viteStaticCopy } from 'vite-plugin-static-copy' ;
6+ import svgr from 'vite-plugin-svgr' ;
37
48const config : StorybookConfig = {
59 stories : [ '../docs/**/*.@(md|mdx)' , '../src/**/*.stories.@(js|jsx|ts|tsx)' , '../src/**/*.@(md|mdx)' ] ,
610
7- addons : [
8- '@storybook/addon-links' ,
9- '@storybook/addon-essentials' ,
10- {
11- name : '@storybook/addon-styling-webpack' ,
12- options : {
13- rules : [
14- // Replaces existing CSS rules to support PostCSS
15- {
16- test : / \. c s s $ / ,
17- use : [
18- 'style-loader' ,
19- {
20- loader : 'css-loader' ,
21- options : { importLoaders : 1 } ,
22- } ,
23- {
24- // Gets options from `postcss.config.js`
25- loader : 'postcss-loader' ,
26- options : { implementation : require . resolve ( 'postcss' ) } ,
27- } ,
28- ] ,
29- } ,
30- ] ,
31- } ,
32- } ,
33- '@storybook/addon-webpack5-compiler-babel' ,
34- ] ,
35-
3611 framework : {
37- name : '@storybook/react-webpack5 ' ,
12+ name : '@storybook/react-vite ' ,
3813 options : { } ,
3914 } ,
4015
@@ -43,41 +18,32 @@ const config: StorybookConfig = {
4318 reactDocgen : 'react-docgen-typescript' ,
4419 } ,
4520
46- webpackFinal : ( webpackConfig ) => {
47- // Remove any svg loader already set and use @svgr /webpack to load svgs on Storybook
48- const svgWebpackRule = webpackConfig . module ?. rules ?. find ( ( rule ) => {
49- if ( rule != null && typeof rule !== 'string' && ( rule as RuleSetRule ) . test instanceof RegExp ) {
50- const testRegExp = ( rule as RuleSetRule ) . test as RegExp ;
51- return testRegExp . test ( '.svg' ) ;
52- }
53-
54- return undefined ;
55- } ) ;
56-
57- if ( typeof svgWebpackRule !== 'string' ) {
58- ( svgWebpackRule as RuleSetRule ) . exclude = / \. s v g $ / ;
59- }
60-
61- webpackConfig . module ?. rules ?. push ( {
62- test : / \. s v g $ / ,
63- use : [ '@svgr/webpack' ] ,
64- } ) ;
65-
66- // Retrieve and update css rule to support raw imports of CSS files using "?raw"
67- const cssRule = webpackConfig . module ?. rules ?. find ( ( rule ) => {
68- if ( rule != null && typeof rule !== 'string' && ( rule as RuleSetRule ) . test instanceof RegExp ) {
69- const testRegExp = ( rule as RuleSetRule ) . test as RegExp ;
70- return testRegExp . test ( '.css' ) ;
71- }
72-
73- return undefined ;
74- } ) ;
21+ addons : [ '@storybook/addon-docs' ] ,
22+
23+ viteFinal : ( viteConfig ) => {
24+ // Add polyfills for path, url and source-map-js node modules and plugin for importing svg files
25+ const plugins = [
26+ nodePolyfills ( { include : [ 'path' , 'url' ] } ) ,
27+ svgr ( { include : '**/*.svg' } ) ,
28+ viteStaticCopy ( { targets : [ { src : './src/theme/fonts/*.ttf' , dest : './fonts' } ] } ) ,
29+ ] ;
30+ const resolve = { alias : { 'source-map-js' : 'source-map' } } ;
31+
32+ const rollupOptions : RollupOptions = {
33+ // Externalize font assets (see https://github.com/storybookjs/storybook/pull/27110)
34+ external : [ / .* \. t t f / ] ,
35+ // Silence "use client" directive and "/* PURE */" comment warnings during Storybook build
36+ onwarn : ( warning , warn ) => {
37+ if ( [ 'MODULE_LEVEL_DIRECTIVE' , 'INVALID_ANNOTATION' ] . includes ( warning . code ?? '' ) ) {
38+ return ;
39+ }
40+ warn ( warning ) ;
41+ } ,
42+ } ;
7543
76- if ( cssRule ) {
77- ( cssRule as RuleSetRule ) . resourceQuery = { not : [ / r a w / ] } ;
78- }
44+ const finalConfigs = mergeConfig ( viteConfig , { plugins, resolve, build : { rollupOptions } } ) ;
7945
80- return webpackConfig ;
46+ return finalConfigs ;
8147 } ,
8248} ;
8349
0 commit comments