@@ -2,6 +2,7 @@ import { stringifyProcessEnvs } from '@storybook/core-common';
2
2
import { build } from 'esbuild' ;
3
3
import { join } from 'path' ;
4
4
import type { Plugin } from 'rollup' ;
5
+ import { esbuildPluginCommonjsNamedExports } from './esbuild-plugin-commonjs-named-exports.js' ;
5
6
import { getNodeModuleDir } from './get-node-module-dir.js' ;
6
7
7
8
export const PREBUNDLED_MODULES_DIR = 'node_modules/.prebundled_modules' ;
@@ -13,9 +14,9 @@ export function rollupPluginPrebundleModules(env: Record<string, string>): Plugi
13
14
name : 'rollup-plugin-prebundle-modules' ,
14
15
15
16
async buildStart ( ) {
16
- const esbuildCommonjsPlugin = ( await import ( '@chialab/esbuild-plugin-commonjs' ) ) . default ; // for CJS compatibility
17
+ const esbuildPluginCommonjs = ( await import ( '@chialab/esbuild-plugin-commonjs' ) ) . default ; // for CJS compatibility
17
18
18
- const modules = getModules ( ) ;
19
+ const modules = CANDIDATES . filter ( moduleExists ) ;
19
20
20
21
for ( const module of modules ) {
21
22
modulePaths [ module ] = join (
@@ -36,11 +37,23 @@ export function rollupPluginPrebundleModules(env: Record<string, string>): Plugi
36
37
assert : require . resolve ( 'browser-assert' ) ,
37
38
lodash : getNodeModuleDir ( 'lodash-es' ) , // more optimal, but also solves esbuild incorrectly compiling lodash/_nodeUtil
38
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
+ } ) ,
39
45
} ,
40
46
define : {
41
47
...stringifyProcessEnvs ( env ) ,
42
48
} ,
43
- plugins : [ esbuildCommonjsPlugin ( ) ] ,
49
+ plugins : [
50
+ /* for @storybook/addon-docs */
51
+ // tocbot can't be automatically transformed by @chialab/esbuild-plugin-commonjs
52
+ // so we need a manual wrapper
53
+ esbuildPluginCommonjsNamedExports ( 'tocbot' , [ 'init' , 'destroy' ] ) ,
54
+
55
+ esbuildPluginCommonjs ( ) ,
56
+ ] ,
44
57
} ) ;
45
58
} ,
46
59
@@ -50,18 +63,6 @@ export function rollupPluginPrebundleModules(env: Record<string, string>): Plugi
50
63
} ;
51
64
}
52
65
53
- function getModules ( ) {
54
- const include = CANDIDATES . filter ( id => {
55
- try {
56
- require . resolve ( id , { paths : [ process . cwd ( ) ] } ) ;
57
- return true ;
58
- } catch ( e ) {
59
- return false ;
60
- }
61
- } ) ;
62
- return include ;
63
- }
64
-
65
66
// this is different to https://github.com/storybookjs/storybook/blob/v7.0.0/code/lib/builder-vite/src/optimizeDeps.ts#L7
66
67
// builder-vite bundles different dependencies for performance reasons
67
68
// we aim only at browserifying NodeJS dependencies (CommonJS/process.env/...)
@@ -80,6 +81,32 @@ export const CANDIDATES = [
80
81
'@testing-library/user-event' ,
81
82
82
83
/* for @storybook/addon-docs */
84
+ '@storybook/react-dom-shim' , // needs special resolution
85
+ 'color-convert' ,
83
86
'doctrine' ,
87
+ 'lodash/cloneDeep.js' ,
84
88
'lodash/mapValues.js' ,
89
+ 'lodash/pickBy.js' ,
90
+ 'lodash/throttle.js' ,
91
+ 'lodash/uniq.js' ,
92
+ 'memoizerific' ,
93
+ 'react' ,
94
+ 'react-dom' ,
95
+ 'tocbot' ,
85
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