ivanhofer/typesafe-i18n

optimize output for production bundles

ivanhofer opened this issue · 0 comments

If you use keyed arguments inside your translations e.g. HI: Hello {name}!, the function to translate that message will also need to pass an object with the property name in it. It sure is more readable than passing index-based arguments, but it needs more characters. Depending of the amount and the length of the argument-property it will have a negative inpact on the generated bundle-size.

We could automatically rewrite arguments for production

if you have a translation like this:

export const en = {
   HI: 'Hello {name}!'
   // other translations
}

and use it inside your code like:

const message = LL.HI({ name: 'John' })

it could be optimize to the following code:

export const en = {
   HI: 'Hello {0}!'
   // other translations
}

const message = LL.HI('John')

Also the keys itself can be minified e.g. from 'HI' to 'a'

const message = LL.a('John')

What needs to be done:

  • write a rollup-plugin (using AST transforms)
    • rewrite all translations to use index-based arguments
    • rewrite all keys to use a shorter version (or an array with indices)
    • parse all translation calls and rewrite keyed arguments to index-based arguments