-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
@@ -0,0 +1,13 @@ | |||
diff --git a/node_modules/@parcel/bundler-default/lib/DefaultBundler.js b/node_modules/@parcel/bundler-default/lib/DefaultBundler.js |
There was a problem hiding this comment.
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.
Build successful! 🎉 |
1 similar comment
Build successful! 🎉 |
Build successful! 🎉 |
1 similar comment
Build successful! 🎉 |
There was a problem hiding this 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:
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Build successful! 🎉 |
1 similar comment
Build successful! 🎉 |
Build successful! 🎉 |
1 similar comment
Build successful! 🎉 |
.circleci/config.yml
Outdated
filters: | ||
branches: | ||
only: main | ||
# filters: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be uncommented?
Build successful! 🎉 |
1 similar comment
Build successful! 🎉 |
…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
Build successful! 🎉 |
Build successful! 🎉 |
Build successful! 🎉 |
Build successful! 🎉 |
Build successful! 🎉 |
Build successful! 🎉 |
Build successful! 🎉 |
Build successful! 🎉 |
Build successful! 🎉 |
@@ -48,6 +50,204 @@ function App() { | |||
|
|||
See the [internationalization docs](internationalization.html) for more information about i18n in React Aria. | |||
|
There was a problem hiding this comment.
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.
#### 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`: |
There was a problem hiding this comment.
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
Build successful! 🎉 |
Build successful! 🎉 |
## API Changes
unknown top level export { type: 'any' } @internationalized/stringLocalizedStringDictionarychanged by:
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:
@react-aria/i18nuseLocalizedStringFormatter 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
+} |
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.