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

Framework: Substitute crypto polyfill dependants with smaller stand-ins #17356

Merged
merged 1 commit into from
Aug 30, 2017

Conversation

aduth
Copy link
Contributor

@aduth aduth commented Aug 18, 2017

A few of our dependencies, namely component-uid and js-sha1, themselves have dependencies on crypto:

https://github.com/emn178/js-sha1/blob/c724643/src/sha1.js#L52-L53
https://github.com/component/uid/blob/2d912b8/index.js#L45 (It appears they try to avoid the polyfill, but it is included in a Webpack build)

crypto is a Node built-in module which is polyfilled in the browser by crypto-browserify. This polyfill is quite substantial in size.

The changes here seek to find suitable replacements for each: uuid (Lodash's uniqueId) and hash.js respectively, which are very reasonable in size.

Aside: I first suspected uuid to also require crypto, but it turns out they bundle a browser-specific variant without the dependency.

The difference is pretty noticeable: 472kb smaller minified (112kb gzipped) in the main build.js.

Before:

wp-calypso (master) $ wc -c public/build.b1e9a12a27f881b1406e.js
 7190661 public/build.b1e9a12a27f881b1406e.js
wp-calypso (master) $ gzip-size public/build.b1e9a12a27f881b1406e.js --raw
1275631

After:

wp-calypso (master) $ wc -c public/build.10d51aa5c5ffef307acf.js
 6718680 public/build.10d51aa5c5ffef307acf.js
wp-calypso (master) $ gzip-size public/build.10d51aa5c5ffef307acf.js --raw
1163240

Testing instructions:

Verify against regressions for the two modules affected:

  • Sync handler (used for caching response data on the posts list)
  • Popover (tooltips, ellipsis menu, etc)

Ensure unit tests pass:

npm test

@aduth aduth added Framework [Type] Performance [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Aug 18, 2017
@aduth aduth requested review from blowery, retrofox and gwwar August 18, 2017 22:03
@matticbot
Copy link
Contributor

@matticbot matticbot added the [Size] XL Probably needs to be broken down into multiple smaller issues label Aug 18, 2017
@aduth aduth requested a review from aidvu August 18, 2017 22:04
@gwwar
Copy link
Contributor

gwwar commented Aug 19, 2017

I'm not sure if it's related but devdocs/blocks isn't loading for me on this branch.

@aidvu
Copy link
Contributor

aidvu commented Aug 19, 2017

@gwwar It was fixed: 925af92.

@@ -361,7 +361,7 @@ class Popover extends Component {
}

setPopoverId( id ) {
this.id = id || `pop__${ uid( 16 ) }`;
this.id = id || `pop__${ uuid() }`;
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is almost unneeded. If we think that it's better to get rid of it let's do it. I could do it in a next PR as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A static incrementing number could work just as well, yeah. (Or nothing at all)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, lodash/uniqueId seems better (and simpler) for this case. It actually guarantees that it will be unique, while uuid can't.

@@ -1,11 +1,10 @@
/**
* External dependencies
*/
import sha1 from 'js-sha1';
import sha1 from 'hash.js/lib/hash/sha/1';
Copy link
Contributor

Choose a reason for hiding this comment

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

in fact, we could get rid of sync-handler as well. In this way, we could clean this module completely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in fact, we could get rid of sync-handler as well. In this way, we could clean this module completely.

Yeah, I figured sync handler is on its way out. Most of the pieces are already there with the newer custom post type screen (i.e. caching post responses for future use). I think the remaining issue is whether we need to preserve existing UIs (especially with newer features like re-Publicize) and how we extend CPT components to support those cases. Regardless, it's a bigger issue than one I want to address here, and I think it'll be good to keep the sync handler around until a full replacement can be made.

@matticbot
Copy link
Contributor

@aduth This PR needs a rebase

@blowery
Copy link
Contributor

blowery commented Aug 22, 2017

The difference is pretty noticeable: 472kb smaller minified (112kb gzipped)

😲

@matticbot
Copy link
Contributor

@aduth This PR needs a rebase

@alisterscott
Copy link
Contributor

I did some testing of this and couldn't find any regressions to areas mentioned 👍

@aduth aduth force-pushed the remove/crypto-dependency branch from 1558328 to 5dde395 Compare August 30, 2017 16:19
@aduth
Copy link
Contributor Author

aduth commented Aug 30, 2017

Rebased and replaced the Popover's uuid with Lodash uniqueId, regenerated shrinkwrap once more, did a final round of testing, and will merge shortly.

@aduth aduth added [Status] Ready to Merge and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Aug 30, 2017
@gwwar
Copy link
Contributor

gwwar commented Aug 30, 2017

@aduth @tyxla heads up on overlapping changes in #17560

Copy link
Contributor

@gwwar gwwar left a comment

Choose a reason for hiding this comment

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

Thanks @aduth this tests well for me

@aduth aduth merged commit c4f6c73 into master Aug 30, 2017
@aduth aduth deleted the remove/crypto-dependency branch August 30, 2017 17:38
@aduth
Copy link
Contributor Author

aduth commented Aug 30, 2017

Thanks for catching the overlap @gwwar

@tyxla
Copy link
Member

tyxla commented Aug 30, 2017

Thanks @aduth @gwwar!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Framework [Size] XL Probably needs to be broken down into multiple smaller issues [Type] Performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants