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

Upgrade webpack to version 5 #26382

Merged
merged 7 commits into from
Jan 3, 2021
Merged

Upgrade webpack to version 5 #26382

merged 7 commits into from
Jan 3, 2021

Conversation

jsnajdr
Copy link
Member

@jsnajdr jsnajdr commented Oct 22, 2020

Closes #21647.

This is a draft PR that upgrades webpack to version 5. There are 18 small commits that make independent changes that are needed. Most can be merged as separate PRs, to make things compatible between v4 and v5 before actually doing the upgrade.

Migration guide:
https://webpack.js.org/migrate/5/

Cc @scinos who's also interested in webpack 5 but is not a member of this repo, so I cannot add him as a reviewer 🙂

@jsnajdr jsnajdr self-assigned this Oct 22, 2020
@github-actions
Copy link

github-actions bot commented Oct 22, 2020

Size Change: -34.1 kB (-3%)

Total Size: 1.25 MB

Filename Size Change
build/a11y/index.js 1.01 kB -133 B (-12%) 👏
build/annotations/index.js 3.48 kB -326 B (-9%)
build/api-fetch/index.js 3.19 kB -225 B (-7%)
build/autop/index.js 2.6 kB -231 B (-8%)
build/blob/index.js 514 B -151 B (-23%) 🎉
build/block-directory/index.js 8.16 kB -548 B (-6%)
build/block-editor/index.js 125 kB -3.73 kB (-3%)
build/block-library/index.js 148 kB -1.73 kB (-1%)
build/block-serialization-default-parser/index.js 1.63 kB -244 B (-13%) 👏
build/block-serialization-spec-parser/index.js 2.77 kB -292 B (-10%) 👏
build/blocks/index.js 47.3 kB -775 B (-2%)
build/components/index.js 163 kB -7.95 kB (-5%)
build/compose/index.js 10.7 kB -352 B (-3%)
build/core-data/index.js 14.7 kB -418 B (-3%)
build/data-controls/index.js 676 B -153 B (-18%) 👏
build/data/index.js 8.62 kB -369 B (-4%)
build/date/index.js 31.4 kB -387 B (-1%)
build/deprecated/index.js 610 B -158 B (-21%) 🎉
build/dom-ready/index.js 418 B -153 B (-27%) 🎉
build/dom/index.js 4.67 kB -286 B (-6%)
build/edit-navigation/index.js 10.6 kB -546 B (-5%)
build/edit-post/index.js 303 kB -2.79 kB (-1%)
build/edit-site/index.js 23.3 kB -1.19 kB (-5%)
build/edit-widgets/index.js 24.1 kB -2.25 kB (-9%)
build/editor/index.js 41.6 kB -1.21 kB (-3%)
build/element/index.js 4.22 kB -409 B (-9%)
build/escape-html/index.js 578 B -157 B (-21%) 🎉
build/format-library/index.js 6.24 kB -504 B (-7%)
build/hooks/index.js 1.75 kB -524 B (-23%) 🎉
build/html-entities/index.js 476 B -146 B (-23%) 🎉
build/i18n/index.js 3.43 kB -137 B (-4%)
build/is-shallow-equal/index.js 552 B -146 B (-21%) 🎉
build/keyboard-shortcuts/index.js 2.22 kB -313 B (-12%) 👏
build/keycodes/index.js 1.67 kB -261 B (-13%) 👏
build/list-reusable-blocks/index.js 2.84 kB -273 B (-9%)
build/media-utils/index.js 4.95 kB -361 B (-7%)
build/notices/index.js 1.61 kB -252 B (-14%) 👏
build/nux/index.js 3.07 kB -353 B (-10%) 👏
build/plugins/index.js 2.23 kB -311 B (-12%) 👏
build/primitives/index.js 1.21 kB -217 B (-15%) 👏
build/priority-queue/index.js 636 B -153 B (-19%) 👏
build/redux-routine/index.js 2.68 kB -161 B (-6%)
build/reusable-blocks/index.js 2.68 kB -240 B (-8%)
build/rich-text/index.js 13 kB -475 B (-4%)
build/server-side-render/index.js 2.44 kB -328 B (-12%) 👏
build/shortcode/index.js 1.56 kB -141 B (-8%)
build/token-list/index.js 1.1 kB -169 B (-13%) 👏
build/url/index.js 2.73 kB -298 B (-10%) 👏
build/viewport/index.js 1.57 kB -286 B (-15%) 👏
build/warning/index.js 363 B -774 B (-68%) 🏆
build/wordcount/index.js 1.07 kB -156 B (-13%) 👏
ℹ️ View Unchanged
Filename Size Change
build/block-directory/style-rtl.css 943 B 0 B
build/block-directory/style.css 942 B 0 B
build/block-editor/style-rtl.css 11.3 kB 0 B
build/block-editor/style.css 11.2 kB 0 B
build/block-library/blocks/archives/editor-rtl.css 120 B 0 B
build/block-library/blocks/archives/editor.css 119 B 0 B
build/block-library/blocks/audio/editor-rtl.css 118 B 0 B
build/block-library/blocks/audio/editor.css 118 B 0 B
build/block-library/blocks/audio/style-rtl.css 152 B 0 B
build/block-library/blocks/audio/style.css 152 B 0 B
build/block-library/blocks/block/editor-rtl.css 211 B 0 B
build/block-library/blocks/block/editor.css 211 B 0 B
build/block-library/blocks/button/editor-rtl.css 513 B 0 B
build/block-library/blocks/button/editor.css 513 B 0 B
build/block-library/blocks/button/style-rtl.css 488 B 0 B
build/block-library/blocks/button/style.css 488 B 0 B
build/block-library/blocks/buttons/editor-rtl.css 275 B 0 B
build/block-library/blocks/buttons/editor.css 275 B 0 B
build/block-library/blocks/buttons/style-rtl.css 346 B 0 B
build/block-library/blocks/buttons/style.css 346 B 0 B
build/block-library/blocks/calendar/style-rtl.css 249 B 0 B
build/block-library/blocks/calendar/style.css 249 B 0 B
build/block-library/blocks/categories/editor-rtl.css 135 B 0 B
build/block-library/blocks/categories/editor.css 135 B 0 B
build/block-library/blocks/categories/style-rtl.css 132 B 0 B
build/block-library/blocks/categories/style.css 132 B 0 B
build/block-library/blocks/code/style-rtl.css 141 B 0 B
build/block-library/blocks/code/style.css 141 B 0 B
build/block-library/blocks/columns/editor-rtl.css 239 B 0 B
build/block-library/blocks/columns/editor.css 239 B 0 B
build/block-library/blocks/columns/style-rtl.css 467 B 0 B
build/block-library/blocks/columns/style.css 466 B 0 B
build/block-library/blocks/cover/editor-rtl.css 440 B 0 B
build/block-library/blocks/cover/editor.css 438 B 0 B
build/block-library/blocks/cover/style-rtl.css 1.27 kB 0 B
build/block-library/blocks/cover/style.css 1.27 kB 0 B
build/block-library/blocks/embed/editor-rtl.css 529 B 0 B
build/block-library/blocks/embed/editor.css 529 B 0 B
build/block-library/blocks/embed/style-rtl.css 419 B 0 B
build/block-library/blocks/embed/style.css 419 B 0 B
build/block-library/blocks/file/editor-rtl.css 246 B 0 B
build/block-library/blocks/file/editor.css 245 B 0 B
build/block-library/blocks/file/style-rtl.css 288 B 0 B
build/block-library/blocks/file/style.css 289 B 0 B
build/block-library/blocks/freeform/editor-rtl.css 2.49 kB 0 B
build/block-library/blocks/freeform/editor.css 2.49 kB 0 B
build/block-library/blocks/gallery/editor-rtl.css 692 B 0 B
build/block-library/blocks/gallery/editor.css 693 B 0 B
build/block-library/blocks/gallery/style-rtl.css 1.11 kB 0 B
build/block-library/blocks/gallery/style.css 1.11 kB 0 B
build/block-library/blocks/group/editor-rtl.css 364 B 0 B
build/block-library/blocks/group/editor.css 364 B 0 B
build/block-library/blocks/group/style-rtl.css 117 B 0 B
build/block-library/blocks/group/style.css 117 B 0 B
build/block-library/blocks/heading/editor-rtl.css 174 B 0 B
build/block-library/blocks/heading/editor.css 174 B 0 B
build/block-library/blocks/heading/style-rtl.css 137 B 0 B
build/block-library/blocks/heading/style.css 137 B 0 B
build/block-library/blocks/html/editor-rtl.css 322 B 0 B
build/block-library/blocks/html/editor.css 324 B 0 B
build/block-library/blocks/image/editor-rtl.css 738 B 0 B
build/block-library/blocks/image/editor.css 737 B 0 B
build/block-library/blocks/image/style-rtl.css 510 B 0 B
build/block-library/blocks/image/style.css 511 B 0 B
build/block-library/blocks/latest-comments/editor-rtl.css 201 B 0 B
build/block-library/blocks/latest-comments/editor.css 200 B 0 B
build/block-library/blocks/latest-comments/style-rtl.css 315 B 0 B
build/block-library/blocks/latest-comments/style.css 315 B 0 B
build/block-library/blocks/latest-posts/editor-rtl.css 183 B 0 B
build/block-library/blocks/latest-posts/editor.css 183 B 0 B
build/block-library/blocks/latest-posts/style-rtl.css 568 B 0 B
build/block-library/blocks/latest-posts/style.css 567 B 0 B
build/block-library/blocks/list/editor-rtl.css 129 B 0 B
build/block-library/blocks/list/editor.css 129 B 0 B
build/block-library/blocks/list/style-rtl.css 127 B 0 B
build/block-library/blocks/list/style.css 127 B 0 B
build/block-library/blocks/media-text/editor-rtl.css 240 B 0 B
build/block-library/blocks/media-text/editor.css 240 B 0 B
build/block-library/blocks/media-text/style-rtl.css 579 B 0 B
build/block-library/blocks/media-text/style.css 577 B 0 B
build/block-library/blocks/more/editor-rtl.css 479 B 0 B
build/block-library/blocks/more/editor.css 479 B 0 B
build/block-library/blocks/navigation-link/editor-rtl.css 438 B 0 B
build/block-library/blocks/navigation-link/editor.css 440 B 0 B
build/block-library/blocks/navigation-link/style-rtl.css 747 B 0 B
build/block-library/blocks/navigation-link/style.css 745 B 0 B
build/block-library/blocks/navigation/editor-rtl.css 1.31 kB 0 B
build/block-library/blocks/navigation/editor.css 1.31 kB 0 B
build/block-library/blocks/navigation/style-rtl.css 205 B 0 B
build/block-library/blocks/navigation/style.css 205 B 0 B
build/block-library/blocks/nextpage/editor-rtl.css 440 B 0 B
build/block-library/blocks/nextpage/editor.css 440 B 0 B
build/block-library/blocks/paragraph/editor-rtl.css 161 B 0 B
build/block-library/blocks/paragraph/editor.css 161 B 0 B
build/block-library/blocks/paragraph/style-rtl.css 279 B 0 B
build/block-library/blocks/paragraph/style.css 279 B 0 B
build/block-library/blocks/post-author/editor-rtl.css 255 B 0 B
build/block-library/blocks/post-author/editor.css 255 B 0 B
build/block-library/blocks/post-author/style-rtl.css 229 B 0 B
build/block-library/blocks/post-author/style.css 230 B 0 B
build/block-library/blocks/post-comments-form/style-rtl.css 293 B 0 B
build/block-library/blocks/post-comments-form/style.css 293 B 0 B
build/block-library/blocks/post-content/editor-rtl.css 187 B 0 B
build/block-library/blocks/post-content/editor.css 187 B 0 B
build/block-library/blocks/post-excerpt/editor-rtl.css 134 B 0 B
build/block-library/blocks/post-excerpt/editor.css 134 B 0 B
build/block-library/blocks/post-featured-image/editor-rtl.css 387 B 0 B
build/block-library/blocks/post-featured-image/editor.css 386 B 0 B
build/block-library/blocks/post-featured-image/style-rtl.css 149 B 0 B
build/block-library/blocks/post-featured-image/style.css 149 B 0 B
build/block-library/blocks/preformatted/style-rtl.css 119 B 0 B
build/block-library/blocks/preformatted/style.css 119 B 0 B
build/block-library/blocks/pullquote/editor-rtl.css 231 B 0 B
build/block-library/blocks/pullquote/editor.css 231 B 0 B
build/block-library/blocks/pullquote/style-rtl.css 359 B 0 B
build/block-library/blocks/pullquote/style.css 359 B 0 B
build/block-library/blocks/query-loop/editor-rtl.css 142 B 0 B
build/block-library/blocks/query-loop/editor.css 141 B 0 B
build/block-library/blocks/query-loop/style-rtl.css 361 B 0 B
build/block-library/blocks/query-loop/style.css 363 B 0 B
build/block-library/blocks/query/editor-rtl.css 210 B 0 B
build/block-library/blocks/query/editor.css 210 B 0 B
build/block-library/blocks/quote/editor-rtl.css 121 B 0 B
build/block-library/blocks/quote/editor.css 121 B 0 B
build/block-library/blocks/quote/style-rtl.css 215 B 0 B
build/block-library/blocks/quote/style.css 214 B 0 B
build/block-library/blocks/rss/editor-rtl.css 245 B 0 B
build/block-library/blocks/rss/editor.css 246 B 0 B
build/block-library/blocks/rss/style-rtl.css 329 B 0 B
build/block-library/blocks/rss/style.css 328 B 0 B
build/block-library/blocks/search/editor-rtl.css 213 B 0 B
build/block-library/blocks/search/editor.css 213 B 0 B
build/block-library/blocks/search/style-rtl.css 384 B 0 B
build/block-library/blocks/search/style.css 386 B 0 B
build/block-library/blocks/separator/editor-rtl.css 151 B 0 B
build/block-library/blocks/separator/editor.css 151 B 0 B
build/block-library/blocks/separator/style-rtl.css 281 B 0 B
build/block-library/blocks/separator/style.css 281 B 0 B
build/block-library/blocks/shortcode/editor-rtl.css 547 B 0 B
build/block-library/blocks/shortcode/editor.css 547 B 0 B
build/block-library/blocks/site-logo/editor-rtl.css 251 B 0 B
build/block-library/blocks/site-logo/editor.css 251 B 0 B
build/block-library/blocks/site-logo/style-rtl.css 166 B 0 B
build/block-library/blocks/site-logo/style.css 166 B 0 B
build/block-library/blocks/social-link/editor-rtl.css 211 B 0 B
build/block-library/blocks/social-link/editor.css 211 B 0 B
build/block-library/blocks/social-links/editor-rtl.css 749 B 0 B
build/block-library/blocks/social-links/editor.css 749 B 0 B
build/block-library/blocks/social-links/style-rtl.css 1.37 kB 0 B
build/block-library/blocks/social-links/style.css 1.37 kB 0 B
build/block-library/blocks/spacer/editor-rtl.css 321 B 0 B
build/block-library/blocks/spacer/editor.css 321 B 0 B
build/block-library/blocks/spacer/style-rtl.css 107 B 0 B
build/block-library/blocks/spacer/style.css 107 B 0 B
build/block-library/blocks/subhead/editor-rtl.css 148 B 0 B
build/block-library/blocks/subhead/editor.css 148 B 0 B
build/block-library/blocks/subhead/style-rtl.css 134 B 0 B
build/block-library/blocks/subhead/style.css 134 B 0 B
build/block-library/blocks/table/editor-rtl.css 530 B 0 B
build/block-library/blocks/table/editor.css 530 B 0 B
build/block-library/blocks/table/style-rtl.css 433 B 0 B
build/block-library/blocks/table/style.css 433 B 0 B
build/block-library/blocks/tag-cloud/editor-rtl.css 162 B 0 B
build/block-library/blocks/tag-cloud/editor.css 162 B 0 B
build/block-library/blocks/tag-cloud/style-rtl.css 145 B 0 B
build/block-library/blocks/tag-cloud/style.css 145 B 0 B
build/block-library/blocks/template-part/editor-rtl.css 644 B 0 B
build/block-library/blocks/template-part/editor.css 645 B 0 B
build/block-library/blocks/text-columns/editor-rtl.css 146 B 0 B
build/block-library/blocks/text-columns/editor.css 146 B 0 B
build/block-library/blocks/text-columns/style-rtl.css 209 B 0 B
build/block-library/blocks/text-columns/style.css 209 B 0 B
build/block-library/blocks/verse/editor-rtl.css 118 B 0 B
build/block-library/blocks/verse/editor.css 118 B 0 B
build/block-library/blocks/verse/style-rtl.css 137 B 0 B
build/block-library/blocks/verse/style.css 137 B 0 B
build/block-library/blocks/video/editor-rtl.css 547 B 0 B
build/block-library/blocks/video/editor.css 548 B 0 B
build/block-library/blocks/video/style-rtl.css 241 B 0 B
build/block-library/blocks/video/style.css 241 B 0 B
build/block-library/common-rtl.css 940 B 0 B
build/block-library/common.css 937 B 0 B
build/block-library/editor-rtl.css 8.87 kB 0 B
build/block-library/editor.css 8.87 kB 0 B
build/block-library/style-rtl.css 8.48 kB 0 B
build/block-library/style.css 8.48 kB 0 B
build/block-library/theme-rtl.css 789 B 0 B
build/block-library/theme.css 790 B 0 B
build/components/style-rtl.css 15.4 kB 0 B
build/components/style.css 15.4 kB 0 B
build/edit-navigation/style-rtl.css 881 B 0 B
build/edit-navigation/style.css 885 B 0 B
build/edit-post/style-rtl.css 6.47 kB 0 B
build/edit-post/style.css 6.45 kB 0 B
build/edit-site/style-rtl.css 3.91 kB 0 B
build/edit-site/style.css 3.91 kB 0 B
build/edit-widgets/style-rtl.css 3.1 kB 0 B
build/edit-widgets/style.css 3.1 kB 0 B
build/editor/editor-styles-rtl.css 476 B 0 B
build/editor/editor-styles.css 478 B 0 B
build/editor/style-rtl.css 3.84 kB 0 B
build/editor/style.css 3.84 kB 0 B
build/format-library/style-rtl.css 547 B 0 B
build/format-library/style.css 548 B 0 B
build/list-reusable-blocks/style-rtl.css 476 B 0 B
build/list-reusable-blocks/style.css 476 B 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B

