Skip to content

Update diff to 4.0.2 and work around tree-shaking issues#21994

Merged
sgomes merged 2 commits intomasterfrom
update/diff-4.0.2
May 4, 2020
Merged

Update diff to 4.0.2 and work around tree-shaking issues#21994
sgomes merged 2 commits intomasterfrom
update/diff-4.0.2

Conversation

@sgomes
Copy link
Copy Markdown
Contributor

@sgomes sgomes commented Apr 30, 2020

Description

Update the diff dependency to 4.0.2 or later, as the 3.x.x version range is no longer getting any updates.

The PR also works around tree-shaking issues in the library, which results in a substantial improvement in bundle size. Bumping the version will also allow for the Gutenboarding project in Calypso to not require its own copy of diff.

How has this been tested?

Unit tests and ad-hoc testing of this functionality. To test:

  • Create e.g. a paragraph block,
  • Edit it as HTML,
  • Modify the HTML tags to something invalid,
  • Click "Resolve" when the error dialog is displayed,
  • Verify that a diff of changes is shown, and that it matches what is shown for a similar sequence of steps before this change.

Types of changes

Dependency update only; no code changes.

Checklist:

  • My code is tested.

@sgomes sgomes added the [Package] Block editor /packages/block-editor label Apr 30, 2020
@sgomes sgomes requested review from aduth and gziolo April 30, 2020 12:48
@sgomes
Copy link
Copy Markdown
Contributor Author

sgomes commented Apr 30, 2020

An added note: I checked the notes for the release, and it doesn't look like there were any breaking changes that concern us on the move from 3 to 4.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 30, 2020

Size Change: -5.87 kB (0%)

Total Size: 819 kB

Filename Size Change
build/annotations/index.js 3.62 kB -1 B
build/api-fetch/index.js 4.08 kB -1 B
build/block-editor/index.js 101 kB -5.86 kB (5%)
build/block-library/index.js 115 kB -1 B
build/components/index.js 179 kB -1 B
build/core-data/index.js 11.4 kB -13 B (0%)
build/data-controls/index.js 1.28 kB -2 B (0%)
build/data/index.js 8.44 kB -2 B (0%)
build/date/index.js 5.47 kB +2 B (0%)
build/edit-navigation/index.js 4.05 kB +1 B
build/edit-post/index.js 28.1 kB -3 B (0%)
build/edit-widgets/index.js 7.77 kB -1 B
build/element/index.js 4.65 kB +1 B
build/format-library/index.js 7.64 kB +2 B (0%)
build/hooks/index.js 2.13 kB +1 B
build/is-shallow-equal/index.js 712 B +2 B (0%)
build/media-utils/index.js 5.29 kB -1 B
build/notices/index.js 1.79 kB +1 B
build/plugins/index.js 2.56 kB -1 B
build/primitives/index.js 1.5 kB -1 B
build/shortcode/index.js 1.7 kB +1 B
build/token-list/index.js 1.28 kB +1 B
build/url/index.js 4.02 kB +1 B
build/warning/index.js 1.14 kB -1 B
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 6.6 kB 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 761 B 0 B
build/block-editor/style-rtl.css 10.2 kB 0 B
build/block-editor/style.css 10.2 kB 0 B
build/block-library/editor-rtl.css 7.08 kB 0 B
build/block-library/editor.css 7.08 kB 0 B
build/block-library/style-rtl.css 7.22 kB 0 B
build/block-library/style.css 7.23 kB 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/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 48.1 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/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/edit-post/style-rtl.css 12.2 kB 0 B
build/edit-post/style.css 12.2 kB 0 B
build/edit-site/index.js 11.4 kB 0 B
build/edit-site/style-rtl.css 5.18 kB 0 B
build/edit-site/style.css 5.18 kB 0 B
build/edit-widgets/style-rtl.css 4.67 kB 0 B
build/edit-widgets/style.css 4.66 kB 0 B
build/editor/editor-styles-rtl.css 428 B 0 B
build/editor/editor-styles.css 431 B 0 B
build/editor/index.js 44.3 kB 0 B
build/editor/style-rtl.css 5.07 kB 0 B
build/editor/style.css 5.08 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/keyboard-shortcuts/index.js 2.51 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.13 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/nux/index.js 3.4 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/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 14.8 kB 0 B
build/server-side-render/index.js 2.67 kB 0 B
build/viewport/index.js 1.84 kB 0 B
build/wordcount/index.js 1.18 kB 0 B

compressed-size-action

@aduth
Copy link
Copy Markdown
Member

aduth commented Apr 30, 2020

An added note: I checked the notes for the release, and it doesn't look like there were any breaking changes that concern us on the move from 3 to 4.

Reference: https://github.com/kpdecker/jsdiff/blob/master/release-notes.md#v400---january-5th-2019

@aduth
Copy link
Copy Markdown
Member

aduth commented Apr 30, 2020

The new version is also slightly smaller and tree-shakeable

According to Bundlephobia, because it doesn't declare sideEffects: false, we're not able to take as much advantage of exports as we would otherwise. I wonder for these cases if we can forcibly override this for packages we assume to be safely without side-effects, but which don't declare so themselves. I see at webpack/webpack#6065 (comment) and by corresponding documentation we might be able to do this in Webpack. My naive attempt at this doesn't seem to make any difference, though it's unclear if I've made a mistake.

