Open SegaraRai opened 1 month ago
Maybe duplicate of https://github.com/vitejs/vite/issues/14674, at least I noticed the same bug that vite incorrectly strips import assertions during compilation against a node18
target. esbuild alone preserves it fine with a node18
target.
In my case, I feel the problem in processing vite.config.ts, not in the bundling of app itself. Config files are bundled in a different manner than the app itself, and their settings are hard-coded.
BTW, there seems to be no doubt that esbuild is erasing import attributes.
target: ["node18"]
: https://esbuild.github.io/try/#dAAwLjIxLjMALS10YXJnZXQ9Im5vZGUxOCIAaW1wb3J0IGRhdGEgZnJvbSAiZXh0ZXJuYWwuanNvbiIgd2l0aCB7IHR5cGU6ICJqc29uIiB9target: ["node22"]
: https://esbuild.github.io/try/#dAAwLjIxLjMALS10YXJnZXQ9Im5vZGUyMiIAaW1wb3J0IGRhdGEgZnJvbSAiZXh0ZXJuYWwuanNvbiIgd2l0aCB7IHR5cGU6ICJqc29uIiB9BTW, there seems to be no doubt that esbuild is erasing import attributes.
That looks like a bug in esbuild. They were initially introduced in node 21 but recently backported into 18 and 20. I opened https://github.com/evanw/esbuild/issues/3778.
Describe the bug
EDIT: I thought I was trying to switch between Node.js versions using Volta, but in fact when I called vite from pnpm it was fixed at Node v22. I tried again with v20 and found no problem when using import assertions, so I have corrected the title. Import assertions have been disabled since Node v22.0.0, so we can import JSON up to Node v20 or v21 with no problem if we use the conventional notation:
assert { type: "json" }
.I am trying to use
@iconify-json/meteocons
in my Vite plugin.I tried the following import methods, but none of them worked:
Those will fail with one of the following errors:
or
I believe that importing JSON in ESM style config files is no longer possible in Node.js v22 because Node.js v22 only supports import attributes instead of deprecated import assertions, and because esbuild removes import attributes under certain conditions (at least with
target: ["node18"]
).I have verified that by patching the following line to
target: ["node22"]
, it works correctly for both Node v20 and Node v22. https://github.com/vitejs/vite/blob/e7d38ab1c45b9d17f182f89d0c129932e2f994eb/packages/vite/src/node/config.ts#L1077Reproduction
https://github.com/SegaraRai/vite-config-import-json
Steps to reproduce
Run
pnpm i
followed bypnpm build
with Node.js v22. An error log is also available in GitHub Actions.System Info
Used Package Manager
pnpm
Logs
Click to expand! - ERR_IMPORT_ATTRIBUTE_MISSING version
```plaintext > vite build "--debug" failed to load config from ...\vite-config-import-json\vite.config.ts error during build: TypeError [ERR_IMPORT_ATTRIBUTE_MISSING]: Module "file:///.../vite-config-import-json/node_modules/.pnpm/@iconify-json+meteocons@1.1.4/node_modules/@iconify-json/meteocons/icons.json" needs an import attribute of "type: json" at validateAttributes (node:internal/modules/esm/assert:88:15) at defaultLoad (node:internal/modules/esm/load:153:3) at async ModuleLoader.load (node:internal/modules/esm/loader:541:7) at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:422:45) ELIFECYCLE Command failed with exit code 1. ```Click to expand! - SyntaxError version
```plaintext > vite build "--debug" failed to load config from ...\vite-config-import-json\vite.config.ts error during build: SyntaxError: Unexpected identifier 'assert' at compileSourceTextModule (node:internal/modules/esm/utils:337:16) at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:166:18) at callTranslator (node:internal/modules/esm/loader:416:14) at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:422:30) at async link (node:internal/modules/esm/module_job:88:21) ELIFECYCLE Command failed with exit code 1. ```Validations