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 ES6 builds to libraries #3900

Merged
merged 3 commits into from
Dec 1, 2022
Merged

Add ES6 builds to libraries #3900

merged 3 commits into from
Dec 1, 2022

Conversation

eatyourgreens
Copy link
Contributor

@eatyourgreens eatyourgreens commented Nov 18, 2022

Add ES6 library builds with Babel:

  • Enable the React automatic runtime for the classifier and Zooniverse React Components.
  • Fix @babel/runtime in classifier builds.
  • Add an ES6 build to @zooniverse/react-components. Export dist/es6/index.js as a module.
  • Add an ES6 build to @zooniverse/classifier. Export dist/es6/components/Classifier/index.js as a module.
  • Add yarn bootstrap:es6 to bootstrap the monorepo with ES modules.
  • Use i18n.createInstance() to set up i18n for each library. Give each instance its own useTranslation hook and withTranslation HOC.
  • Use the ES6 build scripts for CI builds and for deploys. This might give us faster builds and smaller images.

Remove redundant packages from the builds:

  • Replace mime-types with mime/lite.
  • Remove @fortawesome/react-fontawesome and @fortawesome/fontawesome-svg-core.
  • Remove import * as d3.
  • Remove import _ from lodash.
  • Replace superagent with fetch in the classifier.

I've left the Webpack UMD module builds in for the Mocha tests, and for backwards compatibility with any third-party projects which require in @zooniverse/react-components. If we can figure out #3779 then I'm fairly sure we can remove the webpack builds and use pure ES modules for the libraries.

With the changes here, I'm seeing the JS download on a typical project Classify page drop from ~1.7MB to ~800k. NextJS reports the project page build size as ~490k (Classify pages are ~800k), which is still too large but much better than what we've currently got in production.

Lots of files are touched by this PR, but many of them are one-line changes to the imports from i18next packages.

Package

app-project
lib-classifier
lib-react-components

How to Review

There are a few options. Pick the one you're most comfortable with:

I've run a branch deploy of 504229f so that we can test the Docker image on kubernetes.

Here are some test projects:

Checklist

PR Creator - Please cater the checklist to fit the review needed for your code changes.
PR Reviewer - Use the checklist during your review. Each point should be checkmarked or discussed before PR approval.

General

  • Tests are passing locally and on Github
  • Documentation is up to date and changelog has been updated if appropriate
  • You can yarn panic && yarn bootstrap or docker-compose up --build and FEM works as expected
  • FEM works in all major desktop browsers: Firefox, Chrome, Edge, Safari (Use Browserstack account as needed)
  • FEM works in a mobile browser

General UX

Example Staging Project: i-fancy-cats

  • All pages of a FEM project load: Home Page, Classify Page, and About Pages
  • Can submit a classification
  • Can sign-in and sign-out
  • The component is accessible

Refactoring

  • The PR creator has described the reason for refactoring
  • The refactored component(s) continue to work as expected

@eatyourgreens eatyourgreens added the ops Changes that affect how something is built or deployed label Nov 18, 2022
@coveralls
Copy link

coveralls commented Nov 18, 2022

Coverage Status

Coverage decreased (-0.3%) to 82.922% when pulling 0a1036f on es6-builds into fb6a5dc on master.

@eatyourgreens
Copy link
Contributor Author

eatyourgreens commented Nov 18, 2022

Tests are passing and the apps are building, so that looks good.

Dynamic imports seem to be broken in the app bundles. In the project app:

  • dynamically imported translation text is missing from the page header and footer. (Fixed, I think, by using import instead of require.)
  • the Classifier component isn't dynamically loaded into the Classify page. (Fixed by adding the classifier's JSON language files to the build.)

The content pages app looks good, but it doesn't use internationalisation.

EDIT: I forgot to include translations JSON files in the classifier build. Fixed now, and the classifier seems to load.

@eatyourgreens eatyourgreens marked this pull request as draft November 18, 2022 19:59
@eatyourgreens eatyourgreens marked this pull request as ready for review November 18, 2022 20:51
@eatyourgreens eatyourgreens requested a review from a team November 18, 2022 20:51
@eatyourgreens eatyourgreens force-pushed the es6-builds branch 2 times, most recently from 9c3892b to a062247 Compare November 18, 2022 22:01
@eatyourgreens eatyourgreens force-pushed the es6-builds branch 2 times, most recently from a9df545 to e990b71 Compare November 19, 2022 11:50
@eatyourgreens
Copy link
Contributor Author

eatyourgreens commented Nov 19, 2022

Tests are passing and both NextJS apps are building. I've deployed the project app to kubernetes. It loads and runs without errors, but translated labels don't show for components that are loaded in from @zooniverse/react-components.
https://fe-project-branch.preview.zooniverse.org/projects/nora-dot-eisner/planet-hunters-tess/classify/workflow/11235

Screenshot of PH-TESS showing placeholder text for translation keys in the Zooniverse site navigation bar.

EDIT: this was caused by each library calling i18n.init() on the same instance of i18n, which is why the bug only showed up on the Classify page. The classifier replaced the react-components translation strings with its own translation strings. Fixed by giving each library its own instance of i18next.

@eatyourgreens
Copy link
Contributor Author

eatyourgreens commented Nov 19, 2022

The project storybook doesn't show translation strings on this branch, which is odd because this PR doesn't touch project translations.

The story for project recent classifications, showing translation keys instead of text labels.

