nodejs / node-eps

Node.js Enhancement Proposals for discussion on future API additions/changes to Node core
443 stars 66 forks source link

Importing json #34

Closed kentcdodds closed 7 years ago

kentcdodds commented 8 years ago

@jdalton invited me to file this issue. I didn't see anything in the proposal about JSON support. Will importing JSON be supported?

import data from './data.json'

Due to the fact that JSON is static, I think it'd be reasonable to use ImportSpecifiers as well. One drawback is it could be confusing. But in scenarios where the JSON file is large, tree shaking could be helpful in this scenario.

Here's an example:

data.json

{  
  "gender":"male",
  "name":{  
    "title":"mr",
    "first":"isaac",
    "last":"sanz"
  },
  "location":{  
    "street":"5286 calle de arturo soria",
    "city":"logroño",
    "state":"castilla y león",
    "postcode":50435
  },
  "email":"isaac.sanz@example.com",
  "login":{  
    "username":"organicduck350",
    "password":"jazzman",
    "salt":"gFH7zlZ9",
    "md5":"b284fbb639b2cb23493ab2ac0263ca01",
    "sha1":"7d7715926ce1f7dff3bef56c4c96a8f8daee5197",
    "sha256":"6e9961e11e17ee33a62545de397af1de4d5fda93d913956d2b0ee8aa75fcd774"
  },
  "registered":924488158,
  "dob":870811843,
  "phone":"994-654-870",
  "cell":"632-172-267",
  "id":{  
    "name":"DNI",
    "value":"21433930-U"
  },
  "picture":{  
    "large":"https://randomuser.me/api/portraits/men/29.jpg",
    "medium":"https://randomuser.me/api/portraits/med/men/29.jpg",
    "thumbnail":"https://randomuser.me/api/portraits/thumb/men/29.jpg"
  },
  "nat":"ES"
}

index.js

import {picture} from './data.json'

I think that this would be reasonable. As cool as it would be to have a destructuring-like syntax to get a specific picture (large for example), I don't think that's really in the cards here.

All of that said, tree-shaking is really a bundler concern and I don't think that Node proper really needs to concern itself with that. So honestly, just getting the entire parsed JS object as a default export which could then be destructured as normal would be fantastic.

kentcdodds commented 8 years ago

ping for those watching the repo. I've updated the issue with more info.

bmeck commented 8 years ago

yes, it will be subject to the interop bridge since it is a single value. This weeks CTC will have an update on the bridge discussing why we can't do hoisting. (was on vacation last week, sorry)

sam-github commented 7 years ago

@bmeck any update on this?

bmeck commented 7 years ago

@sam-github still going to be same bridge as CJS, closable?

sam-github commented 7 years ago

I don't know, I was hoping to read the link giving more context ;-).