diff --git a/webpack.config.js b/webpack.config.js
index 340728cfe1..b2a8f962ee 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -5,7 +5,7 @@ const { DefinePlugin } = require( 'webpack' );
 const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
 const postcss = require( 'postcss' );
 const { get, escapeRegExp, compact } = require( 'lodash' );
-const { basename, sep } = require( 'path' );
+const { basename, sep, resolve } = require( 'path' );
 
 /**
  * WordPress dependencies
@@ -54,6 +54,10 @@ module.exports = {
 	},
 	module: {
 		rules: compact( [
+			{
+				include: resolve( 'node_modules', 'diff' ),
+				sideEffects: false,
+			},
 			mode !== 'production' && {
 				test: /\.js$/,
 				use: require.resolve( 'source-map-loader' ),

With or without this change, I see 390kb for block-editor chunk. Still better than it is on master (395kb).

@sgomes
Copy link
Copy Markdown
Contributor Author

sgomes commented May 4, 2020

According to Bundlephobia, because it doesn't declare sideEffects: false, we're not able to take as much advantage of exports as we would otherwise.

Thanks for looking into this, @aduth!

It appears that the library has some more fundamental issues than just a missing sideEffects declaration. As far as I can tell, its index files aren't correctly generated for tree-shaking, with lib/index.es6.js (the module file) being a gigantic single export, and lib/index.js (the main file) possibly not being in the correct format for webpack to grok, even with sideEffects: false (I manually edited its package.json to be sure).

Here are some experiments, with sideEffects: false:

import { diffChars } from 'diff' -> 16.47KB / 6.44KB gzipped
import { diffChars } from 'diff/lib/index.js' -> 19.57KB / 6.12KB gzipped
import { diffChars } from 'diff/lib/diff/character' -> 2.64KB / 1.16KB gzipped

The latter doesn't need sideEffects: false, so I've switched to it in the PR, which means we get some nice savings 🙂

@sgomes sgomes force-pushed the update/diff-4.0.2 branch from 3c92b25 to 67a893f Compare May 4, 2020 09:55
@sgomes
Copy link
Copy Markdown
Contributor Author

sgomes commented May 4, 2020

Rebased on master to see if the github-actions size results make more sense. I assume that it's comparing vs master rather than comparing vs the branching point of the PR?

Edit: Looking at the updated results, that does appear to be the case. They make a lot more sense now.

@sgomes
Copy link
Copy Markdown
Contributor Author

sgomes commented May 4, 2020

I'm not sure why the Build artifacts check is complaining that there are uncommitted changes; neither npm install nor npm run docs:build seem to produce any changed files for me.

@sgomes sgomes changed the title Update diff to 4.0.2 Update diff to 4.0.2 and work around tree-shaking issues May 4, 2020
@aduth
Copy link
Copy Markdown
Member

aduth commented May 4, 2020

I'm not sure why the Build artifacts check is complaining that there are uncommitted changes; neither npm install nor npm run docs:build seem to produce any changed files for me.

I can see local changes, but only after first removing node_modules.

diff --git a/package-lock.json b/package-lock.json
index 172654b86d..5905049454 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -31146,9 +31146,9 @@
 			}
 		},
 		"mkdirp-classic": {
-			"version": "0.5.2",
-			"resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.2.tgz",
-			"integrity": "sha512-ejdnDQcR75gwknmMw/tx02AuRs8jCtqFoFqDZMjiNxsu85sRIJVXDKHuLYvUUPRBUtV2FpSZa9bL1BUa3BdR2g==",
+			"version": "0.5.3",
+			"resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz",
+			"integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==",
 			"dev": true
 		},
 		"mkdirp-promise": {

@youknowriad
Copy link
Copy Markdown
Contributor

@aduth I had the same issue, I commited this change to master

sgomes added 2 commits May 4, 2020 14:18
The 3.x.x version range is no longer getting any updates.
@sgomes sgomes force-pushed the update/diff-4.0.2 branch from 67a893f to 5353322 Compare May 4, 2020 13:18
@sgomes
Copy link
Copy Markdown
Contributor Author

sgomes commented May 4, 2020

Thanks, @youknowriad! I rebased on master again to see if it picks up your changes.

@sgomes
Copy link
Copy Markdown
Contributor Author

sgomes commented May 4, 2020

Everything looks green now 👍

Since I'm working around tree-shaking issues as well now, I'm re-requesting a review in case you want to take another look, @aduth

@sgomes sgomes requested a review from aduth May 4, 2020 14:01
Copy link
Copy Markdown
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Still appears to work well in testing 👍

@sgomes
Copy link
Copy Markdown
Contributor Author

sgomes commented May 4, 2020

Thank you for the reviews, @aduth! 👍

@sgomes sgomes merged commit 9db5982 into master May 4, 2020
@sgomes sgomes deleted the update/diff-4.0.2 branch May 4, 2020 15:06
@github-actions github-actions Bot added this to the Gutenberg 8.1 milestone May 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Block editor /packages/block-editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants