Conversation
|
You need to change the bundling process somehow in Lines 43 to 48 in 5642470 Please also add tests to check each exported locale definition with an |
Codecov Report
@@ Coverage Diff @@
## main #642 +/- ##
========================================
Coverage 99.67% 99.67%
========================================
Files 2119 2119
Lines 227439 227553 +114
Branches 982 1040 +58
========================================
+ Hits 226691 226808 +117
+ Misses 728 725 -3
Partials 20 20
|
|
Thanks @Shinigami92 for the quick feedback. |
|
It's working? omg that would be really nice, thank you @mshima for this fix, I assume you will make many happy with that |
There was a problem hiding this comment.
Currently we need to do something like this:
import { Faker } from "@faker-js/faker";
import es from "@faker-js/faker/locales/es";
import fr from "@faker-js/faker/locales/fr";
console.log(
"Manually loaded locale:",
new Faker({
locales: { es: es.default, fr: fr.default },
locale: "es",
localeFallback: "fr",
}).locales.es.title
); // Manually loaded locale: SpanishThis is working, but it would be much better if we can somehow make it possible to omit the need of using <locale>.default
Or do you know a better way of importing them?
But also there are no types defined for these exports 🤔

So at least this needs to be fixed before we can merge this PR!
Edit 1:
I found out that this <locale>.default is only an issue using esm, but not cjs
For cjs it's just:
import { Faker } from "@faker-js/faker";
import es from "@faker-js/faker/locales/es";
import fr from "@faker-js/faker/locales/fr";
console.log(
"Manually loaded locale:",
new Faker({
locales: { es, fr },
locale: "es",
localeFallback: "fr",
}).locales.es.title
); // Manually loaded locale: SpanishEdit 2:
But only when you use TS with cjs 😆
When using cjs with raw js you need .default again 🤷
const { Faker } = require("@faker-js/faker");
const es = require("@faker-js/faker/locales/es");
const fr = require("@faker-js/faker/locales/fr");
console.log(
"Manually loaded locale:",
new Faker({
locales: { es: es.default, fr: fr.default },
locale: "es",
localeFallback: "fr",
}).locales.es.title
);
I think this is because of: Line 36 in 5642470 This makes node esm to import cjs version. |
I was not fully aware of what each of the things are doing when I crafted all together. Feel free to fix it until it's correct and I approved that everything is working. |
|
I will checkout the project to see the generated code. Until now it could be done using CI 😄. |
Looks like this is a pattern esbuild uses. node -e 'console.log(require("@faker-js/faker"));'
{ Faker: [Getter], default: [Getter], faker: [Getter] }Need to change the approach |
|
Should work like: const { Faker } = require("@faker-js/faker");
const { es, fr } = require("@faker-js/faker/locales");
console.log(
"Manually loaded locale:",
new Faker({
locales: { es, fr },
locale: "es",
localeFallback: "fr",
}).locales.es.title
); |
|
I will test this again this evening, due to I do not have the local playground on my workstation laptop but on my home pc |
|
cjs js: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './locales/es' is not defined by "exports" in /home/shinigami/OpenSource/faker.js-tests/cjs/node_modules/@faker-js/faker/package.jsoncjs ts: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './locales/es' is not defined by "exports" in /home/shinigami/OpenSource/faker.js-tests/cjs/node_modules/@faker-js/faker/package.jsonesm js: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './locales/es' is not defined by "exports" in /home/shinigami/OpenSource/faker.js-tests/esm/node_modules/@faker-js/faker/package.json imported from /home/shinigami/OpenSource/faker.js-tests/esm/index.jsesm ts: ✘ [ERROR] Could not resolve "@faker-js/faker/locales/es"
<stdin>:1:7:
1 │ import "@faker-js/faker/locales/es"
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The path "./locales/es" is not exported by package "@faker-js/faker":
node_modules/@faker-js/faker/package.json:34:13:
34 │ "exports": {
╵ ^
You can mark the path "@faker-js/faker/locales/es" as external to exclude it from the bundle,
which will remove this error.
✘ [ERROR] Could not resolve "@faker-js/faker/locales/fr"
<stdin>:1:7:
1 │ import "@faker-js/faker/locales/fr"
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The path "./locales/fr" is not exported by package "@faker-js/faker":
node_modules/@faker-js/faker/package.json:34:13:
34 │ "exports": {
╵ ^
You can mark the path "@faker-js/faker/locales/fr" as external to exclude it from the bundle,
which will remove this error.
/home/shinigami/OpenSource/faker.js-tests/esm/node_modules/.pnpm/esbuild@0.14.12/node_modules/esbuild/lib/main.js:1557
let error = new Error(`${text}${summary}`);
^
Error: Build failed with 1 error:
<stdin>:1:7: ERROR: Could not resolve "@faker-js/faker/locales/es"Types also doesn't resolve I will setup a faker playground repo that you can checkout to test it also yourself |
|
@mshima I created a playground here: https://github.com/faker-js/playground |
|
@mshima I see this PR and I like it, but currently our maintainer resources are a bit limited and we have a bunch of other things on our agenda. So it still could take a bit until we can proceed with this wanted feature.
import { de } from '@faker-js/de'
import { Faker } from '@faker-js/faker'
new Faker({ /* ... use de somehow */ }) |
|
@Shinigami92 no problem, I cannot use v7 for now since we need node 12 support. |
|
I think these makes more sense
|
@Shinigami92 is there a decision about the api? We are about to start a major version, so I may work on this soon. |
|
We will discuss this in the next team meeting again. |
|
@Shinigami92 will tackle this in a new PR for v8. |
|
As workaround: |
|
The locales can now be imported via Superseded by #1735 |
Locales should be exported otherwise the only way to have localeFallback is to use the default exported Faker instance.
Example: