Reduce size of Webpack i18n plugin template boilerplate#9180
Conversation
changelog: Internal, Performance, Reduce size of translation JavaScript files
allthesignals
left a comment
There was a problem hiding this comment.
Seems reasonable to me. Any implications for running this in a node environment?
Not sure if you had specific concerns in mind, but |
More thinking about the |
|
Never one to miss a code golf opportunity, I found some small savings in 0db75fe by avoiding the initial Object.assign({ foo: 'bar' }, undefined);
// { foo: 'bar' } |
|
Alright, I think I've squeezed as much as I'm going to get out of this after 2f93d03. I also toyed with an idea to use spread operator, which was shorter uncompressed template, but (a) may have marginally worse performance due to doing a shallow clone of both objects rather than direct (mutative) merge, and (b) it compressed worse than the current implementation (91 bytes vs. 87 bytes brotli'd for Diff, for reference: diff --git a/app/javascript/packages/rails-i18n-webpack-plugin/rails-i18n-webpack-plugin.js b/app/javascript/packages/rails-i18n-webpack-plugin/rails-i18n-webpack-plugin.js
index 520f48ab7..09c9ddbee 100644
--- a/app/javascript/packages/rails-i18n-webpack-plugin/rails-i18n-webpack-plugin.js
+++ b/app/javascript/packages/rails-i18n-webpack-plugin/rails-i18n-webpack-plugin.js
@@ -114,5 +114,5 @@ class RailsI18nWebpackPlugin extends ExtractKeysWebpackPlugin {
/** @type {RailsI18nWebpackPluginOptions} */
static DEFAULT_OPTIONS = {
- template: '_locale_data=Object.assign(%j,this._locale_data)',
+ template: '_locale_data={...this._locale_data,...%j}',
configPath: path.resolve(process.cwd(), 'config/locales'),
defaultLocale: 'en', |
|
And another idea which works, is shorter uncompressed, but compresses worse (91 bytes for diff --git a/app/javascript/packages/rails-i18n-webpack-plugin/rails-i18n-webpack-plugin.js b/app/javascript/packages/rails-i18n-webpack-plugin/rails-i18n-webpack-plugin.js
index 520f48ab7..111876c2e 100644
--- a/app/javascript/packages/rails-i18n-webpack-plugin/rails-i18n-webpack-plugin.js
+++ b/app/javascript/packages/rails-i18n-webpack-plugin/rails-i18n-webpack-plugin.js
@@ -114,5 +114,5 @@ class RailsI18nWebpackPlugin extends ExtractKeysWebpackPlugin {
/** @type {RailsI18nWebpackPluginOptions} */
static DEFAULT_OPTIONS = {
- template: '_locale_data=Object.assign(%j,this._locale_data)',
+ template: 'Object.assign(this._locale_data??={},%j)',
configPath: path.resolve(process.cwd(), 'config/locales'),
defaultLocale: 'en', |
🛠 Summary of changes
Updates the Webpack i18n template to reduce its size. The original implementation was meant as largely equivalent to
Object.assign, but compatible with older browsers which didn't support it. Now that our browser support is such thatObject.assignshould always be available, we can compact the size of the template.Since this is a boilerplate "header" of sorts, the impact is bigger for smaller translation files. Bigger files like document capture won't benefit as much.
Example:
Small file
Before: 130 bytes
After: 87 bytes
Diff: -43 bytes (-33.1%)
Medium file:
Before: 952 bytes
After: 927 bytes
Diff: -25 bytes (-2.63%)
Large file:
Before: 3.02kb
After: 2.98kb
Diff: -0.4kb (-1.32%)
📜 Testing Plan
Verify no regression in behavior of JavaScript localization.