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

Reduce dist size by 500 KB - reinstate browserslist, removing transforms for ancient browsers #3284

Merged
merged 5 commits into from
Dec 6, 2024

Conversation

bradenmacdonald
Copy link
Contributor

@bradenmacdonald bradenmacdonald commented Nov 13, 2024

Description

Currently, Paragon is built using Babel's preset-env but without specifying any targets nor .browserslistrc config file. This has the unfortunate effect that:

When no targets are specified: Babel will assume you are targeting the oldest browsers possible. For example, @babel/preset-env will transform all ES2015-ES2020 code to be ES5 compatible.

We recognize this isn’t ideal and will be revisiting this in Babel v8.

We recommend setting targets to reduce the output code size.

So if you look at any of the files in dist/, you'll see that they are being transformed in an extremely inefficient way in order to support pre-ES5 browsers (that are more than a decade old!).

In this PR, I am proposing we change the build to use the recommended targets: defaults option. This small change to the config results in a big 500KB savings to the transformed files in dist when npm run build is run:

Before: 2,919,906 bytes (4.5 MB on disk) for 740 items
After: 2,401,170 bytes (4.1 MB on disk) for 740 items

I have also removed @babel/plugin-proposal-class-properties and @babel/plugin-proposal-object-rest-spread because they're both deprecated (click their links for details), and the replacements are now included in preset-env so don't need to be separately installed.

See also #3283 which makes a similar change for the icons.

What about compatibility?

This is now using the browserslist defaults query which is equivalent to > 0.5%, last 2 versions, Firefox ESR, not dead. This is considered best practice. We could change it to @edx/browserslist-config but as far as I can tell that's actually less compatible than the browserslist defaults (because it explicitly specifies only the few browsers supported by Open edX, whereas the browserslist defaults includes old browsers if they have enough marketshare). As an example, they advise "Don’t remove browsers just because you don’t know them. Opera Mini has 100 million users in Africa and it is more popular in the global market than Microsoft Edge. Chinese QQ Browsers has more market share than Firefox and desktop Safari combined." but Open edX browserslist config has removed those browsers.

Example

Here is a typical diff showing the simplification that results in the build. This is dist/Alert/index.js.

@@ -1,20 +1,3 @@
-// dist/Alert/index.js
-function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
-var _excluded = ["children", "icon", "actions", "dismissible", "onClose", "closeLabel", "stacked"];
-function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
-function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
-function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
-function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
-function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
-function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
-function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
-function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
-function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
-function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
-function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
-function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
-function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
-function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
 import React, { useCallback, useEffect, useState } from 'react';
 import PropTypes from 'prop-types';
 import classNames from 'classnames';
@@ -26,42 +9,42 @@
 import breakpoints from '../utils/breakpoints';
 import Button from '../Button';
 import ActionRow from '../ActionRow';
