From 7d806d9a0b9914f9e0ce0c11e529bdd8ae116fc4 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Sat, 19 Feb 2022 23:56:06 +0100 Subject: [PATCH] build: add redirect aliases for cjs (#520) --- .gitignore | 1 + package.json | 1 + scripts/bundle.ts | 16 ++++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/.gitignore b/.gitignore index 414655adbac..dba79bf0e48 100644 --- a/.gitignore +++ b/.gitignore @@ -74,6 +74,7 @@ versions.json /docs/.vitepress/dist /docs/api/typedoc.json /lib +/locale # Faker TAGS diff --git a/package.json b/package.json index 5404cef51b7..94ccac85852 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "CHANGELOG.md", "CHANGELOG_old.md", "dist", + "locale", "tsconfig.json" ], "scripts": { diff --git a/scripts/bundle.ts b/scripts/bundle.ts index 793f27a1436..6d1b7b749ba 100644 --- a/scripts/bundle.ts +++ b/scripts/bundle.ts @@ -1,8 +1,24 @@ import { buildSync } from 'esbuild'; import { sync as globSync } from 'glob'; +import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; import locales from '../src/locales'; console.log('Building dist for node (cjs)...'); + +// Generate entry-points for cjs compatibility +const localeDir = 'locale'; +if (existsSync(localeDir)) { + rmSync(localeDir, { recursive: true, force: true }); +} +mkdirSync(localeDir); +for (const locale of Object.keys(locales)) { + writeFileSync( + `${localeDir}/${locale}.js`, + `module.exports = require('../dist/cjs/locale/${locale}');\n`, + { encoding: 'utf8' } + ); +} + buildSync({ entryPoints: globSync('./src/**/*.ts'), // We can use the following entry points when esbuild supports cjs+splitting