Skip to content

Commit cbcc56f

Browse files
committed
fix(storybook-builder): fix tocbot exports for addon-docs
1 parent 5a42855 commit cbcc56f

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web/storybook-builder': patch
3+
---
4+
5+
fix tocbot exports for addon-docs
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Plugin } from 'esbuild';
2+
3+
export function esbuildPluginCommonjsNamedExports(module: string, namedExports: string[]): Plugin {
4+
return {
5+
name: 'commonjs-named-exports',
6+
setup(build) {
7+
build.onResolve({ filter: new RegExp(`^${module}$`) }, args => {
8+
return {
9+
path: args.path,
10+
namespace: `commonjs-named-exports-${module}`,
11+
pluginData: {
12+
resolveDir: args.resolveDir,
13+
},
14+
};
15+
});
16+
build.onLoad({ filter: /.*/, namespace: `commonjs-named-exports-${module}` }, async args => {
17+
return {
18+
resolveDir: args.pluginData.resolveDir,
19+
contents: `
20+
import { default as commonjsExports } from '${module}?force-original';
21+
${namedExports
22+
.map(name => `export const ${name} = commonjsExports.${name};`)
23+
.join('\n')}
24+
`,
25+
};
26+
});
27+
},
28+
};
29+
}

packages/storybook-builder/src/rollup-plugin-prebundle-modules.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { stringifyProcessEnvs } from '@storybook/core-common';
22
import { build } from 'esbuild';
33
import { join } from 'path';
44
import type { Plugin } from 'rollup';
5+
import { esbuildPluginCommonjsNamedExports } from './esbuild-plugin-commonjs-named-exports.js';
56
import { getNodeModuleDir } from './get-node-module-dir.js';
67

78
export const PREBUNDLED_MODULES_DIR = 'node_modules/.prebundled_modules';
@@ -13,7 +14,7 @@ export function rollupPluginPrebundleModules(env: Record<string, string>): Plugi
1314
name: 'rollup-plugin-prebundle-modules',
1415

1516
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
1718

1819
const modules = getModules();
1920

@@ -40,7 +41,14 @@ export function rollupPluginPrebundleModules(env: Record<string, string>): Plugi
4041
define: {
4142
...stringifyProcessEnvs(env),
4243
},
43-
plugins: [esbuildCommonjsPlugin()],
44+
plugins: [
45+
/* for @storybook/addon-docs */
46+
// tocbot can't be automatically transformed by @chialab/esbuild-plugin-commonjs
47+
// so we need a manual wrapper
48+
esbuildPluginCommonjsNamedExports('tocbot', ['init', 'destroy']),
49+
50+
esbuildPluginCommonjs(),
51+
],
4452
});
4553
},
4654

0 commit comments

Comments
 (0)