compressed-size-action

@sgomes
Copy link
Contributor

sgomes commented Oct 22, 2020

Disable Babel runtime ES modules until their imports get fixed

They should now be fixed.

@gziolo gziolo added [Type] Build Tooling Issues or PRs related to build tooling [Tool] WP Scripts /packages/scripts labels Oct 22, 2020
@@ -61,6 +61,7 @@ module.exports = {
],
},
mode,
target: 'browserslist',
Copy link
Member

Choose a reason for hiding this comment

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

I assume we will need to apply the same changes in the shared config in @wordpress/scripts. I can take care of it later, once the build process for Gutenberg is polished.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, at this moment, this doesn't even have the desired effect because webpack/webpack#11754 wasn't released yet. So it's hard to test properly.

Copy link
Member Author

Choose a reason for hiding this comment

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

A month later, target: 'browserslist' now works perfectly after a few more webpack 5 bugfix releases. I also added the target config to the shared webpack config in packages/scripts.

package.json Outdated Show resolved Hide resolved
@gziolo
Copy link
Member

gziolo commented Dec 8, 2020

It looks two PRs that are open:

  • Babel upgrade
  • webpack plugin for dependencies

Should bring this PR to a tiny size when you exclude the lock file changes 😃

@jsnajdr
Copy link
Member Author

