faker icon indicating copy to clipboard operation
faker copied to clipboard

Investigate why importing anything from '@faker-js/faker' directly causes all of faker to be bundled.

Open ST-DDT opened this issue 2 years ago • 10 comments

Clear and concise description of the problem

Importing anything directly from @faker-js/faker causes the bundler to contain all of faker in the resulting bundle.

Reproduction:

https://github.com/faker-js/playground/blob/main/playgrounds/vite-cjs/src/App.vue

Add:

import { FakerError } from "@faker-js/faker";

const natureImageUrl = ref(faker.image.nature() + new FakerError("test"));

That will increase the bundle size from 0.6MB to 3.6MB.

Maybe try with sideEffect: false + refactorings.

Alternative

No response

Additional context

No response

ST-DDT avatar Jan 29 '23 16:01 ST-DDT

So to explain what a side effect is:

When a function modifies the state of something outside its scope, then it is declared as a side effect

For example:

https://github.com/faker-js/faker/blob/64f480d214dc0bd173b0921025c8a1ad76d12c0f/src/modules/helpers/unique.ts#L144

https://github.com/faker-js/faker/blob/64f480d214dc0bd173b0921025c8a1ad76d12c0f/src/modules/helpers/unique.ts#L104

https://github.com/faker-js/faker/blob/64f480d214dc0bd173b0921025c8a1ad76d12c0f/src/modules/helpers/unique.ts#L9

The helpers.unique function takes GLOBAL_UNIQUE_STORE as a default value and this is defined out of scope of the function itself, but is modified inside it in line 144. So if the default is taken for unique, then the modification to GLOBAL_UNIQUE_STORE is a side effect.


I'm not aware of any other side effects right now

Shinigami92 avatar Jan 29 '23 19:01 Shinigami92

Would moving this to a class field fix that issue?

ST-DDT avatar Jan 31 '23 00:01 ST-DDT

Team Decision

We will try to investigate this for v8.0

  • Remove global unique store in favor of a faker (helperModule) instance bound one

ST-DDT avatar Apr 13 '23 17:04 ST-DDT

@xDivisionByZerox pointed made a hint in meeting 2023-04-20 that we could move the variable allLocales into the file src/locales/index.ts itself

https://github.com/faker-js/faker/blob/bf67a219365d63c9430d4935056752859eaeaa8f/src/index.ts#L41

This should be tested and could work, so that when loading the file, no variable assignment will be made in src/index.ts itself, but only in the underlying file on demand

Shinigami92 avatar Apr 20 '23 18:04 Shinigami92

@evanw could you help us with this bug?

Please checkout

git clone [email protected]:faker-js/faker.git
cd faker
pnpm install
pnpm run build

cd ..

git clone [email protected]:faker-js/playground.git
cd playground
git switch next
pnpm install
cd playgrounds/vite-cjs

pnpm run compile # 3759.57 KiB / gzip: 995.54 KiB

Open /playground/playgrounds/vite-cjs/src/App.vue

-   faker.image.urlLoremFlickr({ category: "nature" }) + new FakerError("test")
+   faker.image.urlLoremFlickr({ category: "nature" }) + new Error("test")

run pnpm run compile again, see difference in bundle size (618.70 KiB / gzip: 189.16 KiB)

Shinigami92 avatar Apr 20 '23 19:04 Shinigami92

Moving this into Milestone v8 as we have decided that it is not blocking a release v8.0 Still a bug, but we need help e.g. from the community

Shinigami92 avatar May 06 '23 14:05 Shinigami92

I just realised that faker is taking 3.6MB after build in React... Imo its a critical issue, its way too big

graniczny avatar Jun 29 '23 10:06 graniczny

Thats why we explicitly ask you to install our library as a Dev Dependency: https://fakerjs.dev/guide/#installation

Install it as a Dev Dependency using your favorite package manager.

There is also additional hints about this in the Usage Guide for the Browser: https://fakerjs.dev/guide/usage.html#browser

Note: Using the browser is great for experimenting 👍. However, due to all of the strings Faker uses to generate fake data, Faker is a large package. It's > 5 MiB minified. Please avoid deploying the full Faker in your web app.

xDivisionByZerox avatar Jun 29 '23 10:06 xDivisionByZerox

Faker version 5 is totally fine on the browser.

FredericHeem avatar Jun 29 '23 10:06 FredericHeem

You can also import a specific locale to avoid the bug.

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

https://fakerjs.dev/guide/localization.html#individual-localized-packages

ST-DDT avatar Jun 29 '23 12:06 ST-DDT

Faker version 5 is totally fine on the browser.

It is super heavy.

SalahAdDin avatar Apr 07 '24 00:04 SalahAdDin