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

Glob imports do not work on JSONs with spaces in the keys #14245

Closed
7 tasks done
Kerryliu opened this issue Aug 30, 2023 · 2 comments
Closed
7 tasks done

Glob imports do not work on JSONs with spaces in the keys #14245

Kerryliu opened this issue Aug 30, 2023 · 2 comments
Labels
duplicate This issue or pull request already exists

Comments

@Kerryliu
Copy link

Describe the bug

When using import.meta.glob('./*.json', { eager: true }); to import a json like the following, I am unable to access the second key-value. I'm not sure if it's related to the space in the key, but removing it allows the key to be accessed as expected.

{
  "hello": "how are you",
  "hello world": "this doesn't work"
}

The first one is accessible via glob["./sample.json"]["hello"], however glob["./sample.json"]["hello world"] returns undefined.


Importing the json with the options {eager: true, as: "raw"} and then grabbing the json via JSON.parse() on the raw string is how I'm currently sidestepping this problem.

Reproduction

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

Steps to reproduce

No response

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.20.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.6.10 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^4.4.9 => 4.4.9

Used Package Manager

npm

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented Aug 30, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sapphi-red
Copy link
Member

hello world is not a valid identifier and therefore it wasn't possible to declare a named export like that.
Thanks to the "arbitrary module namespace identifier names" feature, it's now possible but it's not supported yet.

You can access it by glob["./sample.json"].default["hello world"] as a workaround.
Or you can use import.meta.glob('./*.json', { eager: true, import: 'default' }).

Closing as a duplicate of #11359

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Aug 31, 2023
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Aug 31, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Sep 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants