@eartharoid/vite-plugin-i18n
v1.0.0-alpha.7
Published
A Vite plugin for I18n and CIF
Downloads
41
Readme
I18n Vite Plugin
This plugin converts your raw message JSON files into a new JSON object containing cif
data and the locale_id
,
which then gets turned into an ESM module by the internal vite:json
plugin.
The advantages of CIF are that it is smaller and slightly faster than JSON, however whilst making this:
- I realised vite converts JSON files into ESM modules, so there's no
JSON.parse()
needed at runtime. - I found that after applying gzip/brotli compression, the size difference becomes insignificant.
- I made major optimisations to message loading/parsing to the point that even converting to I18n JSON rather than CIF offers minor benefits, over just importing a JSON file.
[!TIP] Should I use this?
- Where you want to minimise the build size (such as for browser extensions) or most of your messages are used immediately, yes.
- For websites served by a CDN that supports zip/brotli compression where most messages are not used immediately upon page load, ensuring that imported JSON files are converted to ESM modules and keeping deferred extraction enabled (both are defaults) is the most optimal.
Refer to these comparisons for more information:
Installation
npm install @eartharoid/vite-plugin-i18n
// vite.config.ts
import { I18nPlugin } from '@eartharoid/vite-plugin-i18n';
export default {
plugins: [
I18nPlugin({
include: 'src/locales/*' // a picomatch pattern
})
],
};
Configuration
interface I18nPluginOptions {
exclude?: string | RegExp | Array<string | RegExp>,
id_regex?: RegExp,
include: string | RegExp | Array<string | RegExp>,
parser?(src: string): string,
}
exclude
A picomatch glob pattern for files to not process.
id_regex
Default:
/(?<id>[a-z-_]+)\.[a-z]+/i
A RegExp with an id
group to extract the locale_id
from the import ID (file name/path).
You should only need to change this if you have multiple files for each locale (e.g. /src/locales/en/home.json
).
include
A picomatch glob pattern for files to process.
parser
A function to parse the code if your messages are not stored in JSON files.
Example
// vite.config.ts
import { I18nPlugin } from '@eartharoid/vite-plugin-i18n';
import { parse } from 'yaml'
export default {
plugins: [
I18nPlugin({
include: 'src/locales/*', // a picomatch pattern
parser: (code) => parse(code)
})
],
};