Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Fix package duplication issues by aliasing all storybook packages #11092

Merged
merged 11 commits into from
Jun 11, 2020
11 changes: 11 additions & 0 deletions examples/cra-kitchen-sink/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const path = require('path');

module.exports = {
addons: [
'@storybook/preset-create-react-app',
Expand All @@ -16,4 +18,13 @@ module.exports = {
'@storybook/addon-jest',
],
stories: ['../src/stories/**/*.stories.@(js|mdx)'],
webpackFinal: (config) => {
// add monorepo root as a valid directory to import modules from
config.resolve.plugins.forEach((p) => {
if (Array.isArray(p.appSrcs)) {
p.appSrcs.push(path.join(__dirname, '..', '..', '..'));
}
});
return config;
},
};
11 changes: 11 additions & 0 deletions examples/cra-react15/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
const path = require('path');

module.exports = {
stories: ['../src/stories/welcome.stories', '../src/stories/**/button.stories.js'],
addons: [
'@storybook/preset-create-react-app',
'@storybook/addon-actions',
'@storybook/addon-links',
],
webpackFinal: (config) => {
// add monorepo root as a valid directory to import modules from
config.resolve.plugins.forEach((p) => {
if (Array.isArray(p.appSrcs)) {
p.appSrcs.push(path.join(__dirname, '..', '..', '..'));
}
});
return config;
},
};
11 changes: 11 additions & 0 deletions examples/cra-ts-essentials/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const path = require('path');

module.exports = {
stories: ['../src/**/*.stories.tsx'],
addons: [
Expand All @@ -9,4 +11,13 @@ module.exports = {
},
},
],
webpackFinal: (config) => {
// add monorepo root as a valid directory to import modules from
config.resolve.plugins.forEach((p) => {
if (Array.isArray(p.appSrcs)) {
p.appSrcs.push(path.join(__dirname, '..', '..', '..'));
}
});
return config;
},
};
15 changes: 14 additions & 1 deletion examples/cra-ts-kitchen-sink/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @ts-ignore
// eslint-disable-next-line import/no-extraneous-dependencies
import { Configuration } from 'webpack';

const path = require('path');

module.exports = {
Expand Down Expand Up @@ -26,4 +28,15 @@ module.exports = {
'@storybook/addon-links',
'@storybook/addon-a11y',
],
webpackFinal: (config: Configuration) => {
// add monorepo root as a valid directory to import modules from
config.resolve.plugins.forEach((p) => {
// @ts-ignore
if (Array.isArray(p.appSrcs)) {
// @ts-ignore
p.appSrcs.push(path.join(__dirname, '..', '..', '..'));
}
});
return config;
},
};
2 changes: 2 additions & 0 deletions lib/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
"@babel/register": "^7.8.3",
"@storybook/addons": "6.0.0-beta.23",
"@storybook/api": "6.0.0-beta.23",
"@storybook/channels": "6.0.0-beta.23",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are added to be sure to have them and be able to have a working alias?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah all packages of @storybook/* that are conceivably used are aliased here so that there can only 1 of them in the entire bundle.

"@storybook/channel-postmessage": "6.0.0-beta.23",
"@storybook/components": "6.0.0-beta.23",
"@storybook/client-api": "6.0.0-beta.23",
"@storybook/client-logger": "6.0.0-beta.23",
"@storybook/core-events": "6.0.0-beta.23",
Expand Down
15 changes: 14 additions & 1 deletion lib/core/src/server/manager/manager-webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import HtmlWebpackPlugin from 'html-webpack-plugin';
import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin';
import PnpWebpackPlugin from 'pnp-webpack-plugin';
import VirtualModulePlugin from 'webpack-virtual-modules';
import TerserWebpackPlugin from 'terser-webpack-plugin';

import themingPaths from '@storybook/theming/paths';
import uiPaths from '@storybook/ui/paths';
Expand Down Expand Up @@ -139,7 +140,6 @@ export default async ({
alias: {
...themingPaths,
...uiPaths,
semver: require.resolve('@storybook/semver'),
},
plugins: [
// Transparently resolve packages via PnP when needed; noop otherwise
Expand All @@ -158,6 +158,19 @@ export default async ({
chunks: 'all',
},
runtimeChunk: true,
minimizer: isProd
? [
new TerserWebpackPlugin({
cache: true,
parallel: true,
sourceMap: true,
terserOptions: {
mangle: false,
keep_fnames: true,
},
}),
]
: [],
},
};
};
63 changes: 43 additions & 20 deletions lib/core/src/server/preview/iframe-webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';

import resolveFrom from 'resolve-from';

import themingPaths from '@storybook/theming/paths';

import { createBabelLoader } from './babel-loader-preview';
import es6Transpiler from '../common/es6Transpiler';

Expand All @@ -21,6 +23,30 @@ import { toRequireContextString } from './to-require-context';
import { useBaseTsSupport } from '../config/useBaseTsSupport';

const reactPaths = {};

const storybookPaths = [
'addons',
'addons',
'api',
'channels',
'channel-postmessage',
'components',
'core-events',
'router',
'theming',
'semver',
'client-api',
'client-logger',
].reduce(
(acc, package) => ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package is not a valid variable name in JS 😉 => sbPackage

...acc,
[`@storybook/${package}`]: path.dirname(
resolveFrom(__dirname, `@storybook/${package}/package.json`)
),
}),
{}
);

try {
reactPaths.react = path.dirname(resolveFrom(process.cwd(), 'react/package.json'));
reactPaths['react-dom'] = path.dirname(resolveFrom(process.cwd(), 'react-dom/package.json'));
Expand Down Expand Up @@ -64,13 +90,8 @@ export default async ({
const match = entryFilename.match(/(.*)-generated-(config|other)-entry.js$/);
if (match) {
const configFilename = match[1];
const isUsingYarnPnp = typeof process.versions.pnp !== 'undefined';
const clientApi = isUsingYarnPnp
? `${require.resolve('@storybook/client-api')}`
: '@storybook/client-api';
const clientLogger = isUsingYarnPnp
? `${require.resolve('@storybook/client-logger')}`
: '@storybook/client-logger';
const clientApi = storybookPaths['@storybook/client-api'];
const clientLogger = storybookPaths['@storybook/client-logger'];

virtualModuleMapping[entryFilename] = interpolate(entryTemplate, {
configFilename,
Expand Down Expand Up @@ -159,8 +180,8 @@ export default async ({
extensions: ['.mjs', '.js', '.jsx', '.ts', '.tsx', '.json', '.cjs'],
modules: ['node_modules'].concat(raw.NODE_PATH || []),
alias: {
'babel-runtime/core-js/object/assign': require.resolve('core-js/es/object/assign'),
semver: require.resolve('@storybook/semver'),
...themingPaths,
...storybookPaths,
...reactPaths,
},

Expand All @@ -177,17 +198,19 @@ export default async ({
chunks: 'all',
},
runtimeChunk: true,
minimizer: [
new TerserWebpackPlugin({
cache: true,
parallel: true,
sourceMap: true,
terserOptions: {
mangle: false,
keep_fnames: true,
},
}),
],
minimizer: isProd
? [
new TerserWebpackPlugin({
cache: true,
parallel: true,
sourceMap: true,
terserOptions: {
mangle: false,
keep_fnames: true,
},
}),
]
: [],
},
performance: {
hints: isProd ? 'warning' : false,
Expand Down