Faker.js: May I import faker.js using ES6 modules syntax?

Created on 20 May 2019  路  7Comments  路  Source: Marak/faker.js

The question is pretty self-explanatory. May I?

Most helpful comment

The answer is yes (kind of), i.e. you can import the sub namespaces explicitly.

import { company, address } from 'faker';

However this works because the entire faker namespace is assigned to module['exports'] in index.js and I'm not so sure if this still allows for tree shaking, since those sub "namespaces" are simply members of a huge object and no es/ts modules.
Update: I just tested it with rollup and I was right. It doesn't matter if I import faker.. or import { company }... the bundle size is always the same.

Consequently what you cannot do is import only single functions from each namespace, i.e.

import { city } from 'faker/company';

I've just looked into it and this would require major package restructuring. Going for typescript (since its a library) could also be considered while doing this but is in fact out of scope of import concerns.

@Marak whats your take on this? Do you plan on splitting up all the namespaces to their own es modules? I could help with that if necessary.

All 7 comments

IMO That would be great!

You can do it like such in TypeScript:

```typescript
import * as faker from 'faker';
````

I'm not sure how this would work for ES6 though

You can do it !
I use example: import faker from 'faker'; or var faker = require('faker');

const faker = require("faker");

Bingo!

I think the comments are missing the intent of the issue. The ability to import faker with es6 is to import only what functionality you need and leverage tree-shaking. Currently, you pay the penalty of the entire library. See below.
image

The answer is yes (kind of), i.e. you can import the sub namespaces explicitly.

import { company, address } from 'faker';

However this works because the entire faker namespace is assigned to module['exports'] in index.js and I'm not so sure if this still allows for tree shaking, since those sub "namespaces" are simply members of a huge object and no es/ts modules.
Update: I just tested it with rollup and I was right. It doesn't matter if I import faker.. or import { company }... the bundle size is always the same.

Consequently what you cannot do is import only single functions from each namespace, i.e.

import { city } from 'faker/company';

I've just looked into it and this would require major package restructuring. Going for typescript (since its a library) could also be considered while doing this but is in fact out of scope of import concerns.

@Marak whats your take on this? Do you plan on splitting up all the namespaces to their own es modules? I could help with that if necessary.

Any traction on this? I can collaborate as well if necessary.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jamesbrucepower picture jamesbrucepower  路  6Comments

kevinburke picture kevinburke  路  5Comments

Deilan picture Deilan  路  3Comments

maticzav picture maticzav  路  3Comments

aadamsx picture aadamsx  路  6Comments