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

FR: tree shake JSON imports #4000

Open
Conaclos opened this issue Dec 9, 2024 · 0 comments
Open

FR: tree shake JSON imports #4000

Conaclos opened this issue Dec 9, 2024 · 0 comments

Comments

@Conaclos
Copy link

Conaclos commented Dec 9, 2024

As indicated in the ESBuild documentation, ESbuild is able to tree shake a JSON import when we use named imports.
Unfortunately, named imports for JSON files are now an outdated feature in JS engines.
A JSON file must now be imported as a default import and with an import attribute.

ESbuild could recognize standard JSON imports and tree shake them.

Given the following code:

import packageJson from "./package.json" with { type: "json" };
export const VERSION = packageJson.version;

And assuming that version is set to 1.0.0 in package.json, ESbuild could output the following bundled code:

export const VERSION = "1.0.0";
Conaclos added a commit to bare-ts/bare that referenced this issue Dec 26, 2024
Instead of relying on esbuild to set the package version of the CLI,
we now load it from `VERSION.json`.
`scipts/version.sh` and thus `npm version` update `VERSION.json`.

We could directly load `package.json`,
unfortunately ESbuild is not able to tree-shake JSON files.
I opened the following issue: evanw/esbuild#4000

We have to require NodeJS 20.10.0 or above for import attributes.
Conaclos added a commit to bare-ts/bare that referenced this issue Dec 26, 2024
Instead of relying on esbuild to set the package version of the CLI,
we now load it from `VERSION.json`.
`scipts/version.sh` and thus `npm version` update `VERSION.json`.

We could directly load `package.json`,
unfortunately ESbuild is not able to tree-shake JSON files.
I opened the following issue: evanw/esbuild#4000

We have to require NodeJS 20.10.0 or above for import attributes.
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

1 participant