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

Add analyze-bundles script #21827

Merged
merged 5 commits into from
May 6, 2020
Merged

Add analyze-bundles script #21827

merged 5 commits into from
May 6, 2020

Conversation

sgomes
Copy link
Contributor

@sgomes sgomes commented Apr 23, 2020

This makes it easier to analyse bundles with webpack-bundle-analyzer, which is already included as a project dependency.

This generates colourful and helpful graphics like this one:

image

Description

Add a new script, analyze-bundles, as well as the instrumentation required for it in webpack.config.js.

How has this been tested?

This should not affect any code, only the build process, which appears to still work normally when using any other script.

Test by running npm run analyze-bundles.

Types of changes

New feature involving build changes only.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code has proper inline documentation.

@sgomes sgomes added [Type] Build Tooling Issues or PRs related to build tooling [Type] Performance Related to performance efforts labels Apr 23, 2020
@sgomes sgomes requested review from gziolo and aduth April 23, 2020 14:25
@sgomes sgomes force-pushed the add/analyze-bundles-script branch from a30380f to 6b99217 Compare April 23, 2020 14:28
@github-actions
Copy link

github-actions bot commented Apr 23, 2020

Size Change: +4.98 kB (0%)

Total Size: 821 kB

Filename Size Change
build/api-fetch/index.js 4.08 kB -2 B (0%)
build/autop/index.js 2.82 kB +2 B (0%)
build/block-directory/index.js 6.6 kB +376 B (5%) 🔍
build/block-editor/index.js 101 kB -4.81 kB (4%)
build/block-editor/style-rtl.css 10.2 kB +16 B (0%)
build/block-editor/style.css 10.2 kB +16 B (0%)
build/block-library/editor-rtl.css 7.08 kB +55 B (0%)
build/block-library/editor.css 7.08 kB +58 B (0%)
build/block-library/index.js 115 kB +1.28 kB (1%)
build/block-library/style-rtl.css 7.24 kB +100 B (1%)
build/block-library/style.css 7.25 kB +107 B (1%)
build/block-serialization-spec-parser/index.js 3.1 kB +1 B
build/blocks/index.js 48.1 kB -3 B (0%)
build/components/index.js 179 kB +11 B (0%)
build/core-data/index.js 11.4 kB -13 B (0%)
build/data/index.js 8.44 kB +6 B (0%)
build/date/index.js 5.47 kB -1 B
build/edit-navigation/index.js 4.05 kB +513 B (12%) ⚠️
build/edit-post/index.js 28.1 kB +541 B (1%)
build/edit-post/style-rtl.css 12.2 kB +29 B (0%)
build/edit-post/style.css 12.2 kB +30 B (0%)
build/edit-site/index.js 12.3 kB +1.37 kB (11%) ⚠️
build/edit-site/style-rtl.css 5.19 kB +88 B (1%)
build/edit-site/style.css 5.2 kB +90 B (1%)
build/edit-widgets/index.js 8.37 kB +875 B (10%) ⚠️
build/edit-widgets/style-rtl.css 4.68 kB +16 B (0%)
build/edit-widgets/style.css 4.68 kB +17 B (0%)
build/editor/index.js 44.3 kB +703 B (1%)
build/editor/style-rtl.css 5.07 kB +1.8 kB (35%) 🚨
build/editor/style.css 5.08 kB +1.81 kB (35%) 🚨
build/escape-html/index.js 734 B +1 B
build/format-library/index.js 7.63 kB +2 B (0%)
build/keyboard-shortcuts/index.js 2.51 kB +2 B (0%)
build/keycodes/index.js 1.94 kB +1 B
build/list-reusable-blocks/index.js 3.13 kB +1 B
build/media-utils/index.js 5.29 kB -1 B
build/nux/index.js 3.4 kB +1 B
build/plugins/index.js 2.56 kB -114 B (4%)
build/primitives/index.js 1.5 kB +2 B (0%)
build/redux-routine/index.js 2.85 kB +1 B
build/rich-text/index.js 14.8 kB +3 B (0%)
build/server-side-render/index.js 2.67 kB -7 B (0%)
build/url/index.js 4.02 kB -2 B (0%)
build/viewport/index.js 1.84 kB -1 B
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/annotations/index.js 3.62 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 761 B 0 B
build/block-library/theme-rtl.css 683 B 0 B
build/block-library/theme.css 685 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/components/style-rtl.css 16.9 kB 0 B
build/components/style.css 16.9 kB 0 B
build/compose/index.js 6.66 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.1 kB 0 B
build/edit-navigation/style-rtl.css 485 B 0 B
build/edit-navigation/style.css 485 B 0 B
build/editor/editor-styles-rtl.css 428 B 0 B
build/editor/editor-styles.css 431 B 0 B
build/element/index.js 4.65 kB 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/priority-queue/index.js 789 B 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.18 kB 0 B

compressed-size-action

@gziolo
Copy link
Member

gziolo commented Apr 24, 2020

@youknowriad or @aduth – I think we had it before but it was removed for some reason. Does it ring a bell?

@aduth
Copy link
Member

aduth commented Apr 24, 2020

@youknowriad or @aduth – I think we had it before but it was removed for some reason. Does it ring a bell?

I do vaguely recall something like this a very long time ago. My impression is that it's not much of a concern if it's totally optional. Maybe it was removed if it wasn't providing much value and was causing some trouble? I'll see if I can dig it up.

@aduth
Copy link
Member

