Skip to content

Slimming down Javascript bundle#1390

Merged
mattolson merged 18 commits intobigcommerce:masterfrom
mattolson:STRF-4965
Jan 10, 2019
Merged

Slimming down Javascript bundle#1390
mattolson merged 18 commits intobigcommerce:masterfrom
mattolson:STRF-4965

Conversation

@mattolson
Copy link
Copy Markdown
Contributor

@mattolson mattolson commented Dec 1, 2018

What?

Responding to code review on #1229 that was done post-merge

Based on recommendations from @davidchin in #1229 (comment) :

  • Move stencil-cli and grunt-cli to devDependencies
  • Use npx to run tests using local instead of global packages
  • Add Node 8.x (can't support 10 yet because stencil-cli does not)

Based on #1229 (comment) :

  • Change function passed to jQuery each so reference to this is correct

Based on #1229 (comment) :

  • Add webpack performance warnings for large assets and chunks

Based on #1229 (comment) :

  • Enable source maps in production

Update to babel 7

  • Remove babel-polyfill and instead rely on useBuiltIns: 'usage'
  • Use @babel/preset-env
  • Set targets
  • Add webpack-bundle-analyzer to help us visualize webpack bundles
  • Remove transform-regenerator plugin
  • Bump versions to latest for all babel related packages
  • Use minimized jquery from distribution
  • Use splitChunks: all to separate node_modules into a separate file
    to avoid re-downloading it again when minor changes are made to app.js

Reduce usage of sweetAlert and compareProducts

  • Remove sweetalert2 from global bundle
  • Create Foundation reveal modal to be used for error messages
  • Replace usage of sweetalert2 with alert modal in every place
    except cart, shipping estimator, and account details, which
    have more complex use cases
  • Change global/sweet-alert to return the swal instance so we
    can call it anywhere to get the initialized instance, rather than
    doing this in jquery onReady
  • Bump sweetalert to latest 6.x
  • Remove compareProducts from global bundle and add to category,
    brand, compare, and search pages.

Remove async/await

  • Removal of async/await results in bundle savings because webpack
    doesn't need to include a shim

Switch from pace to nanobar

  • Pace is an unmaintained, large library that we use for the loading progress indicator. Replace with nanobar and jQuery ajax hooks.

Impact

The bundle size for the entrypoint has been reduced from 376kb to 286kb.

Before

[matt.olson@C02X425EJG5M:~/workspace/cornerstone] $ URL=http://matt-olson.mybigcommerce.com npm run lighthouse

> bigcommerce-cornerstone@2.6.0 lighthouse /Users/matt.olson/workspace/cornerstone
> npx lighthouse --output json --chrome-flags="--headless" --quiet --disable-device-emulation --throttling-method=provided $URL | jq '{ttfb: .audits | .["time-to-first-byte"] | .rawValue, paint: .audits | .["first-meaningful-paint"] | .rawValue, interactive: .audits.interactive.rawValue}'

{
  "ttfb": 277.21999999999997,
  "paint": 702.078,
  "interactive": 845.0290001220703
}

After

[matt.olson@C02X425EJG5M:~/workspace/cornerstone] $ URL=http://matt-olson.mybigcommerce.com npm run lighthouse

> bigcommerce-cornerstone@2.6.0 lighthouse /Users/matt.olson/workspace/cornerstone
> npx lighthouse --output json --chrome-flags="--headless" --quiet --disable-device-emulation --throttling-method=provided $URL | jq '{ttfb: .audits | .["time-to-first-byte"] | .rawValue, paint: .audits | .["first-meaningful-paint"] | .rawValue, interactive: .audits.interactive.rawValue}'

{
  "ttfb": 245.334,
  "paint": 523.496,
  "interactive": 618.681
}

@bigbot
Copy link
Copy Markdown

bigbot commented Dec 1, 2018

Autotagging @bigcommerce/storefront-team @davidchin

@mattolson mattolson changed the title STRF-4965: Minor modifications to build STRF-4965: Responding to post-merge code review of #1229 Dec 1, 2018
junedkazi
junedkazi previously approved these changes Dec 1, 2018
@mattolson mattolson dismissed junedkazi’s stale review December 7, 2018 01:41

More commits added since review

@mattolson mattolson changed the title STRF-4965: Responding to post-merge code review of #1229 Babel 7 + Webpack updates => slimmed down bundle Dec 7, 2018
Comment thread .travis.yml
text: tmp.textContent || tmp.innerText,
type: 'error',
});
return showAlertModal(tmp.textContent || tmp.innerText);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you please add a before and after for this change