jsnajdr commented Dec 8, 2020

Should bring this PR to a tiny size when you exclude the lock file changes

That's true, it will be ridiculously simple after these two PRs are merged. Change version in package.json from 4 to 5 and not much more than that 🙂

Building the Gutenberg libraries is fairly straightforward, much simpler than a typical SPA with async-loaded chunks and many other optimizations.

@gziolo
Copy link
Member

gziolo commented Jan 3, 2021

It looks like Storybook doesn't work at all when webpack 5 is installed:

storybookjs/storybook#9216

@jsnajdr
Copy link
Member Author

jsnajdr commented Jan 4, 2021

Thanks @gziolo for finishing and merging this upgrade 🎉

It looks like Storybook doesn't work at all when webpack 5 is installed:

Does that mean that Storybook is currently broken in Gutenberg?

@gziolo
Copy link
Member

gziolo commented Jan 4, 2021

Does that mean that Storybook is currently broken in Gutenberg?

Yes, it is broken. See: https://github.com/WordPress/gutenberg/runs/1640682536

Screen Shot 2021-01-04 at 09 19 36

As a temporary solution, we need to find a creative way to run Storybook with webpack 4 ...

@gziolo gziolo modified the milestones: @wordpress/scripts v13, Gutenberg 9.8 Jan 4, 2021
@gziolo
Copy link
Member

