#Vite build Storybook errors with window

16 messages · Page 1 of 1 (latest)

languid arrow
#

Should we do something to avoid those errors @turbid remnant ?

#

At the moment I am passing:

const defineConstant =
      configType === 'DEVELOPMENT' ? { global: 'window' } : {};

and then spreading it in define of viteConfig,

define: {
        ...config.define,
        ...defineConstant,
},

but should this maybe be in the docs as it's almost certain that people will use the addon-essentials?

turbid remnant
#

I haven’t seen that error before, I’ll have to look into it. Did it start in recent alpha versions? What if you try changing to alpha.43 or something?

languid arrow
earnest wren
#

isn't this the known bug we have, that we are solving by having a preview-head.htmlwith:

  // eslint-disable-next-line no-undef
  window.global = window;
</script>```
?
languid arrow
#

Is this shipped to a next version @earnest wren ?

earnest wren
#

by "solved" I mean that the user has to add this, for now. AFAIK we plan on solving it for real before 7.0 release

languid arrow
#

In my case the define setting solved this that's why I open this thread, not sure if useful at all for you

turbid remnant
#

@earnest wren His error looked different, it wasn't able to import from window...

earnest wren
#

yeah I see, it's the other way around

turbid remnant
#

Can you share the rest of your .storybook/main.cjs and vite.config.js (if you have one)? @languid arrow

languid arrow
#

@turbid remnant this is the only piece I use as a vite config, as the importing react library does not have one

#

For the main.js

import { mergeConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
import path from 'path';

export default {
  framework: '@storybook/react-vite',
  features: {
    storyStoreV7: true,
  },
  docs: {
    docsPage: true,
    defaultName: 'Docs',
  },
  stories: ['../src/**/*.stories.@(js|jsx|ts|tsx|mdx)'],
  staticDirs: ['../src/assets'],
  addons: [
    '@storybook/addon-links',
    '@storybook/addon-essentials',
    '@storybook/addon-a11y',
    '@storybook/addon-storysource',
    // 'storybook-addon-pseudo-states',
  ],
  async viteFinal(config, { configType }) {
    const defineConstant =
      configType === 'DEVELOPMENT' ? { global: 'window' } : {};
    /** @type {import('vite').UserConfig} */
    const customViteConfig = {
      plugins: [tsconfigPaths()],
      resolve: {
        alias: {
          '@neo4j-ndl/react/src': path.resolve(__dirname, '../../react/src'),
        },
      },
      define: {
        ...config.define,
        ...defineConstant,
      },
    };
    // Merge custom configuration into the default config
    return mergeConfig(config, customViteConfig);
  },
};
#

Here it is

turbid remnant
#

thanks. One interesting part, is that your defineConstant will only apply in dev, but I think configType is production during builds, where it is crashing, right?

#

So, wouldn't your define config do nothing during builds?