aduth commented Apr 24, 2020

There was #10185. It was moved from the root webpack.config.js to the shared default configuration in @wordpress/scripts.

In fact, it still exists!

// WP_BUNDLE_ANALYZER global variable enables utility that represents bundle content
// as convenient interactive zoomable treemap.
process.env.WP_BUNDLE_ANALYZER && new BundleAnalyzerPlugin(),

But if I recall (because I debated it 😄), we moved away from reusing this shared configuration in Gutenberg, so we probably don't have it available currently via the environment variable.

I think if we're to reintroduce it, we should find some way which reuses the configuration from @wordpress/scripts, which should be fine to do since the build is run through this package anyways.

@sgomes
Copy link
Contributor Author

sgomes commented Apr 24, 2020

I think if we're to reintroduce it, we should find some way which reuses the configuration from @wordpress/scripts, which should be fine to do since the build is run through this package anyways.

As far as I can tell, the webpack.config.js file in packages/scripts/config/ isn't being used for the main build at all, however. At least throwing an error at the top level there doesn't seem to impact the build process in any way 🤔

@aduth
Copy link
Member

aduth commented Apr 24, 2020

As far as I can tell, the webpack.config.js file in packages/scripts/config/ isn't being used for the main build at all, however. At least throwing an error at the top level there doesn't seem to impact the build process in any way 🤔

Yes, that's largely what I was expecting in referring to "we moved away from reusing this shared configuration in Gutenberg". There was some legitimate case for Gutenberg having very particular needs that motivated the change to a completely custom configuration, though it was still disappointing that we couldn't reconcile our own needs with a configuration we were otherwise touting as being a "common" configuration.

What I mean by "find some way which reuses", in other cases we've promoted to merge some or all of the configuration.

Example: https://github.com/WordPress/gutenberg/blob/master/packages/scripts/README.md#extending-the-webpack-config

It would be nice if we could do something like that here, in order to avoid duplicating this configuration. (Aside: It's also duplicated in a way which isn't consistent, e.g. there is no optimization configuration in packages/scripts/config/webpack.config.js). It's hard to say if it would be possible, since merging config.plugins is pretty risky and also not easy to "pick" desired merge values.

My recommendation would be:

  • Make a best effort to see if there are options to merge values from the common configuration.
  • Otherwise, it would be good to make the duplication as consistent as possible.

This makes it easier to analyze bundles with webpack-bundle-analyzer,
which is already included as a project dependency.
@sgomes
Copy link
Contributor Author

sgomes commented Apr 29, 2020

My recommendation would be:

  • Make a best effort to see if there are options to merge values from the common configuration.
  • Otherwise, it would be good to make the duplication as consistent as possible.

Thanks, @aduth!

I gave this a go, and I couldn't find any great options for merging configuration.

The biggest issue there is the plugins, as you pointed out. Since it's an array of instanced plugin objects, you lose all information regarding what's in there. So your only options become to either replace the plugins array entirely, or to accept everything in it and just append your own plugins at the end.

If plugins could be an object instead (which it can't; I tried), that would neatly solve the issue by allowing us to name specific plugins to find and replace them if needed.

The other option would be to export a plugins object separately, in something like a named export, but that's not supported in CommonJS.

As such, all I can do is make things a bit more consistent between both webpack configs :-/

@sgomes sgomes force-pushed the add/analyze-bundles-script branch from 99fac3b to 601a6ef Compare April 29, 2020 16:13
Follows the default behaviour of webpack
Comment on lines +22 to +26
optimization: {
// Only concatenate modules in production, when not analyzing bundles.
concatenateModules:
mode === 'production' && ! process.env.WP_BUNDLE_ANALYZER,
},
Copy link
Member

Choose a reason for hiding this comment

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

Is there a relevant entry we should mention in scripts/CHANGELOG.md ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added an entry 👍

@sgomes sgomes merged commit ba5b8be into master May 6, 2020
@sgomes sgomes deleted the add/analyze-bundles-script branch May 6, 2020 08:50
@github-actions github-actions bot added this to the Gutenberg 8.1 milestone May 6, 2020
@@ -3,6 +3,7 @@
### Breaking Changes

- The bundled `puppeteer` (`^2.0.0`) dependency has been replaced with `puppeteer-core` in version `3.0.0`. Puppeteer uses Chromium v81 instead of Chromium v79. See the [full list of changes](https://github.com/puppeteer/puppeteer/releases/tag/v3.0.0). It also allowed preventing Chromium installation together with `@wordpress/scripts`. It happens now on-demand when running `test-e2e` script, and it re-triggers only when a new version is required.
- Bundle analysis now runs with module concatenation disabled. This represents the size of individual modules more accurately, at the cost of not providing an exact byte-for-byte match to the final size in the production chunk.
Copy link
Member

@gziolo gziolo May 6, 2020

Choose a reason for hiding this comment

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

I think you merged this change without rebasing. It ended up in the section that was already published to npm:

https://github.com/WordPress/gutenberg/blob/master/packages/scripts/CHANGELOG.md#900-2020-04-30

Also, it doesn't look like a breaking change, or is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, I did do that, yes. Whether it's a breaking change or not depends on interpretation; if we consider the bundle analysis to be part of the scripts package "user contract", then this is a breaking change, otherwise it's not.

Shall I prepare a PR moving this to the right section and making it a non-breaking change?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it would be great 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Build Tooling Issues or PRs related to build tooling [Type] Performance Related to performance efforts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants