@@ -9,8 +9,10 @@ import type { StorybookConfig } from "@storybook/react-vite";
99import fs from "node:fs" ;
1010import { nodePolyfills } from "vite-plugin-node-polyfills" ;
1111import { mergeConfig } from "vite" ;
12- import { dirname , join } from "node:path" ;
12+ import { basename , dirname , extname , join } from "node:path" ;
1313import { fileURLToPath } from "node:url" ;
14+ import { defineStorybookVis } from "storybook-addon-vis/node" ;
15+ import { trimCommonFolder } from "storybook-addon-vis/vitest-plugin" ;
1416
1517const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
1618
@@ -28,12 +30,27 @@ for (const lang of languageFiles) {
2830 }
2931}
3032
31- /**
32- * This function is used to resolve the absolute path of a package.
33- * It is needed in projects that use Yarn PnP or are set up within a monorepo.
34- */
35- function getAbsolutePath ( value : string ) : any {
36- return dirname ( fileURLToPath ( import . meta. resolve ( `${ value } /package.json` ) ) ) ;
33+ function resolveVisualSnapshotRootDir ( { ci, platform } : { ci : boolean ; platform : string } ) : string {
34+ return `__vis__/${ ci ? "linux" : platform } ` ;
35+ }
36+
37+ function slugifySnapshotSegment ( segment : string ) : string {
38+ return segment
39+ . trim ( )
40+ . toLowerCase ( )
41+ . replace ( / [ ^ a - z 0 - 9 ] + / g, "-" )
42+ . replace ( / ^ - + | - + $ / g, "" ) ;
43+ }
44+
45+ function resolveVisualSnapshotSubpath ( { subpath } : { subpath : string } ) : string {
46+ const normalizedSubpath = trimCommonFolder ( subpath . startsWith ( "./" ) ? subpath . slice ( 2 ) : subpath ) . replaceAll (
47+ "\\" ,
48+ "/" ,
49+ ) ;
50+ const topLevelDirectory = normalizedSubpath . split ( "/" , 1 ) [ 0 ] ?? "shared-components" ;
51+ const storyName = basename ( normalizedSubpath , extname ( normalizedSubpath ) ) . replace ( / \. s t o r i e s $ / , "" ) ;
52+
53+ return `${ slugifySnapshotSegment ( topLevelDirectory ) } /${ storyName } ` ;
3754}
3855
3956const config : StorybookConfig = {
@@ -43,7 +60,14 @@ const config: StorybookConfig = {
4360 "@storybook/addon-designs" ,
4461 "@storybook/addon-a11y" ,
4562 "@storybook/addon-vitest" ,
46- getAbsolutePath ( "storybook-addon-vis" ) ,
63+ defineStorybookVis ( {
64+ visProjects : [
65+ {
66+ snapshotRootDir : resolveVisualSnapshotRootDir ,
67+ snapshotSubpath : resolveVisualSnapshotSubpath ,
68+ } ,
69+ ] ,
70+ } ) ,
4771 ] ,
4872 framework : "@storybook/react-vite" ,
4973 core : {
0 commit comments