gziolo commented Jan 4, 2021

thread-loader and webpack-livereload-plugin don't quite work with @wordpress/scripts. mini-css-extract-plugin works differently as well. I'm trying to fix all that in another PR, related commit:
4269f31

@gziolo
Copy link
Member

gziolo commented Jan 4, 2021

wp-scripts start errors with the scaffolded block. Steps to reproduce:

$ npx wp-create-block --no-wp-scripts
$ cd esnext-example 
$ ../node_modules/.bin/wp-scripts start
(node:72132) [DEP_WEBPACK_WATCH_WITHOUT_CALLBACK] DeprecationWarning: A 'callback' argument need to be provided to the 'webpack(options, callback)' function when the 'watch' option is set. There is no way to handle the 'watch' option without a callback.
(Use `node --trace-deprecation ...` to show where the warning was created)
Live Reload listening on port 35729

[webpack-cli] Compilation starting...
[webpack-cli] Uncaught exception: Error: MainTemplate.hooks.startup has been removed (use RuntimeGlobals.startup instead)
[webpack-cli] Error: MainTemplate.hooks.startup has been removed (use RuntimeGlobals.startup instead)
    at Object.tap (/Users/gziolo/Projects/gutenberg/node_modules/webpack/lib/MainTemplate.js:100:12)
    at LiveReloadPlugin.applyCompilation (/Users/gziolo/Projects/gutenberg/node_modules/webpack-livereload-plugin/index.js:164:42)
    at Hook.eval [as call] (eval at create (/Users/gziolo/Projects/gutenberg/node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:104:1)
    at Hook.CALL_DELEGATE [as _call] (/Users/gziolo/Projects/gutenberg/node_modules/webpack/node_modules/tapable/lib/Hook.js:14:14)
    at Compiler.newCompilation (/Users/gziolo/Projects/gutenberg/node_modules/webpack/lib/Compiler.js:943:26)
    at /Users/gziolo/Projects/gutenberg/node_modules/webpack/lib/Compiler.js:984:29
    at Hook.eval [as callAsync] (eval at create (/Users/gziolo/Projects/gutenberg/node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/gziolo/Projects/gutenberg/node_modules/webpack/node_modules/tapable/lib/Hook.js:18:14)
    at Compiler.compile (/Users/gziolo/Projects/gutenberg/node_modules/webpack/lib/Compiler.js:979:28)
    at /Users/gziolo/Projects/gutenberg/node_modules/webpack/lib/Watching.js:112:19

gziolo added a commit that referenced this pull request Jan 4, 2021
@gziolo
Copy link
Member

gziolo commented Jan 4, 2021

I opened #27974 where I plan to revert these changes. It looks like we need to wait another couple of weeks until Storybook and all webpack plugins in use have good support for v5.

gziolo added a commit that referenced this pull request Jan 4, 2021
* Revert "Upgrade webpack to version 5 (#26382)"

This reverts commit 85eda57.

* Fix the package lock file
@gziolo
Copy link
Member

gziolo commented Jan 4, 2021

It's now reverted. We need to open another PR to track progress. @jsnajdr, major props for the work done so far 🙇🏻

@jsnajdr
Copy link
Member Author

jsnajdr commented Jan 14, 2021

The issue with Storybook is very similar (if not exactly the same) as the one that @scinos encountered in Calypso with mini-css-extract-plugin (reported in webpack-contrib/mini-css-extract-plugin#668). But here we're dealing with terser-webpack-plugin instead.

@storybook/core depends on webpack 4 and has a copy in its private node_modules folder. There's webpack 5 in the root node_modules, so the v4 can't be hoisted there. Both the @storybook/core and the root package depend on terser-webpack-plugin. Both dependencies are compatible, so they share a common copy hoisted into root node_modules.

Now the webpack 4 compilation in @storybook/core requires terser-webpack-plugin, which in turn requires webpack, but in the root node_modules the webpack request resolves to the webpack 5 copy there.

The webpack -> terser-webpack-plugin -> webpack require chain is supposed to be a closed loop where both webpacks are the same module, but in this case they are two different modules.

A possible solution could be something like Yarn's nohoist option, which could prevent terser-webpack-plugin, or any other dependencies of @storybook/*, from hoisting to the top. That creates a kind of "sandbox" environment for Storybook, not affected by the root node_modules folder. But that works only for Yarn, and also only for workspaces, not arbitrary packages in dependencies.

Another option is to manually patch the package-lock.json lockfile to install a copy of terser-webpack-plugin inside @storybook/core. But that's very fragile and will break on the very next npm install run.

@sirreal
Copy link
Member

sirreal commented Jan 28, 2021

I want to surface another issue I know @jsnajdr was already aware of. Webpack 5 compiled sources may not be compatible with the Plugin Repository i18n string extraction. Apparently webpack 5 outputs module function calls of the form (0, mod.exp)(params) whereas with webpack 4 they had the form Object(mod.exp)(params) (I'm paraphrasing @jsnajdr in another conversation).

The webpack 5 form does not seem to be properly extracted (e.g. (0,wpI18n.__)('translated string')), which seems like a blocker for updating this.

cc: @swissspidy

@swissspidy
Copy link
Member

@sirreal Sounds like we should add support for it in WP-CLI soon then!

Can you confirm that this is the correct output for webpack 5?

@simison
Copy link
Member

simison commented Jan 28, 2021

See wp-cli/i18n-command#228

@sirreal
Copy link
Member

sirreal commented Jan 28, 2021

@sirreal Sounds like we should add support for it in WP-CLI soon then!

Can you confirm that this is the correct output for webpack 5?

Yes, I looked at the compiled plugin from the commit that landed this PR: 85eda57

It shows this type of usage. Here's a sample:

(0,xe.__)("No tags.")
(0,xe.__)("Post tags block: Tags are not available for this post type.")
(0,xe.__)("Post tags block: No post found for this block.")
…

@swissspidy
Copy link
Member

Ah, looks like it's a Babel thing ten, not webpack specific

@jsnajdr
Copy link
Member Author

jsnajdr commented Jan 28, 2021

@swissspidy The (0,xe.__)('No tags.') calls are created by webpack, when replacing usages of import-ed bindings with its own plumbing code.

In webpack 4, they used Object(xe.__)('No tags.'). The purpose of both these tricks is to unbind the this pointer of the __ function from being xe and reset this to either global or undefined.

@gziolo
Copy link
Member

gziolo commented Mar 15, 2021

Webpack 5 support for Storybook is still behind the flag but at least there is some progress:
https://storybook.js.org/blog/storybook-for-webpack-5/

We’re excited to announce experimental Webpack 5 support in Storybook, the world’s most popular UI component workshop.

@gziolo
Copy link
Member

gziolo commented Apr 9, 2021

#30347 should unblock webpack 5 upgrade, @jsnajdr are you up for giving it another try?

@jsnajdr
Copy link
Member Author

jsnajdr commented Apr 9, 2021

are you up for giving it another try?

OK 🙂 Hopefully in the next two weeks -- I'm a bit overwhelmed these days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Tool] WP Scripts /packages/scripts [Type] Breaking Change For PRs that introduce a change that will break existing functionality [Type] Build Tooling Issues or PRs related to build tooling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Build Tooling: Configure Webpack to skip Node polyfills
6 participants