EDIT: I think this can be fixed by giving each library its own instance of i18next, rather than overwriting the default instance and any translation strings it contains.

@eatyourgreens eatyourgreens force-pushed the es6-builds branch 3 times, most recently from 50b3c66 to 4f8d388 Compare November 20, 2022 23:22
@eatyourgreens
Copy link
Contributor Author

eatyourgreens commented Nov 20, 2022

This is working locally for me now without translations bugs, in production mode. The storybooks are working too. I've run a branch deploy of 8f2ba69 so that we can test the Docker image on kubernetes.

@eatyourgreens
Copy link
Contributor Author

eatyourgreens commented Nov 20, 2022

Still to do: update the changelog for @zooniverse/react-components. This could be released as version 1.3, since it keeps the Webpack build for backwards compatibility with loaders that don't support ESM.

@eatyourgreens
Copy link
Contributor Author

eatyourgreens commented Nov 21, 2022

Also to do: roll #3847 into this branch, to fix @babel/runtime in the classifier.

@eatyourgreens eatyourgreens force-pushed the es6-builds branch 2 times, most recently from 5760db4 to c756197 Compare November 22, 2022 21:28
@mcbouslog mcbouslog mentioned this pull request Nov 22, 2022
5 tasks
@eatyourgreens eatyourgreens force-pushed the es6-builds branch 2 times, most recently from a7bd75c to f065cde Compare November 23, 2022 10:01
@eatyourgreens eatyourgreens force-pushed the es6-builds branch 4 times, most recently from 9c09fba to 504229f Compare November 28, 2022 22:15
@mcbouslog mcbouslog self-assigned this Nov 29, 2022
@mcbouslog mcbouslog requested review from mcbouslog and removed request for a team December 1, 2022 06:18
Copy link
Contributor

@mcbouslog mcbouslog left a comment

Choose a reason for hiding this comment

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

This looks great!!

I tested all the linked fe-project-branch projects, noting changing languages, signing in and out, feedback (positive and negative), d3 related subject viewers - all work as expected. Tested across browsers without issue.

I successfully built and ran app-content-pages and app-project locally. Also ran the storybooks locally; spot-checked a few stories including test translation without issue.

I can sign in/out on the fe-project-branch linked projects.

Looks great!!

@@ -26,7 +26,8 @@ const supportedLngs = [
'zh-tw'
]

i18n.use(initReactI18next).init({
const zrcI18n = i18n.createInstance()
Copy link
Contributor

Choose a reason for hiding this comment

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

This is terribly minor, 100% ok to dismiss, but with the old Zooniverse-React-Components repo should we avoid referencing the new FEM lib-react-components as zrc? So maybe this variable could be reactComponentsI18n or something similar, like how classifierI18n is the related variable in lib-classifier? I realize reactComponents has a larger meaning than our new Zooniverse library of react components though, so I understand if would prefer to keep as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This library is the replacement for the old ZRC library, in new projects, so I think the naming is OK.

Something I discovered yesterday is that the build I added here (straight Babel transform from JSX to JS) is the build script from the old library. I’m not sure why it’s taken so long to copy this across to the new libraries.

https://github.com/zooniverse/Zooniverse-React-Components/blob/49bea57c6089bb99e51f2552b148728f96960705/package.json#L9

Copy link
Contributor Author

Choose a reason for hiding this comment

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

- Enable the React automatic runtime for the classifier and Zooniverse React Components.
- Fix `@babel/runtime` in classifier builds.
- Add an ES6 build to `@zooniverse/react-components`. Export `dist/es6/index.js` as a module.
- Add an ES6 build to `@zooniverse/classifier`. Export `dist/es6/components/Classifier/index.js` as a module.
- Add `yarn bootstrap:es6` to bootstrap the monorepo with ES modules.
- Use `i18n.createInstance()` to set up `i18n` for each library. Give each instance its own `useTranslation` hook and `withTranslation` HOC.
- Replace `mime-types` with `mime/lite`.
- Remove `@fortawesome/react-fontawesome` and `@fortawesome/fontawesome-svg-core`.
- Remove `import * as d3`.
- Remove `import _ from lodash`.
- Replace `superagent` with `fetch` in the classifier.
@eatyourgreens eatyourgreens merged commit 1c87583 into master Dec 1, 2022
@eatyourgreens eatyourgreens deleted the es6-builds branch December 1, 2022 10:11
@eatyourgreens eatyourgreens mentioned this pull request Dec 1, 2022
11 tasks
eatyourgreens added a commit to eatyourgreens/front-end-monorepo that referenced this pull request Jan 8, 2024
A quick ADR to explain the switch from Webpack UMD builds to ESM builds with Babel in zooniverse#3900.
eatyourgreens added a commit to eatyourgreens/front-end-monorepo that referenced this pull request Jan 8, 2024
A quick ADR to explain the switch from Webpack UMD builds to ESM builds with Babel in zooniverse#3900.
eatyourgreens added a commit to eatyourgreens/front-end-monorepo that referenced this pull request Jan 8, 2024
A quick ADR to explain the switch from Webpack UMD builds to ESM builds with Babel in zooniverse#3900.
goplayoutside3 added a commit that referenced this pull request Jan 8, 2024
A quick ADR to explain the switch from Webpack UMD builds to ESM builds with Babel in #3900.

---------

Co-authored-by: Delilah C <23665803+goplayoutside3@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ops Changes that affect how something is built or deployed performance ux
Projects
Development

Successfully merging this pull request may close these issues.

3 participants