diff --git a/code/builders/builder-webpack5/src/preview/iframe-webpack.config.ts b/code/builders/builder-webpack5/src/preview/iframe-webpack.config.ts index 704d8fe2b98b..513b3c36849d 100644 --- a/code/builders/builder-webpack5/src/preview/iframe-webpack.config.ts +++ b/code/builders/builder-webpack5/src/preview/iframe-webpack.config.ts @@ -29,38 +29,42 @@ import { createBabelLoader, createSWCLoader } from './loaders'; const getAbsolutePath = (input: I): I => dirname(require.resolve(join(input, 'package.json'))) as any; +const maybeGetAbsolutePath = (input: I): I | false => { + try { + return getAbsolutePath(input); + } catch (e) { + return false; + } +}; +const managerAPIPath = maybeGetAbsolutePath(`@storybook/manager-api`); +const componentsPath = maybeGetAbsolutePath(`@storybook/components`); +const componentsExperimentalPath = componentsPath ? `${componentsPath}/dist/experimental` : false; +const globalPath = maybeGetAbsolutePath(`@storybook/global`); +const routerPath = maybeGetAbsolutePath(`@storybook/router`); +const themingPath = maybeGetAbsolutePath(`@storybook/theming`); + +// these packages are not pre-bundled because of react dependencies. +// these are not dependencies of the builder anymore, thus resolving them can fail. +// we should remove the aliases in 8.0, I'm not sure why they are here in the first place. const storybookPaths: Record = { - // this is a temporary hack to get webpack to alias this correctly - [`@storybook/components/experimental`]: `${getAbsolutePath( - `@storybook/components` - )}/dist/experimental`, - ...[ - // these packages are not pre-bundled because of react dependencies. - // these are not dependencies of the builder anymore, thus resolving them can fail. - // we should remove the aliases in 8.0, I'm not sure why they are here in the first place. - 'components', - 'global', - 'manager-api', - 'router', - 'theming', - ].reduce((acc, sbPackage) => { - let packagePath; - try { - packagePath = getAbsolutePath(`@storybook/${sbPackage}`); - } catch (e) { - // ignore - } - if (packagePath) { - return { - ...acc, - [`@storybook/${sbPackage}`]: getAbsolutePath(`@storybook/${sbPackage}`), - }; - } - return acc; - }, {}), - // deprecated, remove in 8.0 - [`@storybook/api`]: getAbsolutePath(`@storybook/manager-api`), + ...(managerAPIPath + ? { + // deprecated, remove in 8.0 + [`@storybook/api`]: managerAPIPath, + [`@storybook/manager-api`]: managerAPIPath, + } + : {}), + ...(componentsPath + ? { + // this is a temporary hack to get webpack to alias this correctly + [`@storybook/components/experimental`]: componentsExperimentalPath as any, + [`@storybook/components`]: componentsPath, + } + : {}), + ...(globalPath ? { [`@storybook/global`]: globalPath } : {}), + ...(routerPath ? { [`@storybook/router`]: routerPath } : {}), + ...(themingPath ? { [`@storybook/theming`]: themingPath } : {}), }; export default async (