Skip to content

Commit 19e5a5f

Browse files
committed
Reduce storybook build time with custom tweaks
- Shared - Remove CaseSensitivePathsPlugin - Prod - Use `swc` when minifying Further improvements may be possible in the future by upgrading to React 6.14.0 and Storybook 7 (when available), and using `storybook-addon-swc` as a babel replacement
1 parent ffdd3d8 commit 19e5a5f

File tree

1 file changed

+63
-28
lines changed

1 file changed

+63
-28
lines changed

.storybook/main.js

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const path = require('path');
22
const webpack = require('webpack');
3-
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
3+
4+
let SpeedMeasurePlugin;
5+
if (process.env.MEASURE) {
6+
SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
7+
}
48

59
module.exports = {
610
stories: ['../jsapp/**/*.stories.@(js|jsx|ts|tsx)'],
@@ -9,33 +13,32 @@ module.exports = {
913
'@storybook/addon-essentials',
1014
'@storybook/addon-interactions',
1115
'@storybook/addon-a11y',
16+
// NB:
17+
// 'storybook-addon-swc' may improve build speed in the future.
18+
// - At time of writing, the build performance gains are negated because it
19+
// switches to a slower refresh plugin and also causes other compatibility
20+
// issues in Storybook 6.
21+
// - Testing with React 16.14.0 and Storybook 7 (beta) seemed to perform
22+
// well.
1223
],
1324
framework: '@storybook/react',
1425
core: {
1526
builder: '@storybook/builder-webpack5',
1627
},
17-
webpackFinal: async (config, { configType }) => {
18-
config.plugins.push(new webpack.ProvidePlugin({ $: 'jquery' })),
19-
config.module.rules.push({
20-
resolve: {
21-
extensions: [
22-
'.jsx',
23-
'.js',
24-
'.es6',
25-
'.coffee',
26-
'.ts',
27-
'.tsx',
28-
'.scss',
29-
],
30-
alias: {
31-
app: path.join(__dirname, '../app'),
32-
jsapp: path.join(__dirname, '../jsapp'),
33-
js: path.join(__dirname, '../jsapp/js'),
34-
scss: path.join(__dirname, '../jsapp/scss'),
35-
utils: path.join(__dirname, '../jsapp/js/utils'),
36-
},
28+
webpackFinal: async (config, {configType}) => {
29+
config.plugins.push(new webpack.ProvidePlugin({$: 'jquery'}));
30+
config.module.rules.push({
31+
resolve: {
32+
extensions: ['.jsx', '.js', '.es6', '.coffee', '.ts', '.tsx', '.scss'],
33+
alias: {
34+
app: path.join(__dirname, '../app'),
35+
jsapp: path.join(__dirname, '../jsapp'),
36+
js: path.join(__dirname, '../jsapp/js'),
37+
scss: path.join(__dirname, '../jsapp/scss'),
38+
utils: path.join(__dirname, '../jsapp/js/utils'),
3739
},
38-
});
40+
},
41+
});
3942
config.module.rules.push(
4043
{
4144
test: /\.scss$/,
@@ -59,16 +62,48 @@ module.exports = {
5962
],
6063
}
6164
);
65+
66+
// Build speed improvements
67+
applySpeedTweaks(config);
68+
6269
// Print speed measurement if env variable MEASURE is set
63-
config.plugins.push(
64-
new SpeedMeasurePlugin({ disable: !process.env.MEASURE })
65-
);
70+
if (process.env.MEASURE) {
71+
const smp = new SpeedMeasurePlugin();
72+
return smp.wrap(config);
73+
}
6674
return config;
6775
},
6876
managerWebpack: async (config) => {
69-
config.plugins.push(
70-
new SpeedMeasurePlugin({ disable: !process.env.MEASURE })
71-
);
77+
// Build speed improvements
78+
applySpeedTweaks(config);
79+
80+
if (process.env.MEASURE) {
81+
const smp = new SpeedMeasurePlugin();
82+
return smp.wrap(config);
83+
}
7284
return config;
7385
},
7486
};
87+
88+
/// Apply some customizations to the config, intended to decrease build time
89+
function applySpeedTweaks(config) {
90+
// Remove a linter plugin added by Storybook: CaseSensitivePathsPlugin
91+
// - Its purpose is to prevent macOS devs from accidentally pushing code
92+
// that relies on filesystem case-insensitivity in file imports.
93+
// - We can let CI detect this instead, or use ESLint.
94+
// 'import/no-unresolved': [2, { caseSensitive: true }]
95+
config.plugins = config.plugins.filter(
96+
(plugin) => plugin.constructor.name !== 'CaseSensitivePathsPlugin'
97+
);
98+
99+
// Use swc to make the Terser step faster
100+
if (config.mode === 'production') {
101+
const TerserPlugin = require('terser-webpack-plugin');
102+
config.optimization.minimizer = [
103+
new TerserPlugin({
104+
minify: TerserPlugin.swcMinify,
105+
terserOptions: {},
106+
}),
107+
];
108+
}
109+
}

0 commit comments

Comments
 (0)