storybook: [Bug]: 7.6.5 @storybook/react-vite runs fine in dev but builds have JavaScript error that blocks loading

Describe the bug

Working on a revision to @carbon/charts-react to use @storybook/react-vite instead of @storybook/react (Webpack).

When I run npx storybook dev -p 9006, the Storybook runs and behaves as expected. When I do a build of the Storybook npx storybook build -o demo/bundle then run that either using a local web server or push it to Vercel, I get a JavaScript console error…

Uncaught ReferenceError: dc is not defined for this line…

dc.__docgenInfo = {
	description: '',
	methods: [
		{ name: 'addLineEventListener', docblock: null, modifiers: [], params: [], returns: null },
		{ name: 'addNodeEventListener', docblock: null, modifiers: [], params: [], returns: null },
		{
			name: 'traverse',
			docblock: null,
			modifiers: [],
			params: [
				{ name: 'e', optional: !1, type: null },
				{ name: 't', optional: !1, type: null },
				{ name: 's', optional: !0, type: null }
			],
			returns: null
		},
		{ name: 'getRightArrowIcon', docblock: null, modifiers: [], params: [], returns: null },
		{ name: 'destroy', docblock: null, modifiers: [], params: [], returns: null }
	],
	displayName: 'dc'
}

Here is my .storybook/main.ts for the @carbon/charts-react package…

import { mergeConfig, type InlineConfig } from 'vite'
import type { StorybookConfig } from '@storybook/react-vite'

const config: StorybookConfig = {
	stories: ['../src/stories/**/*.stories.tsx', '../src/stories/**/*.mdx'],
	staticDirs: ['../../core/.storybook/assets'],

	addons: [
		{
			name: '@storybook/addon-essentials',
			options: {
				actions: false
			}
		}
	],

	framework: {
		name: '@storybook/react-vite',
		options: {}
	},

	docs: {
		autodocs: 'tag'
	},

	// typescript: {
	// 	reactDocgen: 'react-docgen'
	// },

	async viteFinal(config: InlineConfig) {
		config.plugins = config.plugins!.filter((plugin) => plugin!.name !== 'vite:dts')
		const newConfig: InlineConfig = mergeConfig(config, {
			build: {
				chunkSizeWarningLimit: 1800,
			},
			optimizeDeps: {
				include: [
					'@carbon/charts'
				]
			}
		})
		// console.log('Vite Config', newConfig)
		return newConfig
	},

	features: {
		storyStoreV7: false // required for storiesOf API
	}
}
export default config

CLUES:

  1. If I comment out the optimizeDeps.include for @carbon/charts, I get the SAME error when I run the dev server.
  2. Other packages do not have the problem - @carbon/charts uses @storybook/html-vite, @carbon/charts-svelte uses @storybook/sveltekit, @carbon/charts-vue uses @storybook/vue3-vite. These component libraries are thin wrappers around @carbon/charts
  3. Using @storybook/react (Webpack) works but the goal is to get rid of Webpack.

To Reproduce

Cannot reproduce with a minimal repro but the steps to reproduce are not complicated…

  1. Install the monorepo
git clone https://github.com/nstuyvesant/carbon-charts.git
cd carbon-charts
yarn install
yarn build
cd packages/react
  1. Edit packages/react/.storybook/main.ts and comment out these lines
	typescript: {
		reactDocgen: false // Required to overcome https://github.com/storybookjs/storybook/issues/25247
	},
  1. Run yarn storybook to see it working at http://localhost:9006
  2. Stop the dev server
  3. Run yarn build:demo
  4. Using a local web server, host demo/bundle with index.html as the default doc then access it via http://localhost. Storybook will not fully load and the error mentioned above will be logged to the JavaScript Console.

System

Storybook Environment Info:

  System:
    OS: macOS 14.2
    CPU: (10) arm64 Apple M1 Pro
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.19.0 - /opt/homebrew/bin/node
    Yarn: 4.0.2 - /opt/homebrew/bin/yarn <----- active
    npm: 10.2.5 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 120.0.6099.109
    Safari: 17.2

  Other important environmental info:
    Monorepo: Yes
    Storybook: 7.6.5
    Lerna (nx): 8.0.1
    Vite: 5.0.10
    TypeScript: 5.3.3
    React: 18.2.0

Additional context

Our .storybook/main.ts must have features.storyStoreV7 set to false because we have almost 1,000 stories generated via the storiesOf() API.

Other things I tried without success…

  1. Setting typescript.reactDocgen to ‘react-docgen’ without success.
  2. Setting build.rollupOptions.treeshake = false to the config in viteFinal().
  3. Reverting to Vite 4

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

@nstuyvesant I did try that as well (in both main.js and in my main vite config) but that didn’t fix it. I was investigating more yesterday and it seems like tree shaking isn’t the culprit, which would explain why optimizeDeps wouldn’t do anything.

The index.js where the docgen is being added imports everything as defaultX then re-exports them with the proper named exports. So the actual component name is never used in the scope of the file itself.

This is our source index file:

export * from "./components"

And the vite/rollup output of that file is:

import { default as default9 } from "./components/MyComponent";

export { default9 as MyComponent }

So when docgen adds the reference for MyComponent at the bottom of that file, MyComponent isn’t in scope.

Just got a workaround…

	typescript: {
		reactDocgen: false
	},

I’m not clear what I’ve lost in the process as the generated build appears to do everything it used to do when I used the Webpack version.