faker icon indicating copy to clipboard operation
faker copied to clipboard

Export individual API modules on their own

Open Cephyric-gh opened this issue 2 years ago • 5 comments

Clear and concise description of the problem

When using the package, even if you only ever needed 1 specific function you have to import the entire thing into your project in order to use it, which leaves it at the mercy of tree-shaking (assuming you're bundling the project that makes use of faker, which I'd assume is most use-cases).

Suggested solution

Instead of exporting everything under the to-level faker class, instead export each API module separately to allow only importing the things that are actually being used

// Current way to import, meaning we have to hope that the specific
// bundler being used (rollup, webpack, esbuild, etc) tree-shakes everything else away
import { faker } from '@faker-js/faker';

const noun = faker.word.noun();

vs

// Proposed solution, guarantees that at most the only thing that is
// bundled in is all the underlying functions for the "word" module
import { word } from '@faker-js/faker';

const noun = word.noun();

Alternative

The APIs could be exported alongside the main export, which would remov th breaking change

import { faker, word } from '@faker-js/faker';

Or the APIs could be exported in their own nested namespace (the same as locales are, though that may make locales funky)

import { faker } from '@faker-js/faker';
import { word } from '@faker-js/faker/word';
import { address } from '@faker-js/faker/locale/de/word';

Additional context

No response

Cephyric-gh avatar Jun 29 '22 13:06 Cephyric-gh