@@ -16,7 +16,7 @@ export function rollupPluginPrebundleModules(env: Record<string, string>): Plugi
16
16
async buildStart ( ) {
17
17
const esbuildPluginCommonjs = ( await import ( '@chialab/esbuild-plugin-commonjs' ) ) . default ; // for CJS compatibility
18
18
19
- const modules = getModules ( ) ;
19
+ const modules = CANDIDATES . filter ( moduleExists ) ;
20
20
21
21
for ( const module of modules ) {
22
22
modulePaths [ module ] = join (
@@ -37,6 +37,11 @@ export function rollupPluginPrebundleModules(env: Record<string, string>): Plugi
37
37
assert : require . resolve ( 'browser-assert' ) ,
38
38
lodash : getNodeModuleDir ( 'lodash-es' ) , // more optimal, but also solves esbuild incorrectly compiling lodash/_nodeUtil
39
39
path : require . resolve ( 'path-browserify' ) ,
40
+
41
+ /* for @storybook/addon-docs */
42
+ ...( moduleExists ( '@storybook/react-dom-shim' ) && {
43
+ '@storybook/react-dom-shim' : getReactDomShimAlias ( ) ,
44
+ } ) ,
40
45
} ,
41
46
define : {
42
47
...stringifyProcessEnvs ( env ) ,
@@ -58,18 +63,6 @@ export function rollupPluginPrebundleModules(env: Record<string, string>): Plugi
58
63
} ;
59
64
}
60
65
61
- function getModules ( ) {
62
- const include = CANDIDATES . filter ( id => {
63
- try {
64
- require . resolve ( id , { paths : [ process . cwd ( ) ] } ) ;
65
- return true ;
66
- } catch ( e ) {
67
- return false ;
68
- }
69
- } ) ;
70
- return include ;
71
- }
72
-
73
66
// this is different to https://github.com/storybookjs/storybook/blob/v7.0.0/code/lib/builder-vite/src/optimizeDeps.ts#L7
74
67
// builder-vite bundles different dependencies for performance reasons
75
68
// we aim only at browserifying NodeJS dependencies (CommonJS/process.env/...)
@@ -88,6 +81,7 @@ export const CANDIDATES = [
88
81
'@testing-library/user-event' ,
89
82
90
83
/* for @storybook/addon-docs */
84
+ '@storybook/react-dom-shim' , // needs special resolution
91
85
'color-convert' ,
92
86
'doctrine' ,
93
87
'lodash/cloneDeep.js' ,
@@ -100,3 +94,19 @@ export const CANDIDATES = [
100
94
'react-dom' ,
101
95
'tocbot' ,
102
96
] ;
97
+
98
+ function getReactDomShimAlias ( ) {
99
+ const { version } = require ( 'react-dom' ) ;
100
+ return version . startsWith ( '18' )
101
+ ? require . resolve ( '@storybook/react-dom-shim/dist/react-18' ) . replace ( / \. j s $ / , '.mjs' )
102
+ : require . resolve ( '@storybook/react-dom-shim' ) . replace ( / \. j s $ / , '.mjs' ) ;
103
+ }
104
+
105
+ function moduleExists ( moduleName : string ) {
106
+ try {
107
+ require . resolve ( moduleName , { paths : [ process . cwd ( ) ] } ) ;
108
+ return true ;
109
+ } catch ( e ) {
110
+ return false ;
111
+ }
112
+ }
0 commit comments