From 5e8714ccc9574614f969a5551547d039fe11a527 Mon Sep 17 00:00:00 2001 From: Dimitris Klouvas Date: Wed, 29 Nov 2023 23:51:25 +0200 Subject: [PATCH] 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: https://github.com/clerk/javascript/pull/2046 Co-authored-by: Andrew --- .changeset/selfish-trains-breathe.md | 14 +++++++ packages/localizations/.gitignore | 2 + packages/localizations/package.json | 53 ++++++++++++++++++++++++++- packages/localizations/subpaths.mjs | 35 ++++++++++++++++++ packages/localizations/tsup.config.ts | 15 ++++---- packages/shared/subpaths.mjs | 2 + scripts/subpath-workaround.mjs | 2 +- 7 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 .changeset/selfish-trains-breathe.md create mode 100644 packages/localizations/.gitignore create mode 100644 packages/localizations/subpaths.mjs diff --git a/.changeset/selfish-trains-breathe.md b/.changeset/selfish-trains-breathe.md new file mode 100644 index 0000000000..055d37fb4f --- /dev/null +++ b/.changeset/selfish-trains-breathe.md @@ -0,0 +1,14 @@ +--- +'@clerk/localizations': patch +--- + +The package now allows for [subpath exports](https://nodejs.org/api/packages.html#subpath-exports). + +```diff +- import { frFR } from "@clerk/localizations" ++ import { frFR } from "@clerk/localizations/fr-FR" +``` + +This should help with tree-shaking by helping the bundler to include only specific localization. + +This is a non-breaking change-previous imports from "@clerk/localizations" are still working as expected. \ No newline at end of file diff --git a/packages/localizations/.gitignore b/packages/localizations/.gitignore new file mode 100644 index 0000000000..5b45b89ce3 --- /dev/null +++ b/packages/localizations/.gitignore @@ -0,0 +1,2 @@ +/*/ +!/src/ diff --git a/packages/localizations/package.json b/packages/localizations/package.json index 29d7056b37..726c2ed90b 100644 --- a/packages/localizations/package.json +++ b/packages/localizations/package.json @@ -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/" diff --git a/packages/localizations/subpaths.mjs b/packages/localizations/subpaths.mjs new file mode 100644 index 0000000000..4b5492c4af --- /dev/null +++ b/packages/localizations/subpaths.mjs @@ -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 = []; diff --git a/packages/localizations/tsup.config.ts b/packages/localizations/tsup.config.ts index f138fb99e1..a275cf8172 100644 --- a/packages/localizations/tsup.config.ts +++ b/packages/localizations/tsup.config.ts @@ -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, }; }); diff --git a/packages/shared/subpaths.mjs b/packages/shared/subpaths.mjs index 62a33e4b06..7aeddf9def 100644 --- a/packages/shared/subpaths.mjs +++ b/packages/shared/subpaths.mjs @@ -27,3 +27,5 @@ export const subpathNames = [ ]; export const subpathFoldersBarrel = ['react']; + +export const ignoredFolders = ['scripts']; diff --git a/scripts/subpath-workaround.mjs b/scripts/subpath-workaround.mjs index 41fd1da56c..cc70aa1a80 100644 --- a/scripts/subpath-workaround.mjs +++ b/scripts/subpath-workaround.mjs @@ -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));