@@ -8,12 +8,7 @@ import { generateImportFnScriptCode } from '../codegen-importfn-script';
8
8
import { generateModernIframeScriptCode } from '../codegen-modern-iframe-script' ;
9
9
import { generateAddonSetupCode } from '../codegen-set-addon-channel' ;
10
10
import { transformIframeHtml } from '../transform-iframe-html' ;
11
- import {
12
- virtualAddonSetupFile ,
13
- virtualFileId ,
14
- virtualPreviewFile ,
15
- virtualStoriesFile ,
16
- } from '../virtual-file-names' ;
11
+ import { SB_VIRTUAL_FILES , getResolvedVirtualModuleId } from '../virtual-file-names' ;
17
12
18
13
export function codeGeneratorPlugin ( options : Options ) : Plugin {
19
14
const iframePath = require . resolve ( '@storybook/builder-vite/input/iframe.html' ) ;
@@ -28,11 +23,15 @@ export function codeGeneratorPlugin(options: Options): Plugin {
28
23
// invalidate the whole vite-app.js script on every file change.
29
24
// (this might be a little too aggressive?)
30
25
server . watcher . on ( 'change' , ( ) => {
31
- const appModule = server . moduleGraph . getModuleById ( virtualFileId ) ;
26
+ const appModule = server . moduleGraph . getModuleById (
27
+ getResolvedVirtualModuleId ( SB_VIRTUAL_FILES . VIRTUAL_APP_FILE )
28
+ ) ;
32
29
if ( appModule ) {
33
30
server . moduleGraph . invalidateModule ( appModule ) ;
34
31
}
35
- const storiesModule = server . moduleGraph . getModuleById ( virtualStoriesFile ) ;
32
+ const storiesModule = server . moduleGraph . getModuleById (
33
+ getResolvedVirtualModuleId ( SB_VIRTUAL_FILES . VIRTUAL_STORIES_FILE )
34
+ ) ;
36
35
if ( storiesModule ) {
37
36
server . moduleGraph . invalidateModule ( storiesModule ) ;
38
37
}
@@ -45,7 +44,7 @@ export function codeGeneratorPlugin(options: Options): Plugin {
45
44
// TODO maybe use the stories declaration in main
46
45
if ( / \. s t o r i e s \. ( [ t j ] ) s x ? $ / . test ( path ) || / \. m d x $ / . test ( path ) ) {
47
46
// We need to emit a change event to trigger HMR
48
- server . watcher . emit ( 'change' , virtualStoriesFile ) ;
47
+ server . watcher . emit ( 'change' , SB_VIRTUAL_FILES . VIRTUAL_STORIES_FILE ) ;
49
48
}
50
49
} ) ;
51
50
} ,
@@ -69,34 +68,34 @@ export function codeGeneratorPlugin(options: Options): Plugin {
69
68
iframeId = `${ config . root } /iframe.html` ;
70
69
} ,
71
70
resolveId ( source ) {
72
- if ( source === virtualFileId ) {
73
- return ` ${ virtualFileId } ` ;
71
+ if ( source === SB_VIRTUAL_FILES . VIRTUAL_APP_FILE ) {
72
+ return getResolvedVirtualModuleId ( SB_VIRTUAL_FILES . VIRTUAL_APP_FILE ) ;
74
73
}
75
74
if ( source === iframePath ) {
76
75
return iframeId ;
77
76
}
78
- if ( source === virtualStoriesFile ) {
79
- return ` ${ virtualStoriesFile } ` ;
77
+ if ( source === SB_VIRTUAL_FILES . VIRTUAL_STORIES_FILE ) {
78
+ return getResolvedVirtualModuleId ( SB_VIRTUAL_FILES . VIRTUAL_STORIES_FILE ) ;
80
79
}
81
- if ( source === virtualPreviewFile ) {
82
- return virtualPreviewFile ;
80
+ if ( source === SB_VIRTUAL_FILES . VIRTUAL_PREVIEW_FILE ) {
81
+ return getResolvedVirtualModuleId ( SB_VIRTUAL_FILES . VIRTUAL_PREVIEW_FILE ) ;
83
82
}
84
- if ( source === virtualAddonSetupFile ) {
85
- return ` ${ virtualAddonSetupFile } ` ;
83
+ if ( source === SB_VIRTUAL_FILES . VIRTUAL_ADDON_SETUP_FILE ) {
84
+ return getResolvedVirtualModuleId ( SB_VIRTUAL_FILES . VIRTUAL_ADDON_SETUP_FILE ) ;
86
85
}
87
86
88
87
return undefined ;
89
88
} ,
90
89
async load ( id , config ) {
91
- if ( id === ` ${ virtualStoriesFile } ` ) {
90
+ if ( id === getResolvedVirtualModuleId ( SB_VIRTUAL_FILES . VIRTUAL_STORIES_FILE ) ) {
92
91
return generateImportFnScriptCode ( options ) ;
93
92
}
94
93
95
- if ( id === ` ${ virtualAddonSetupFile } ` ) {
94
+ if ( id === getResolvedVirtualModuleId ( SB_VIRTUAL_FILES . VIRTUAL_ADDON_SETUP_FILE ) ) {
96
95
return generateAddonSetupCode ( ) ;
97
96
}
98
97
99
- if ( id === ` ${ virtualFileId } ` ) {
98
+ if ( id === getResolvedVirtualModuleId ( SB_VIRTUAL_FILES . VIRTUAL_APP_FILE ) ) {
100
99
return generateModernIframeScriptCode ( options , projectRoot ) ;
101
100
}
102
101
0 commit comments