You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently working on a project where I use JSON files in two ways. In the first way, I import from package.json. In the second, I fetch JSON files at runtime, which are used to store locale data. I just started experiencing a problem where all of the JSON files are converted to JS files, so the fetch fails because example.json doesn't exist (It is now example.js). When importing a JSON file this makes sense, but I'm trying to copy it to the output. Note bundle is true and format is esm.
This fetch call will stay the same after being transformed, though locales/en.json is transformed to locales/en.js, which causes the fetch to fail. This behavior is incorrect, at least for my use case.
Please note: The files in locales are entry points, and the JSON files imported are not.
I can't fix this issue using Esbuild's configuration options, since I cannot specify a different loader for different paths for JSON files.
The text was updated successfully, but these errors were encountered:
First of all, to make esbuild understand the string 'locales/en.json' is actually a file referenced in the bundle's entry points, it should not be put in the fetch() call. The only way to indicate that is using require('<path>')/import '<path>'/import('<path>'). So the correct usage is:
consten=awaitimport('./locales/en.json')someI18nLibrary.locales.set({ en })
Note that this way, esbuild knows you're dynamically importing a file. It will split that file out as a chunk so that it gets lazily loaded. Therefore you do not have to mark this file as entry point. Playground Link
On the other hand, if you want to keep the .json extension and still use fetch() calls to load them. These json files should be considered static assets and be copied to the public folder. In which case you just stop feeding them as entry points, and write extra scripts to do the copy work.
I'm currently working on a project where I use JSON files in two ways. In the first way, I
import
frompackage.json
. In the second, Ifetch
JSON files at runtime, which are used to store locale data. I just started experiencing a problem where all of the JSON files are converted to JS files, so thefetch
fails becauseexample.json
doesn't exist (It is nowexample.js
). Whenimport
ing a JSON file this makes sense, but I'm trying to copy it to the output. Notebundle
istrue
andformat
isesm
.Basically:
example.ts
Gets transformed to
This is correct.
locales.ts
This fetch call will stay the same after being transformed, though
locales/en.json
is transformed tolocales/en.js
, which causes thefetch
to fail. This behavior is incorrect, at least for my use case.Please note: The files in
locales
are entry points, and the JSON filesimport
ed are not.I can't fix this issue using Esbuild's configuration options, since I cannot specify a different loader for different paths for JSON files.
The text was updated successfully, but these errors were encountered: