cibernox / svelte-intl-precompile

I18n library for Svelte.js that analyzes your keys at build time for max performance and minimal footprint
https://svelte-intl-precompile.com
ISC License
274 stars 13 forks source link

feat: support all i18n-ally file formats for locales #34

Closed sastan closed 2 years ago

sastan commented 2 years ago

Hello @cibernox,

sorry for such a big PR. I've added a lot of things. If you like I can create separate PRs for some of the features or remove them completely if you don't see them as part of this library.

Here is a list of things I've added or changed with links to the lines in the source.

Support all i18n-ally file formats

  1. [FEATURE] Support all i18n-ally file formats for locales – json5 (.json5 and .json) and yaml (.yaml and .yml) — https://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.js#L26:L39 and https://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.js#L126-L139
  2. [FEATURE] Support locales to be defined within .js, .ts, and .mjs files — https://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.js#L29-L31
  3. [FEATURE] Allow configuring new transformers based on file extension — https://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.js#L42-L51
  4. [FEATURE] Support loading of locales without an extension ($locales/en instead of $locales/en.js) — https://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.js#L183-L195 and https://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.js#L205-L217

Loading of sveltekit-plugin

These changes allow to use

without an extension.

The legacy svelte-intl-precompile/sveltekit-plugin.js and svelte-intl-precompile/sveltekit-plugin.cjs do still work. In the future, I would suggest deprecating these because svelte-intl-precompile/sveltekit-plugin works for import and require since Node.js v12.7.0

This structure and content of package.json fields exports, main, and type are based on the recommendations given in https://nodejs.org/dist/latest-v16.x/docs/api/packages.html#conditional-exports.

  1. [FEATURE] Add exports field to package.json with legacy ./sveltekit-plugin.js and ./sveltekit-plugin.cjs exports — https://github.com/sastan/svelte-intl-precompile/blob/main/package.json#L5-L26
  2. [FEATURE] Add typescript definitions for svelte-intl-precompile/sveltekit-pluginhttps://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.d.ts
  3. [BUILD] Autogenerate ./sveltekit-plugin.cjs from ./sveltekit-plugin.js during publishing — https://github.com/sastan/svelte-intl-precompile/blob/main/package.json#L26-L27 and https://github.com/sastan/svelte-intl-precompile/blob/main/.gitignore#L2

Windows file path handling

This is done by using path-starts-with to check it a file is within the locales directory.

  1. [BUGFIX] Normalize paths when determining if a file is within the locales directory — https://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.js#L15, https://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.js#L222, and

Misc

  1. [FEATURE] Sort availableLocales export of $locales module that more specific locales come first en-US comes before enhttps://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.js#L86-L94
  2. [FEATURE] Add filename to babel transform for better debugging — https://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.js#L115 and https://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.js#L10-L12
  3. [BUGFIX] Invalidate $locales module in HMR mode if a file within the locales directory has changed — https://github.com/sastan/svelte-intl-precompile/blob/main/sveltekit-plugin.js#L168-L172
  4. [BUGFIX] Add index.cjs to re-export precompile-intl-runtime for cjs compat loading — https://github.com/sastan/svelte-intl-precompile/blob/main/package.json#L10 and https://github.com/sastan/svelte-intl-precompile/blob/main/index.cjs
cibernox commented 2 years ago

This. Is. Awesome

It's amazing how many little details you've pulled in one go! This this package gaining so many features (it used to be a very simple reexport of precompile-intl-runtime) it's time to create a test suite, but that's beyond the scope of this PR. I'll merge it now!

sastan commented 2 years ago

Thank you for the fast merge. About testing I'm eager to try vitest. I think this could be a good fit as this is basically a vite plugin. If you want I could give it a try.

cibernox commented 2 years ago

I’ve been meaning to try it too so go ahead 👍