Skip to content

Values get separated from object when importing JSON. #20305

Closed
@bitfexl

Description

@bitfexl

Describe the bug

When importing JSON like this:

import json from "./data.json";
document.querySelector("#app").innerHTML = JSON.stringify(json);
{
    "a": "valueA",
    "bcd_EFG": "valueB",
    "x y z": "valueXYZ"
}

the output of vite build --minify false is:

const a = "valueA";
const bcd_EFG = "valueB";
const json = {
  a,
  bcd_EFG,
  "x y z": "valueXYZ"
};
document.querySelector("#app").innerHTML = JSON.stringify(json);

With minification terser is able to inline the variables again, esbuild is not.

However if the JSON "object" is an array the output of vite build --minify false is:

const json = [
  {
    a: "valueA",
    bcd_EFG: "valueB",
    "x y z": "valueXYZ"
  }
];
document.querySelector("#app").innerHTML = JSON.stringify(json);

Why does Vite import a JSON object so weirdly in the first case? Is this a bug?

Reproduction

https://stackblitz.com/edit/vitejs-vite-cqp5np6f?file=src%2Fmain.js

Steps to reproduce

see above examples

System Info

vite/7.0.0 also happens on version 5

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions