npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@eartharoid/vite-plugin-i18n

v1.0.0-alpha.5

Published

A Vite plugin for I18n and CIF

Downloads

31

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)
		})
	],
};

Usage