Comment thread package.json Outdated
Comment thread package.json
@mattolson mattolson changed the title Babel 7 + Webpack updates => slimmed down bundle Slimming down Javascript bundle Dec 18, 2018
davidwrpayne
davidwrpayne previously approved these changes Dec 20, 2018
Copy link
Copy Markdown

@davidwrpayne davidwrpayne left a comment

Choose a reason for hiding this comment

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

Looks fine to me. Pretty awesome results.

@mattolson
Copy link
Copy Markdown
Contributor Author

@Ubersmake This is good to go from my perspective. Can you help with some additional testing? It's a pretty broad change, so extra eyes will be helpful.

Comment thread webpack.common.js Outdated
@steve-ross
Copy link
Copy Markdown
Contributor

FYI if you use _.get() you need to pass the following options to the lodash plugin {paths: true}

Thanks for this PR it helped me with optimizing our build on a custom theme.

* Move stencil-cli and grunt-cli to devDependencies
* Use npx to run tests using local instead of global packages
In browsers that don't support objectfit, the current implementation
is broken because reference to `this` is different when using arrow
functions vs not.
* Update to babel 7
* Remove babel-polyfill and instead rely on useBuiltIns: 'usage'
* Use @babel/preset-env
* Set targets
* Add webpack-bundle-analyzer to help us visualize webpack bundles
* Remove transform-regenerator plugin
* Bump versions to latest for all babel related packages
* Use minimized jquery from distribution
* Use splitChunks: all to separate node_modules into a separate file
  to avoid re-downloading it again when minor changes are made to app.js
* Remove sweetalert2 from global bundle
* Create Foundation reveal modal to be used for error messages
* Replace usage of sweetalert2 with alert modal in every place
  except cart, shipping estimator, and account details, which
  have more complex use cases
* Change global/sweet-alert to return the swal instance so we
  can call it anywhere to get the initialized instance, rather than
  doing this in jquery onReady
* Bump sweetalert to latest 6.x
* Remove compareProducts from global bundle and add to category,
  brand, compare, and search pages.
* Removal of async/await results in bundle savings because webpack
  doesn't need to include a shim
* Remove explicit imports and instead rely on ProvidePlugin
  to load jQuery
* Bump webpack to latest
* Add noEmitOnErrors: true to production build
* We only need a small subset of jquery-migrate (to support Foundation
5.5), so pull that in directly rather than the whole library
* Webpack was fetching a secondary file for page-specific js even
  though there is nothing useful for several of our pages. This
  changes the import to a noop to avoid the secondary fetch.
* Bring in babel/polyfill because the docs say to. This doesn't appear
  to have any effect on the bundle.
* Use babel's dynamic import instead of an old plugin
* Need to bring in more from jquery-migrate to support Foundation 5.5
* Fix interface for ./globals/sweet-alert so it returns the actual
  sweetAlert object
* We now have two implementations of the alert modal. Sweet Alert
  is used on cart, shipping estimator, and account details because
  they have more advanced use cases. Everywhere else, we use a
  Foundation Reveal modal.
* This commit updates the styling of the new alert modal to more closely
  match the Sweet Alert modal.
@mattolson mattolson dismissed davidwrpayne’s stale review January 10, 2019 18:39

More commits since review

Comment thread templates/components/common/body.html
Copy link
Copy Markdown

@davidwrpayne davidwrpayne left a comment

Choose a reason for hiding this comment

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

All looks fine to me from a non Js background.

@mattolson mattolson merged commit 637ef1b into bigcommerce:master Jan 10, 2019
@mattolson mattolson deleted the STRF-4965 branch January 10, 2019 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants