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

Dynamic Import of JSON File skips import of Keys with Fullstop/Dot (.) #11359

Open
7 tasks done
fritzfelix opened this issue Dec 13, 2022 · 4 comments
Open
7 tasks done
Labels
bug: upstream Bug in a dependency of Vite enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@fritzfelix
Copy link

fritzfelix commented Dec 13, 2022

Describe the bug

When dynamiclly importing a JSON file that contains keys with fullstops in it (i.e. foo.bar), these key/value pairs are not imported but skipped:

de.json:

{
    "foo": "bar",
    "foo.bar": "baz"
}

Import:

const thing = await import(`./translations/${locale}.json`);
console.log(thing);

Output:
It can be seen, that "foo.bar" only exists under "default":

image

Expected Output (previously working with Webpack Dynamic Import):
"foo.bar" exists at top level:

image

Reproduction

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

Steps to reproduce

No response

System Info

System:
    OS: macOS 13.0.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 62.20 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.12.1 - /opt/homebrew/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.19.2 - /opt/homebrew/bin/npm
  Browsers:
    Brave Browser: 107.1.45.113
    Chrome: 108.0.5359.98
    Firefox Developer Edition: 108.0
    Safari: 16.1

Used Package Manager

yarn

Logs

No response

Validations

@sapphi-red
Copy link
Member

This is not a bug. foo.bar is not a valid name for export.

export const foo.bar = '' // this is not a valid js syntax

If we use "Arbitrary module namespace identifier names" that landed in ES2022, we could support this:

const foo_bar = ''

export { foo_bar as 'foo.bar' }

But this is not supported by rollup yet (rollup/rollup#4322).

@sapphi-red sapphi-red added enhancement New feature or request bug: upstream Bug in a dependency of Vite p2-nice-to-have Not breaking anything but nice to have (priority) and removed pending triage labels Dec 13, 2022
@sapphi-red sapphi-red removed the bug: upstream Bug in a dependency of Vite label Dec 28, 2022
@LeoDog896 LeoDog896 mentioned this issue Jan 16, 2023
8 tasks
@steventilator
Copy link

I am facing a similar issue. I have a JSON file with translations. Some keys contain spaces. For example: "Add employee": "Mitarbeiter hinzufügen". These keys are not included in the imported JSON file.

How can I fix it?

@steventilator
Copy link

Ha, I just fixed it. For future reference:

Does not work:

import * as de from './locales/de.json'

Works:

import de from './locales/de.json'

@jonahgoldwastaken
Copy link

Hey, just checking in to ask what the progress is on fixing this? We just migrated to Vite and this is causing a big regression in the way we translate our app. We've implemented a workaround but it's less than pretty and would like to be able to just dynamically import the JSON files directly.

@bluwy bluwy added the bug: upstream Bug in a dependency of Vite label Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug: upstream Bug in a dependency of Vite enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
Status: Has plan
Development

No branches or pull requests

5 participants