Skip to content

Commit

Permalink
feat(localizations): Allow usage of subpath exports (#2236)
Browse files Browse the repository at this point in the history
* feat(localizations): Allow usage of subpath exports

The addition of `.gitignore` and `subpaths.mjs` is used to support
Expo framework. This fix is also used in `@clerk/shared` package.

original PR: #2046

Co-authored-by: Andrew <[email protected]>

* update changeset

---------

Co-authored-by: Andrew <[email protected]>
Co-authored-by: Lennart <[email protected]>
  • Loading branch information
3 people authored Nov 30, 2023
1 parent 0551488 commit 2d383e4
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 10 deletions.
20 changes: 20 additions & 0 deletions .changeset/selfish-trains-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@clerk/localizations': patch
---

The package now allows for [subpath exports](https://nodejs.org/api/packages.html#subpath-exports). You can now import specific languages like so:

```diff
# Single language
- import { frFR } from "@clerk/localizations"
+ import { frFR } from "@clerk/localizations/fr-FR"

# Multiple languages
- import { enUS, esES } from "@clerk/localizations"
+ import { enUS } from "@clerk/localizations/en-US"
+ import { esES } from "@clerk/localizations/es-ES"
```

This helps with tree-shaking and will reduce your total bundle size in most cases.

You can continue to use the top-level `@clerk/localizations` import as this is a non-breaking change. You can gradually opt-in to this optimization.
2 changes: 2 additions & 0 deletions packages/localizations/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*/
!/src/
53 changes: 51 additions & 2 deletions packages/localizations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,63 @@
"license": "MIT",
"author": "Clerk",
"sideEffects": false,
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"./*": {
"import": {
"types": "./dist/*.d.mts",
"default": "./dist/*.mjs"
},
"require": {
"types": "./dist/*.d.ts",
"default": "./dist/*.js"
}
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
"dist",
"ar-SA",
"cs-CZ",
"da-DK",
"de-DE",
"el-GR",
"en-US",
"es-ES",
"fr-FR",
"he-IL",
"it-IT",
"ja-JP",
"ko-KR",
"nb-NO",
"nl-NL",
"pl-PL",
"pt-BR",
"pt-PT",
"ro-RO",
"ru-RU",
"sk-SK",
"sv-SE",
"tr-TR",
"uk-UA",
"vi-VN",
"zh-CN",
"zh-TW"
],
"scripts": {
"build": "tsup --env.NODE_ENV production",
"postbuild": "node ../../scripts/subpath-workaround.mjs localizations",
"clean": "rimraf ./dist",
"dev": "tsup --watch",
"lint": "eslint src/"
Expand Down
35 changes: 35 additions & 0 deletions packages/localizations/subpaths.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// This file is a helper for the "subpath-workaround.mjs" script
// We have to polyfill our "exports" subpaths :cry:

export const subpathNames = [
'ar-SA',
'cs-CZ',
'da-DK',
'de-DE',
'el-GR',
'en-US',
'es-ES',
'fr-FR',
'he-IL',
'it-IT',
'ja-JP',
'ko-KR',
'nb-NO',
'nl-NL',
'pl-PL',
'pt-BR',
'pt-PT',
'ro-RO',
'ru-RU',
'sk-SK',
'sv-SE',
'tr-TR',
'uk-UA',
'vi-VN',
'zh-CN',
'zh-TW',
];

export const subpathFoldersBarrel = [];

export const ignoredFolders = [];
15 changes: 8 additions & 7 deletions packages/localizations/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { defineConfig } from 'tsup';

export default defineConfig(() => {
export default defineConfig(_overrideOptions => {
const uiRetheme = process.env.CLERK_RETHEME === '1' || process.env.CLERK_RETHEME === 'true';

return {
entry: {
index: uiRetheme ? 'src/index.retheme.ts' : 'src/index.ts',
},
minify: false,
entry: uiRetheme
? ['src', '!src/index.ts', '!src/en-US.ts']
: ['src', '!src/index.retheme.ts', '!src/en-US.retheme.ts'],
format: ['cjs', 'esm'],
bundle: true,
clean: true,
minify: false,
sourcemap: true,
format: ['cjs', 'esm'],
legacyOutput: true,
dts: true,
splitting: false,
};
});
2 changes: 2 additions & 0 deletions packages/shared/subpaths.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ export const subpathNames = [
];

export const subpathFoldersBarrel = ['react'];

export const ignoredFolders = ['scripts'];
2 changes: 1 addition & 1 deletion scripts/subpath-workaround.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ async function run() {
const allFilesNames = [
...subpathHelperFile.subpathNames,
...subpathHelperFile.subpathFoldersBarrel,
...subpathHelperFile.ignoredFolders,
'dist',
'scripts',
];
const hasAllSubpathsInFiles = pkgFile.files.every(name => allFilesNames.includes(name));

Expand Down

0 comments on commit 2d383e4

Please sign in to comment.