@@ -6,6 +6,12 @@ import type { BuilderStats } from 'storybook/internal/types';
6
6
import slash from 'slash' ;
7
7
import type { Plugin } from 'vite' ;
8
8
9
+ import {
10
+ SB_VIRTUAL_FILES ,
11
+ getOriginalVirtualModuleId ,
12
+ getResolvedVirtualModuleId ,
13
+ } from '../virtual-file-names' ;
14
+
9
15
/*
10
16
* Reason, Module are copied from chromatic types
11
17
* https://github.com/chromaui/chromatic-cli/blob/145a5e295dde21042e96396c7e004f250d842182/bin-src/types.ts#L265-L276
@@ -34,11 +40,18 @@ function stripQueryParams(filePath: string): string {
34
40
35
41
/** We only care about user code, not node_modules, vite files, or (most) virtual files. */
36
42
function isUserCode ( moduleName : string ) {
43
+ if ( ! moduleName ) {
44
+ return false ;
45
+ }
46
+
47
+ // keep Storybook's virtual files because they import the story files, so they are essential to the module graph
48
+ if ( Object . values ( SB_VIRTUAL_FILES ) . includes ( getOriginalVirtualModuleId ( moduleName ) ) ) {
49
+ return true ;
50
+ }
51
+
37
52
return Boolean (
38
- moduleName &&
39
- ! moduleName . startsWith ( 'vite/' ) &&
40
- ! moduleName . startsWith ( '\x00' ) &&
41
- ! moduleName . startsWith ( '\u0000' ) &&
53
+ ! moduleName . startsWith ( 'vite/' ) &&
54
+ ! moduleName . startsWith ( '\0' ) &&
42
55
moduleName !== 'react/jsx-runtime' &&
43
56
! moduleName . match ( / n o d e _ m o d u l e s \/ / )
44
57
) ;
@@ -53,6 +66,14 @@ export function pluginWebpackStats({ workingDir }: WebpackStatsPluginOptions): W
53
66
if ( filename . startsWith ( '/virtual:' ) ) {
54
67
return filename ;
55
68
}
69
+ // ! Maintain backwards compatibility with the old virtual file names
70
+ // ! to ensure that the stats file doesn't change between the versions
71
+ // ! Turbosnap is also only compatible with the old virtual file names
72
+ // ! the old virtual file names did not start with the obligatory \0 character
73
+ if ( Object . values ( SB_VIRTUAL_FILES ) . includes ( getOriginalVirtualModuleId ( filename ) ) ) {
74
+ return getOriginalVirtualModuleId ( filename ) ;
75
+ }
76
+
56
77
// Otherwise, we need them in the format `./path/to/file.js`.
57
78
else {
58
79
const relativePath = relative ( workingDir , stripQueryParams ( filename ) ) ;
@@ -82,25 +103,27 @@ export function pluginWebpackStats({ workingDir }: WebpackStatsPluginOptions): W
82
103
// We want this to run after the vite build plugins (https://vitejs.dev/guide/api-plugin.html#plugin-ordering)
83
104
enforce : 'post' ,
84
105
moduleParsed : function ( mod ) {
85
- if ( isUserCode ( mod . id ) ) {
86
- mod . importedIds
87
- . concat ( mod . dynamicallyImportedIds )
88
- . filter ( ( name ) => isUserCode ( name ) )
89
- . forEach ( ( depIdUnsafe ) => {
90
- const depId = normalize ( depIdUnsafe ) ;
91
- if ( statsMap . has ( depId ) ) {
92
- const m = statsMap . get ( depId ) ;
93
- if ( m ) {
94
- m . reasons = ( m . reasons ?? [ ] )
95
- . concat ( createReasons ( [ mod . id ] ) )
96
- . filter ( ( r ) => r . moduleName !== depId ) ;
97
- statsMap . set ( depId , m ) ;
98
- }
99
- } else {
100
- statsMap . set ( depId , createStatsMapModule ( depId , [ mod . id ] ) ) ;
101
- }
102
- } ) ;
106
+ if ( ! isUserCode ( mod . id ) ) {
107
+ return ;
103
108
}
109
+ mod . importedIds
110
+ . concat ( mod . dynamicallyImportedIds )
111
+ . filter ( ( name ) => isUserCode ( name ) )
112
+ . forEach ( ( depIdUnsafe ) => {
113
+ const depId = normalize ( depIdUnsafe ) ;
114
+ if ( ! statsMap . has ( depId ) ) {
115
+ statsMap . set ( depId , createStatsMapModule ( depId , [ mod . id ] ) ) ;
116
+ return ;
117
+ }
118
+ const m = statsMap . get ( depId ) ;
119
+ if ( ! m ) {
120
+ return ;
121
+ }
122
+ m . reasons = ( m . reasons ?? [ ] )
123
+ . concat ( createReasons ( [ mod . id ] ) )
124
+ . filter ( ( r ) => r . moduleName !== depId ) ;
125
+ statsMap . set ( depId , m ) ;
126
+ } ) ;
104
127
} ,
105
128
106
129
storybookGetStats ( ) {
0 commit comments