Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelangen committed Aug 21, 2023
1 parent ff7477e commit 78c2d88
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions code/builders/builder-webpack5/src/preview/iframe-webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,42 @@ import { createBabelLoader, createSWCLoader } from './loaders';

const getAbsolutePath = <I extends string>(input: I): I =>
dirname(require.resolve(join(input, 'package.json'))) as any;
const maybeGetAbsolutePath = <I extends string>(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<string, string> = {
// 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 (
Expand Down

0 comments on commit 78c2d88

Please sign in to comment.