Skip to content

Commit

Permalink
Merge branch 'next' into refactor/locale/normalize-animal-data
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Apr 11, 2024
2 parents 7d2ce24 + fb65976 commit 093ccdd
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 104 deletions.
4 changes: 4 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ const config: UserConfig<DefaultTheme.Config> = {
text: 'Roadmap',
link: '/about/roadmap/',
items: [
{
text: 'v9 - Tree-Shakeable Module-Functions',
link: '/about/roadmap/v9',
},
{ text: 'v8 - Make Faker Handier', link: '/about/roadmap/v8' },
{
text: 'v7 - Cleanup & Improvements',
Expand Down
4 changes: 4 additions & 0 deletions docs/about/roadmap/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Click on the specific versions for more details.

## Upcoming Versions

- [v9 - Tree Shaking](v9.md)

## Current Versions

- [v8 - Make Faker Handier](v8.html)

## Previous Versions
Expand Down
7 changes: 0 additions & 7 deletions docs/about/roadmap/v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,3 @@ Finish the module shuffling.
Split the Faker class into smaller units so you don't have ship an entire locale if you only generate some strings and numbers.

[v8.1 Tasks](https://github.com/faker-js/faker/milestone/11)

## v8.x - Tree-Shakeable Module-Functions

Refactor modules to be tree shakeable.
Potentially allowing individual Faker methods to be called by themselves.

[v8.x Tasks](https://github.com/faker-js/faker/milestone/12)
6 changes: 6 additions & 0 deletions docs/about/roadmap/v9.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## v9.0 - Tree-Shakeable Module-Functions

Fix the issue of Faker not being tree shakeable.
Potentially allowing individual Faker methods to be called by themselves.

[v9.0 Tasks](https://github.com/faker-js/faker/milestone/12)
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 @@ -374,7 +374,7 @@ removeIndexTs(locales);
let localeIndexImports = '';
let localeIndexExportsIndividual = '';
let localeIndexExportsGrouped = '';
let localesIndexExports = '';
let localesIndexImports = '';

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

Expand Down Expand Up @@ -405,7 +405,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 @@ -444,7 +444,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
Loading

0 comments on commit 093ccdd

Please sign in to comment.