Nx Storybook (Webpack 5) "exports is not defined" and infinite loading

Viewed 37

I have upgraded Nx packages to latest version 14.7.5 and it broke my Storybook build which was still using Webpack 4. So I figured updating it to Webpack 5 might fix the problem and after the migration it is successfully building, but the Storybook gets stuck on the main page and infinitely shows the loading animation (XHR call /progress never returns) and in console there is Uncaught ReferenceError: exports is not defined error. This error comes from one of my generated files that is in JS (rest of the files is in TypeScript). I am not sure if the error is causing the infinite loading but it is my only clue.

When I build the Storybook statically it also shows this warning during the compilation:

 export 'AssetService' (reexported as 'AssetService') was not found in './protos/Asset_pb_service' (module has no exports)

which comes from the same file as the error in the console so it might be connected (the files has exports though).

My main Storybook configuration:

module.exports = {
  core: {
    builder: 'webpack5',
  },
  typescript: { reactDocgen: false },
  stories: [],
  addons: [
    {
      name: '@storybook/addon-essentials',
      options: {
        backgrounds: true,
        viewport: false,
      },
    },
    '@nrwl/react/plugins/storybook',
    'storybook-dark-mode'
  ],
};

Storybook configuration of the main app:

// This loads the configuration above
const rootMain = require('../../../.storybook/main');

module.exports = {
  ...rootMain,

  core: { ...rootMain.core, builder: 'webpack5' },

  stories: [
    ...rootMain.stories,
    '../src/**/*.stories.mdx',
    '../src/**/*.stories.@(js|jsx|ts|tsx)',
    '../*.stories.mdx',
    '../../../libs/**/*.stories.mdx',
    '../../../libs/**/*.stories.@(js|jsx|ts|tsx)',
  ],
  webpackFinal: async (config, { configType }) => {
    // apply any global webpack configs that might have been specified in .storybook/main.js
    if (rootMain.webpackFinal) {
      config = await rootMain.webpackFinal(config, { configType });
    }

    // add your own webpack tweaks if needed

    return config;
  },
};

Storybook package versions:

    "@storybook/addon-docs": "6.5.12",
    "@storybook/addon-essentials": "6.5.12",
    "@storybook/builder-webpack5": "6.5.12",
    "@storybook/core-server": "6.5.12",
    "@storybook/manager-webpack5": "6.5.12",
    "@storybook/react": "6.5.12",

Any help would be appreciated.

1 Answers

It was caused by preview.js which was wrapping each story with some providers and it was indirectly importing the JS file that was causing the error. It seems like this preview.js is not compiled the same way as the stories, because after moving the problematic providers to the stories that needed them, Storybook started working again.

Related