-export var ALERT_CLOSE_LABEL_TEXT = 'Dismiss';
-var Alert = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
-  var children = _ref.children,
-    icon = _ref.icon,
-    actions = _ref.actions,
-    dismissible = _ref.dismissible,
-    onClose = _ref.onClose,
-    closeLabel = _ref.closeLabel,
-    stacked = _ref.stacked,
-    props = _objectWithoutProperties(_ref, _excluded);
-  var _useState = useState(stacked),
-    _useState2 = _slicedToArray(_useState, 2),
-    isStacked = _useState2[0],
-    setIsStacked = _useState2[1];
-  var isExtraSmall = useMediaQuery({
+export const ALERT_CLOSE_LABEL_TEXT = 'Dismiss';
+const Alert = /*#__PURE__*/React.forwardRef((_ref, ref) => {
+  let {
+    children,
+    icon,
+    actions,
+    dismissible,
+    onClose,
+    closeLabel,
+    stacked,
+    ...props
+  } = _ref;
+  const [isStacked, setIsStacked] = useState(stacked);
+  const isExtraSmall = useMediaQuery({
     maxWidth: breakpoints.extraSmall.maxWidth
   });
-  var actionButtonSize = 'sm';
-  useEffect(function () {
+  const actionButtonSize = 'sm';
+  useEffect(() => {
     if (isExtraSmall) {
       setIsStacked(true);
     } else {
       setIsStacked(stacked);
     }
   }, [isExtraSmall, stacked]);
-  var cloneActionElement = useCallback(function (Action) {
-    var addtlActionProps = {
+  const cloneActionElement = useCallback(Action => {
+    const addtlActionProps = {
       size: actionButtonSize,
       key: Action.props.children
     };
     return /*#__PURE__*/React.cloneElement(Action, addtlActionProps);
   }, []);
-  return /*#__PURE__*/React.createElement(BaseAlert, _extends({}, props, {
+  return /*#__PURE__*/React.createElement(BaseAlert, {
+    ...props,
     className: classNames('alert-content', props.className),
     ref: ref
-  }), icon && /*#__PURE__*/React.createElement(Icon, {
+  }, icon && /*#__PURE__*/React.createElement(Icon, {
     src: icon,
     className: "alert-icon"
   }), /*#__PURE__*/React.createElement("div", {
@@ -71,7 +54,7 @@
     })
   }, /*#__PURE__*/React.createElement("div", {
     className: "alert-message-content"
-  }, children), (dismissible || (actions === null || actions === void 0 ? void 0 : actions.length) > 0) && /*#__PURE__*/React.createElement(ActionRow, {
+  }, children), (dismissible || actions?.length > 0) && /*#__PURE__*/React.createElement(ActionRow, {
     className: "pgn__alert-actions"
   }, /*#__PURE__*/React.createElement(ActionRow.Spacer, null), dismissible && /*#__PURE__*/React.createElement(Button, {
     size: actionButtonSize,
@@ -87,15 +70,19 @@
 // This is needed to display a default prop for Alert.Heading element
 // Copied from react-bootstrap since BaseAlert.Heading component doesn't have defaultProps,
 // so there seems to be no other way of providing correct default prop for base element
-var DivStyledAsH4 = divWithClassName('h4');
+const DivStyledAsH4 = divWithClassName('h4');
 DivStyledAsH4.displayName = 'DivStyledAsH4';
 function AlertHeading(props) {
-  return /*#__PURE__*/React.createElement(BaseAlert.Heading, props);
+  return /*#__PURE__*/React.createElement(BaseAlert.Heading, {
+    ...props
+  });
 }
 function AlertLink(props) {
-  return /*#__PURE__*/React.createElement(BaseAlert.Link, props);
+  return /*#__PURE__*/React.createElement(BaseAlert.Link, {
+    ...props
+  });
 }
-var commonPropTypes = {
+const commonPropTypes = {
   /** Specifies the base element */
   as: PropTypes.elementType,
   /** Overrides underlying component base CSS class name */
@@ -111,7 +98,8 @@
   as: DivStyledAsH4,
   bsPrefix: 'alert-heading'
 };
-Alert.propTypes = _objectSpread(_objectSpread({}, BaseAlert.propTypes), {}, {
+Alert.propTypes = {
+  ...BaseAlert.propTypes,
   /** Specifies class name to append to the base element */
   className: PropTypes.string,
   /** Overrides underlying component base CSS class name */
@@ -123,7 +111,7 @@
    * more detailed customization is also provided.
    */
   transition: PropTypes.oneOfType([PropTypes.bool, PropTypes.shape({
-    "in": PropTypes.bool,
+    in: PropTypes.bool,
     appear: PropTypes.bool,
     children: PropTypes.node,
     onEnter: PropTypes.func,
@@ -149,17 +137,18 @@
   stacked: PropTypes.bool,
   /** Sets the text for alert close button, defaults to 'Dismiss'. */
   closeLabel: PropTypes.oneOfType([PropTypes.string, PropTypes.element])
-});
-Alert.defaultProps = _objectSpread(_objectSpread({}, BaseAlert.defaultProps), {}, {
+};
+Alert.defaultProps = {
+  ...BaseAlert.defaultProps,
   children: undefined,
   icon: undefined,
   actions: undefined,
   dismissible: false,
-  onClose: function onClose() {},
+  onClose: () => {},
   closeLabel: undefined,
   show: true,
   stacked: false
-});
+};
 Alert.Heading = AlertHeading;
 Alert.Link = AlertLink;
 export default Alert;

Deploy Preview

https://deploy-preview-3284--paragon-openedx.netlify.app

No changes should be discernable.

Merge Checklist

  • If your update includes visual changes, have they been reviewed by a designer? Send them a link to the Netlify deploy preview, if applicable.
  • Does your change adhere to the documented style conventions?
  • Do any prop types have missing descriptions in the Props API tables in the documentation site (check deploy preview)?
  • Were your changes tested using all available themes (see theme switcher in the header of the deploy preview, under the "Settings" icon)?
  • Were your changes tested in the example app?
  • Is there adequate test coverage for your changes?
  • Consider whether this change needs to reviewed/QA'ed for accessibility (a11y). If so, please request an a11y review for the PR in the #wg-paragon Open edX Slack channel.

Post-merge Checklist

  • Verify your changes were released to NPM at the expected version.
  • If you'd like, share your contribution in #show-and-tell.
  • 🎉 🙌 Celebrate! Thanks for your contribution.

@openedx-webhooks
Copy link

Thanks for the pull request, @bradenmacdonald!

What's next?

Please work through the following steps to get your changes ready for engineering review:

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.

🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads

🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

🔘 Let us know that your PR is ready for review:

Who will review my changes?

This repository is currently maintained by @openedx/paragon-working-group. Tag them in a comment and let them know that your changes are ready for review.

Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Nov 13, 2024
Copy link

netlify bot commented Nov 13, 2024

Deploy Preview for paragon-openedx ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 4f366e7
🔍 Latest deploy log https://app.netlify.com/sites/paragon-openedx/deploys/67537a38cfcfd40008ca1536
😎 Deploy Preview https://deploy-preview-3284--paragon-openedx.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codecov bot commented Nov 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.31%. Comparing base (f26c4f2) to head (4f366e7).
Report is 17 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3284   +/-   ##
=======================================
  Coverage   93.31%   93.31%           
=======================================
  Files         249      249           
  Lines        4441     4441           
  Branches     1014     1051   +37     
=======================================
  Hits         4144     4144           
+ Misses        294      291    -3     
- Partials        3        6    +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bradenmacdonald bradenmacdonald mentioned this pull request Nov 13, 2024
10 tasks
@brian-smith-tcril
Copy link
Contributor

After discussing in Paragon working group today we decided it likely makes the most sense to bring back the https://github.com/openedx/browserslist-config/ dependency and update that repo to follow browserslist best practices. That repo is currently set to past 2 major versions to match what was communicated on the edx.org support page, but it absolutely makes sense to increase the coverage to follow best practices. This will ensure we have the same level of support throughout all applications instead of just in Paragon.

@bradenmacdonald
Copy link
Contributor Author

bradenmacdonald commented Nov 13, 2024

Ah, I wasn't aware that it had been removed at some point: #2501

That issue says

browserslist should only be defined in MFEs or any regular applications that render pages on the website, not in component libraries like Paragon.

But this is only true if we distribute Paragon in a "buildless" manner, as .tsx/.jsx source files. As far as I know, none of our MFEs consume Paragon in that way. Since we do have an npm run build step and we are using Babel to transform Paragon files from .jsx/.tsx to .js, the config does matter, and right now it is using a bad default.

It would also be reasonable to distribute Paragon in the most modern form possible, and rely on MFE bunders (webpack+babel) to apply compatibility transforms/shims later where needed. But distributing it in the most compatible form (supporting ancient browsers) forces all the compatibility shims into each and every file, and as far as I know they can't get removed by later steps of the bundling process (in MFEs).

After discussing in Paragon working group today we decided it likely makes the most sense to bring back the https://github.com/openedx/browserslist-config/ dependency and update that repo to follow browserslist best practices. That repo is currently set to past 2 major versions to match what was communicated on the edx.org support page, but it absolutely makes sense to increase the coverage to follow best practices. This will ensure we have the same level of support throughout all applications instead of just in Paragon.

Sounds like a good plan! Do you want me to update this PR to add that dependency?

@bradenmacdonald bradenmacdonald changed the title Reduce dist size by 500 KB - use modern babel config, removing transforms for ancient browsers Reduce dist size by 500 KB - reinstate browserslist, removing transforms for ancient browsers Nov 13, 2024
@brian-smith-tcril
Copy link
Contributor

Sounds like a good plan! Do you want me to update this PR to add that dependency?

That would be wonderful! Just adding back the dependency won't quite cover everything though, so a PR to https://github.com/openedx/browserslist-config updating the exports to use browserslist defaults would be great too!

@bradenmacdonald
Copy link
Contributor Author

@brian-smith-tcril OK, I've made that change (restored use of @edx/browserslist-config), and this should be ready to go!

In doing so, I had to rebuild all the icon files ("Generated icons do not match committed icons. Please run npm run build in the icons directory.") So that made the diff larger. But I kept those changes isolated to one commit, and it means I won't be bugging you later to review #3283 😛

Separately, and independent of this PR, I opened a PR to expand browserslist-config per upstream recommendations: openedx/browserslist-config#16

Copy link
Contributor

@brian-smith-tcril brian-smith-tcril left a comment

Choose a reason for hiding this comment

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

Thank you so much for this! The only remaining question I have is should we land this first or land openedx/browserslist-config#16 first.

I think landing this first makes sense, and we can do a small browserslist-config version bump PR once openedx/browserslist-config#16 lands.

@adamstankiewicz thoughts?

@adamstankiewicz
Copy link
Member

@brian-smith-tcril Sounds reasonable to me. I've also approved openedx/browserslist-config#16

Thanks for tackling this, @bradenmacdonald 💯

@brian-smith-tcril
Copy link
Contributor

@bradenmacdonald looks like this has conflicts and needs a rebase. Once that's done and ci passes again I'm happy to merge it!

@bradenmacdonald
Copy link
Contributor Author

@brian-smith-tcril Rebased!

@brian-smith-tcril brian-smith-tcril merged commit 59a3634 into openedx:master Dec 6, 2024
10 checks passed
@openedx-semantic-release-bot

🎉 This PR is included in version 22.11.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@bradenmacdonald bradenmacdonald deleted the modern-babel-config branch December 7, 2024 17:07
@openedx-semantic-release-bot

🎉 This PR is included in version 23.0.0-alpha.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants