Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON files as entry points and JSON files imported from TS/JS #3860

Closed
james-pre opened this issue Aug 1, 2024 · 2 comments
Closed

JSON files as entry points and JSON files imported from TS/JS #3860

james-pre opened this issue Aug 1, 2024 · 2 comments

Comments

@james-pre
Copy link

james-pre commented Aug 1, 2024

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.

Basically:

example.ts

export { version } from '../package.json';

Gets transformed to

var version, author;
var init_package = __esm({
  "package.json"() {
    version = "example";
  }
});

This is correct.

locales.ts

// ...
await fetch('locales/en.json');
// ...

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.

@hyrious
Copy link

hyrious commented Aug 2, 2024

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:

const en = await import('./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.

@james-pre
Copy link
Author

I've decided it is a lot easier to just copy the locales instead of trying to use Esbuild

@james-pre james-pre closed this as not planned Won't fix, can't repro, duplicate, stale Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants