Skip to content

Commit

Permalink
fix: tree-shaking (#2790)
Browse files Browse the repository at this point in the history
Co-authored-by: ST-DDT <ST-DDT@gmx.de>
  • Loading branch information
xDivisionByZerox and ST-DDT committed Apr 11, 2024
1 parent 200a38e commit fb65976
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 97 deletions.
29 changes: 5 additions & 24 deletions docs/guide/localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,6 @@ For example, you can import the German locale:
You can also build your own Faker instances, with custom locales/overwrites.
:::

## Individual localized packages

Currently, the imports from the main package have a [bug](https://github.com/faker-js/faker/issues/1791) and always cause the entire Faker lib to be imported.
This might result in loading around 5 MB of data into memory and slow down startup times.

_But we got your back!_
When encountering such a problem in a test or production environment, you can use the individual localized packages.

```ts
import { faker } from '@faker-js/faker/locale/de';
```

This will then just load the German locales with additional English locales as fallback. The fallback is required due to not all locales containing data for all features. If you encounter a missing locale entry in your selected language, feel free to open a Pull Request fixing that issue.

::: info Info
The English locales are around 600 KB in size.
All locales together are around 5 MB in size.
:::

::: tip Note
Some locales have limited coverage and rely more heavily on the English locale as the source for features they currently do not have.
However, in most cases, using a specific locale will be beneficial in the long term as specifying a locale reduces the time necessary for startup, which has a compounding effect on testing frameworks that reload the imports every execution.
:::

## Custom locales and fallbacks

If our built-in faker instances don't satisfy your needs, you can build your own:
Expand Down Expand Up @@ -152,6 +128,11 @@ The `Locale` (data) and `Faker` columns refer to the respective `import` names:
import { de, fakerDE } from '@faker-js/faker';
```

::: tip Note
Some locales have limited coverage and rely more heavily on the English locale as the source for features they currently do not have.
However, in most cases, using a specific locale will be beneficial in the long term as specifying a locale reduces the time necessary for startup, which has a compounding effect on testing frameworks that reload the imports every execution.
:::

## Locale codes

Locales are named in a systematic way. The first two characters are a lowercase language code following the [ISO 639-1 standard](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) for example `ar` for Arabic or `en` for English.
Expand Down
18 changes: 18 additions & 0 deletions docs/guide/upgrading_v9/2790.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### Fix Tree Shaking

Prior to this version, users had to resort to workarounds by importing specific faker instances from dedicated paths to overcome tree shaking issues.

```ts
import { faker } from '@faker-js/faker/locale/de';
```

With the implementation of this fix, such workarounds should no longer be necessary.
That means that you should be able to import different localized faker instances from the root of your package.

```ts
import { fakerDE, fakerES, fakerFR } from '@faker-js/faker';
```

The dedicated import paths will still stay for now, to allow a gradual migration for our users.

While the implementation of this change does not constitute as breaking according to semantic versioning guidelines, it does impact the behavior of users bundlers.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@faker-js/faker",
"version": "8.4.1",
"description": "Generate massive amounts of fake contextual data",
"sideEffects": false,
"keywords": [
"faker",
"faker.js",
Expand Down
10 changes: 7 additions & 3 deletions scripts/generate-locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ removeIndexTs(locales);
let localeIndexImports = '';
let localeIndexExportsIndividual = '';
let localeIndexExportsGrouped = '';
let localesIndexExports = '';
let localesIndexImports = '';

let localizationLocales = '| Locale | Name | Faker |\n| :--- | :--- | :--- |\n';

Expand Down Expand Up @@ -406,7 +406,7 @@ for (const locale of locales) {
localeIndexImports += `import { faker as ${localizedFaker} } from './${locale}';\n`;
localeIndexExportsIndividual += ` ${localizedFaker},\n`;
localeIndexExportsGrouped += ` ${locale}: ${localizedFaker},\n`;
localesIndexExports += `export { default as ${locale} } from './${locale}';\n`;
localesIndexImports += `import { default as ${locale} } from './${locale}';\n`;
localizationLocales += `| \`${locale}\` | ${localeTitle} | \`${localizedFaker}\` |\n`;

// src/locale/<locale>.ts
Expand Down Expand Up @@ -445,7 +445,11 @@ writeFileSync(pathLocaleIndex, localeIndexContent);
let localesIndexContent = `
${autoGeneratedCommentHeader}
${localesIndexExports}
${localesIndexImports}
export { ${locales.join(',')} };
export const allLocales = { ${locales.join(',')} };
`;

localesIndexContent = await formatTypescript(localesIndexContent);
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export {
export * from './locale';
export { fakerEN as faker } from './locale';
export * from './locales';
export * as allLocales from './locales';
export { Aircraft } from './modules/airline';
export type { AircraftType, AirlineModule } from './modules/airline';
export type { AnimalModule } from './modules/animal';
Expand Down
282 changes: 213 additions & 69 deletions src/locales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,216 @@
* Run 'pnpm run generate:locales' to update.
*/

export { default as af_ZA } from './af_ZA';
export { default as ar } from './ar';
export { default as az } from './az';
export { default as base } from './base';
export { default as cs_CZ } from './cs_CZ';
export { default as da } from './da';
export { default as de } from './de';
export { default as de_AT } from './de_AT';
export { default as de_CH } from './de_CH';
export { default as dv } from './dv';
export { default as el } from './el';
export { default as en } from './en';
export { default as en_AU } from './en_AU';
export { default as en_AU_ocker } from './en_AU_ocker';
export { default as en_BORK } from './en_BORK';
export { default as en_CA } from './en_CA';
export { default as en_GB } from './en_GB';
export { default as en_GH } from './en_GH';
export { default as en_HK } from './en_HK';
export { default as en_IE } from './en_IE';
export { default as en_IN } from './en_IN';
export { default as en_NG } from './en_NG';
export { default as en_US } from './en_US';
export { default as en_ZA } from './en_ZA';
export { default as eo } from './eo';
export { default as es } from './es';
export { default as es_MX } from './es_MX';
export { default as fa } from './fa';
export { default as fi } from './fi';
export { default as fr } from './fr';
export { default as fr_BE } from './fr_BE';
export { default as fr_CA } from './fr_CA';
export { default as fr_CH } from './fr_CH';
export { default as fr_LU } from './fr_LU';
export { default as fr_SN } from './fr_SN';
export { default as he } from './he';
export { default as hr } from './hr';
export { default as hu } from './hu';
export { default as hy } from './hy';
export { default as id_ID } from './id_ID';
export { default as it } from './it';
export { default as ja } from './ja';
export { default as ka_GE } from './ka_GE';
export { default as ko } from './ko';
export { default as lv } from './lv';
export { default as mk } from './mk';
export { default as nb_NO } from './nb_NO';
export { default as ne } from './ne';
export { default as nl } from './nl';
export { default as nl_BE } from './nl_BE';
export { default as pl } from './pl';
export { default as pt_BR } from './pt_BR';
export { default as pt_PT } from './pt_PT';
export { default as ro } from './ro';
export { default as ro_MD } from './ro_MD';
export { default as ru } from './ru';
export { default as sk } from './sk';
export { default as sr_RS_latin } from './sr_RS_latin';
export { default as sv } from './sv';
export { default as th } from './th';
export { default as tr } from './tr';
export { default as uk } from './uk';
export { default as ur } from './ur';
export { default as uz_UZ_latin } from './uz_UZ_latin';
export { default as vi } from './vi';
export { default as yo_NG } from './yo_NG';
export { default as zh_CN } from './zh_CN';
export { default as zh_TW } from './zh_TW';
export { default as zu_ZA } from './zu_ZA';
import { default as af_ZA } from './af_ZA';
import { default as ar } from './ar';
import { default as az } from './az';
import { default as base } from './base';
import { default as cs_CZ } from './cs_CZ';
import { default as da } from './da';
import { default as de } from './de';
import { default as de_AT } from './de_AT';
import { default as de_CH } from './de_CH';
import { default as dv } from './dv';
import { default as el } from './el';
import { default as en } from './en';
import { default as en_AU } from './en_AU';
import { default as en_AU_ocker } from './en_AU_ocker';
import { default as en_BORK } from './en_BORK';
import { default as en_CA } from './en_CA';
import { default as en_GB } from './en_GB';
import { default as en_GH } from './en_GH';
import { default as en_HK } from './en_HK';
import { default as en_IE } from './en_IE';
import { default as en_IN } from './en_IN';
import { default as en_NG } from './en_NG';
import { default as en_US } from './en_US';
import { default as en_ZA } from './en_ZA';
import { default as eo } from './eo';
import { default as es } from './es';
import { default as es_MX } from './es_MX';
import { default as fa } from './fa';
import { default as fi } from './fi';
import { default as fr } from './fr';
import { default as fr_BE } from './fr_BE';
import { default as fr_CA } from './fr_CA';
import { default as fr_CH } from './fr_CH';
import { default as fr_LU } from './fr_LU';
import { default as fr_SN } from './fr_SN';
import { default as he } from './he';
import { default as hr } from './hr';
import { default as hu } from './hu';
import { default as hy } from './hy';
import { default as id_ID } from './id_ID';
import { default as it } from './it';
import { default as ja } from './ja';
import { default as ka_GE } from './ka_GE';
import { default as ko } from './ko';
import { default as lv } from './lv';
import { default as mk } from './mk';
import { default as nb_NO } from './nb_NO';
import { default as ne } from './ne';
import { default as nl } from './nl';
import { default as nl_BE } from './nl_BE';
import { default as pl } from './pl';
import { default as pt_BR } from './pt_BR';
import { default as pt_PT } from './pt_PT';
import { default as ro } from './ro';
import { default as ro_MD } from './ro_MD';
import { default as ru } from './ru';
import { default as sk } from './sk';
import { default as sr_RS_latin } from './sr_RS_latin';
import { default as sv } from './sv';
import { default as th } from './th';
import { default as tr } from './tr';
import { default as uk } from './uk';
import { default as ur } from './ur';
import { default as uz_UZ_latin } from './uz_UZ_latin';
import { default as vi } from './vi';
import { default as yo_NG } from './yo_NG';
import { default as zh_CN } from './zh_CN';
import { default as zh_TW } from './zh_TW';
import { default as zu_ZA } from './zu_ZA';

export {
af_ZA,
ar,
az,
base,
cs_CZ,
da,
de,
de_AT,
de_CH,
dv,
el,
en,
en_AU,
en_AU_ocker,
en_BORK,
en_CA,
en_GB,
en_GH,
en_HK,
en_IE,
en_IN,
en_NG,
en_US,
en_ZA,
eo,
es,
es_MX,
fa,
fi,
fr,
fr_BE,
fr_CA,
fr_CH,
fr_LU,
fr_SN,
he,
hr,
hu,
hy,
id_ID,
it,
ja,
ka_GE,
ko,
lv,
mk,
nb_NO,
ne,
nl,
nl_BE,
pl,
pt_BR,
pt_PT,
ro,
ro_MD,
ru,
sk,
sr_RS_latin,
sv,
th,
tr,
uk,
ur,
uz_UZ_latin,
vi,
yo_NG,
zh_CN,
zh_TW,
zu_ZA,
};

export const allLocales = {
af_ZA,
ar,
az,
base,
cs_CZ,
da,
de,
de_AT,
de_CH,
dv,
el,
en,
en_AU,
en_AU_ocker,
en_BORK,
en_CA,
en_GB,
en_GH,
en_HK,
en_IE,
en_IN,
en_NG,
en_US,
en_ZA,
eo,
es,
es_MX,
fa,
fi,
fr,
fr_BE,
fr_CA,
fr_CH,
fr_LU,
fr_SN,
he,
hr,
hu,
hy,
id_ID,
it,
ja,
ka_GE,
ko,
lv,
mk,
nb_NO,
ne,
nl,
nl_BE,
pl,
pt_BR,
pt_PT,
ro,
ro_MD,
ru,
sk,
sr_RS_latin,
sv,
th,
tr,
uk,
ur,
uz_UZ_latin,
vi,
yo_NG,
zh_CN,
zh_TW,
zu_ZA,
};

0 comments on commit fb65976

Please sign in to comment.