[Storybook] Fix Webpack5 lazyCompilation and fsCache in Nx project
See original GitHub issueHey @mandarini 🙂, I found what is missing for lazyCompilation and fsCache to work 🎉
🚀 We noticed a huge performance improvement -> more than 10X ⚡ from ~90s to ~7s in dev mode:
$ nx storybook $myProject
How Storybook work?
Setting the following builder …
// .storybook/main.js
module.exports = {
core: {
builder: {
name: 'builder-webpack5',
options: {
lazyCompilation: true,
fsCache: true,
},
},
},
...
… the base webpack configuration should be composed with the following options as a fresh install of Storybook without Nx
const webpackBaseConfig = {
// base config for `lazyCompilation` and `fsCache` to work
optimization: {
splitChunks: { chunks: 'all' },
runtimeChunk: true,
sideEffects: true,
usedExports: false,
moduleIds: 'named',
minimizer: [],
},
cache: { type: 'filesystem' },
experiments: { lazyCompilation: { entries: false } },
// other base config....
}
What needs to do Nx side?
Considering lazyCompilation and fsCache builder options as did by Storybook here:
https://github.com/storybookjs/storybook/blob/7035ea7389393da041985ebc491ee58dedb50d06/code/lib/builder-webpack5/src/preview/base-webpack.config.ts#L53-L55
My final configuration for maximizing the performance (SWC + lazyCompilation + devtool = false)
// .storybook/main.js
module.exports = {
webpackFinal: async (config, { configType }) => {
if (configType === 'DEVELOPMENT') {
// not needed for `lazyCompilation` but you get fastest performance in rebuild with webpack
config.devtool = false
config = {
...config,
optimization: {
splitChunks: { chunks: 'all' },
runtimeChunk: true,
sideEffects: true,
usedExports: false,
moduleIds: 'named',
minimizer: [],
},
cache: { type: 'filesystem' },
experiments: { lazyCompilation: { entries: false } },
}
}
return config
},
addons: [
{
name: 'storybook-addon-swc',
options: {
enable: true,
enableSwcLoader: true,
enableSwcMinify: false,
swcLoaderOptions: {
jsc: {
experimental: {
plugins: [
[
// ONLY if you use styled-components lib
'@swc/plugin-styled-components',
{
displayName: true,
ssr: true,
},
],
],
},
},
},
swcMinifyOptions: {},
},
},
features: {
babelModeV7: true,
// code splitting
storyStoreV7: true,
},
REPORT
Node : 16.13.1
OS : darwin x64
pnpm : 7.13.0
nx : 14.8.2
@nrwl/angular : Not Found
@nrwl/cypress : 14.7.17
@nrwl/detox : Not Found
@nrwl/devkit : 14.8.2
@nrwl/esbuild : Not Found
@nrwl/eslint-plugin-nx : 14.8.2
@nrwl/expo : Not Found
@nrwl/express : Not Found
@nrwl/jest : 14.8.2
@nrwl/js : 14.8.2
@nrwl/linter : 14.8.2
@nrwl/nest : Not Found
@nrwl/next : 14.8.2
@nrwl/node : Not Found
@nrwl/nx-cloud : Not Found
@nrwl/nx-plugin : Not Found
@nrwl/react : 14.8.2
@nrwl/react-native : Not Found
@nrwl/rollup : 14.8.2
@nrwl/schematics : Not Found
@nrwl/storybook : 14.8.2
@nrwl/web : 14.8.2
@nrwl/webpack : 14.8.2
@nrwl/workspace : 14.8.2
typescript : 4.8.4
---------------------------------------
Local workspace plugins:
---------------------------------------
Community plugins:
@jscutlery/semver: 2.26.0
ngx-deploy-npm: 4.1.0
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:16 (12 by maintainers)
Top Results From Across the Web
Webpack - Storybook
Storybook is a frontend workshop for building UI components and pages in isolation. Thousands of teams use it for UI development, testing, and...
Read more >Storybook Webpack Migration - Nx
Next generation build system with first class monorepo support and powerful integrations.
Read more >storybook/builder-webpack5@6.5.0 breaks the ... - Issuehunt
Describe the bug A clear and concise description of what the bug is. When in condition above, compilation failed with. ERR! TypeError: Cannot...
Read more >@storybook/web-components: Versions | Openbase
Full version history for @storybook/web-components including change logs. ... usage from storybook scripts #19366; Webpack5: Fix lazy compilation/fscache ...
Read more >storybook 6.5.15 - Download, Browsing & More | Fossies Archive
Storybook is a JavaScript tool for building UI components and ... 15:59 docs/snippets/common/storybook-main-webpack5-fsCache.js.mdx 173 ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@mandarini thank you for details!
This PR was created on Storybook to address this issue!