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

6.0.0-beta.x regression, array of data imported as JSON file get transformed to an object, why? #18533

Closed
7 tasks done
ghiscoding opened this issue Oct 31, 2024 · 3 comments · Fixed by #18541
Closed
7 tasks done
Labels
p4-important Violate documented behavior or significantly improves performance (priority) regression The issue only appears after a new release

Comments

@ghiscoding
Copy link
Contributor

Describe the bug

I have a JSON file with the following structure

[
  {"name":"Ethel Price","gender":"female","company":"Enersol","id":1,"category":{"id":1,"name":"Gold"}},
  {"name":"Claudine Neal","gender":"female","company":"Sealoud","id":2,"category":{"id":1,"name":"Gold"}},
]

when I import the file via an import, it is returning an array (good) of data in Vite 5.x but in 6.x it imports the same file as an object (bad) for unknown reason when I expect it to remain an array. This seems like a regression when comparing to Vite 5.x

Reproduction

https://stackblitz.com/edit/vitejs-vite-qwnzvw?file=main.js

Steps to reproduce

Just run the Stackblitz provided.

running the same Stackblitz with Vite 5.x I get this
image

but running with Vite 6.0-beta, I get this (notice the prototype changes to Object instead of Array)
image

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 1.71 GB / 15.85 GB
  Binaries:
    Node: 20.13.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.22 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 10.9.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.12.2 - C:\OwnCloudDev10\GitHub\slickgrid-universal\node_modules\.bin\pnpm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.19041.4355

Used Package Manager

pnpm

Logs

No response

Validations

@sapphi-red sapphi-red added p4-important Violate documented behavior or significantly improves performance (priority) regression The issue only appears after a new release labels Oct 31, 2024
@sapphi-red
Copy link
Member

It seems I broke it in #18303. For now, setting json.stringify: false would work.

@ghiscoding
Copy link
Contributor Author

ghiscoding commented Oct 31, 2024

Ah thanks for the info, I'm assuming that it's because of line 54 since pretty everything in JS is derived from object and that includes arrays. Perhaps the if condition should skip arrays?

if (options.stringify !== false) {
if (options.namedExports) {
const parsed = JSON.parse(json)
if (typeof parsed === 'object' && parsed != null) {
const keys = Object.keys(parsed)

Taking a look at the isObject npm package, with 48M download, they also check for array to know if it's an object or not

https://github.com/jonschlinkert/isobject/blob/master/index.js

export default function isObject(val) {
  return val != null && typeof val === 'object' && Array.isArray(val) === false;
};

@bluwy
Copy link
Member

bluwy commented Oct 31, 2024

We also have a isObject utility that we can use:

export function isObject(value: unknown): value is Record<string, any> {
return Object.prototype.toString.call(value) === '[object Object]'
}

@github-actions github-actions bot locked and limited conversation to collaborators Nov 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p4-important Violate documented behavior or significantly improves performance (priority) regression The issue only appears after a new release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants