Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(localizations): Allow usage of subpath exports #2236

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading