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 build plugins to optimize translated strings for unsupported languages #4838

Merged
merged 29 commits into from
Dec 2, 2023

Conversation

devongovett
Copy link
Member

@devongovett devongovett commented Jul 28, 2023

Fixes #3794

This adds build plugins for popular build tools including Parcel, webpack, esbuild, vite, etc. to optimize React Aria and React Spectrum so that only translated strings for languages that your app supports are included in your bundle. We support over 30 languages, but not all apps need all of these, so they can add unnecessary bundle size.

I explored many different ways of allowing developers to manually import the localized strings for languages they support, but this was just too complicated. Ensuring that strings are imported in the right places along side every import of a component (so code splitting works), and not imported too early (e.g. all components in the root of the app) would be very complicated and brittle. In addition, our components often depend on other components or hooks with their own translations internally, and developers would also need to import those.

This approach is much simpler. We move all of our compiled translations to separate files in each published package's dist directory, but still import all of them. With no other changes, the behavior would remain the same - all locales are included by default. However, since they are in separate files, we can provide build plugins to automatically detect and remove files for languages that aren't supported by an app.

The RAC tailwind example is ~23% smaller when it includes only en-US, and the rsp-next-ts example is 20% smaller.

See #5084 for followup work that was included with this to allow including only the language requested during SSR.

@@ -0,0 +1,13 @@
diff --git a/node_modules/@parcel/bundler-default/lib/DefaultBundler.js b/node_modules/@parcel/bundler-default/lib/DefaultBundler.js
Copy link
Member Author

Choose a reason for hiding this comment

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

These patches will go away once parcel-bundler/parcel#9156 is merged and released.

@rspbot
Copy link

rspbot commented Jul 28, 2023

1 similar comment
@rspbot
Copy link

rspbot commented Jul 28, 2023

@rspbot
Copy link

rspbot commented Sep 15, 2023

1 similar comment
@rspbot
Copy link

rspbot commented Sep 15, 2023

Copy link
Member

@LFDanLu LFDanLu left a comment

Choose a reason for hiding this comment

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

The approach looks good to me and I verified that the nextJS size dropped as a result of this:

new: https://reactspectrum.blob.core.windows.net/reactspectrum/2a7211d8cb686fe810cb4838c67802dce3aa8eab/verdaccio/publish-stats/next-build-stats.txt

old: https://reactspectrum.blob.core.windows.net/reactspectrum/6855a5328b9349f6b4f8bdb45cbde960586543f5/verdaccio/publish-stats/next-build-stats.txt

will approve after #5084 is merged in and the circle config is reverted. Might be nice to run verdaccio with the SSR injection happening as well just to verify the work done

Copy link
Member

@snowystinger snowystinger left a comment

Choose a reason for hiding this comment

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

lgtm

@rspbot
Copy link

rspbot commented Oct 19, 2023

1 similar comment
@rspbot
Copy link

rspbot commented Oct 19, 2023

@rspbot
Copy link

rspbot commented Oct 19, 2023

1 similar comment
@rspbot
Copy link

rspbot commented Oct 19, 2023

filters:
branches:
only: main
# filters:
Copy link
Member

Choose a reason for hiding this comment

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

this should be uncommented?

@rspbot
Copy link

rspbot commented Oct 26, 2023

1 similar comment
@rspbot
Copy link

rspbot commented Oct 26, 2023

…locales

# Conflicts:
#	packages/@adobe/react-spectrum/package.json
#	packages/@react-aria/datepicker/src/useDateRangePicker.ts
#	packages/@react-spectrum/button/src/Button.tsx
#	packages/@react-spectrum/picker/src/Picker.tsx
#	packages/react-aria-components/package.json
#	packages/react-aria/package.json
#	patches/@parcel+packager-js+2.10.2.patch
#	yarn.lock
@rspbot
Copy link

rspbot commented Nov 27, 2023

@rspbot
Copy link

rspbot commented Nov 29, 2023

@rspbot
Copy link

rspbot commented Nov 29, 2023

@rspbot
Copy link

rspbot commented Nov 29, 2023

@rspbot
Copy link

rspbot commented Nov 29, 2023

@rspbot
Copy link

rspbot commented Nov 29, 2023

@rspbot
Copy link

rspbot commented Nov 29, 2023

@rspbot
Copy link

rspbot commented Nov 29, 2023

@rspbot
Copy link

rspbot commented Nov 30, 2023

@@ -48,6 +50,204 @@ function App() {

See the [internationalization docs](internationalization.html) for more information about i18n in React Aria.

Copy link
Member

Choose a reason for hiding this comment

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

I think a note mentioning this caveat near the beginning of the "Optimizing bundle size" would be helpful section.

Comment on lines +162 to +164
#### Remix

Remix is supported [when using Vite](https://remix.run/docs/en/main/future/vite) for builds. First, install `@react-aria/optimize-locales-plugin` with your package manager. Then, add the following to your `vite.config.ts`:
Copy link
Member

Choose a reason for hiding this comment

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

Mind adding a note here that LocalizedStringProvider isn't used here? Wasn't sure if the first two steps at the beginning of the "optimizing bundle size" were implicitly included here or not

snowystinger
snowystinger previously approved these changes Nov 30, 2023
@rspbot
Copy link

rspbot commented Dec 2, 2023

@rspbot
Copy link

rspbot commented Dec 2, 2023

@rspbot
Copy link

rspbot commented Dec 2, 2023

## API Changes

unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any', access: 'private' }
unknown top level export { type: 'any', access: 'private' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'identifier', name: 'Column' }
unknown top level export { type: 'identifier', name: 'Column' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }

@internationalized/string

LocalizedStringDictionary

changed by:

  • LocalizedStringDictionary
 LocalizedStringDictionary<K extends string = string, T extends LocalizedString = string> {
   constructor: (LocalizedStrings<string, LocalizedString>, string) => void
+  getGlobalDictionaryForPackage: (string) => LocalizedStringDictionary<string, LocalizedString> | null
   getStringForLocale: (string, string) => LocalizedString
+  getStringsForLocale: (string) => Record<string, LocalizedString>
 }

it changed:

  • LocalizedStringDictionary

@react-aria/i18n

useLocalizedStringFormatter

 useLocalizedStringFormatter<K extends string = string, T extends LocalizedString = string> {
   strings: LocalizedStrings<K, T>
+  packageName?: string
   returnVal: undefined
 }

useLocalizedStringDictionary

-
+useLocalizedStringDictionary<K extends string = string, T extends LocalizedString = string> {
+  strings: LocalizedStrings<K, T>
+  packageName?: string
+  returnVal: undefined
+}

@devongovett devongovett merged commit ee403ec into main Dec 2, 2023
26 checks passed
@devongovett devongovett deleted the optimize-locales branch December 2, 2023 00:32
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.

Externalize localization strings
4 participants