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

Import Json is not exposing properties as exports #3171

Closed
zekth opened this issue Oct 21, 2019 · 5 comments · Fixed by #3210
Closed

Import Json is not exposing properties as exports #3171

zekth opened this issue Oct 21, 2019 · 5 comments · Fixed by #3210

Comments

@zekth
Copy link
Contributor

zekth commented Oct 21, 2019

Importing Json files only works this way:

{
  "foo": "bar"
}

this works:

import file from './foo.json'
console.log(file.foo)

this does not

import { foo } from './foo.json'
console.log(foo)
@kitsonk
Copy link
Contributor

kitsonk commented Oct 21, 2019

Why you say "does not work", what sort of error message do you get?

@kevinkassimo
Copy link
Contributor

kevinkassimo commented Oct 21, 2019

This is because our current json import is through a wrapper looking like

export default { "your_json_field": 1 };

A problem that might arise if we somehow allow import { prop_name } from "./file.json" is that prop_name might be a reserved word or contains space.

@zekth
Copy link
Contributor Author

zekth commented Oct 21, 2019

This is because our current json import is through a wrapper looking like

export default { "your_json_field": 1 };

A problem that might arise if we somehow allow import { prop_name } from "./file.json" is that prop_name might be a reserved word or contains space.

Agreed but rollup / webpack for example does not care of this for example. The output will crash and that's normal. But the output error might be odd.

@kitsonk
Copy link
Contributor

kitsonk commented Oct 21, 2019

Agreed but rollup / webpack for example does not care of this for example.

They also don't output ESM. They output a script bundle. ES modules don't have an "export assignment" operator. There is no clean way to write it so that the whole of the JSON structure is made available. It is unlikely that rollup or webpack would just throw, because they don't have the same challenge, because they write it out in a special bundle format that isn't ESM and then wrap that in some sort of loader mechanism.

I don't think "just failing" would be a good idea. All the following are valid .json that we would expect to be able to import:

[ "foo", "bar" ]
"foo"
1
true

While the primitives are unlikely, I don't think it is safe to assume an array at the top level would be unlikely, and it would be bad to not support that. So while I hate default exports, they are actually the only safe way to expose JSON as an ES module.

@zekth
Copy link
Contributor Author

zekth commented Oct 22, 2019

@kitsonk i get your point about those case you mention. So assuming that the loading method for Json files is using only default exports, this might need to be documented. Like i said, other projets don't have the same implementation, so it could be confusing for users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants