JSON loader to ES6 imports [feature request]

Do you want to request a feature or report a bug?
Feature: convert JSON imports to ES6 imports by default.

What is the current behavior?

import { name } from 'package.json';

Here you get the name variable from package.json, but since it was not converted to ES6 imports first you will not achieve tree shaking and any babel feature on it: in the transpiled and minified source you can see the full package.json even if only the name was imported.

What is the expected behavior?
Any import name from 'filename.json' should transform the latter in a set of ES6 “export” statements, one for each first-level key found (nothing else is needed since destructuring imports is not allowed anyway in ES6).
If user is not “namely importing” then assume import * as name to get the full file like the current behaviour (backward-compatible).
If the user is “namely importing” then convert the JSON file to ES6 export statements to allow tree shaking.

If this is a feature request, what is motivation or use case for changing the behavior?
I do not see why webpack should not apply this optimisation: whether the json file is big or small we can apply the same optimisation (thanks to loaders) of every other JS file.

Author: Fantashit

3 thoughts on “JSON loader to ES6 imports [feature request]

  1. @jkrems it suffers the same data synchronization issue that I’d like to avoid:

    // {"x": 1}
    import {x}, root from 'json';
    console.log(x) // 1
    root.x = 2
    console.log(x) // 1
    console.log(root.x) // 2

    If that is solved so they stay in sync some way (freezing the target or setting up some form of proxy), seems fine. Curious what to do when default is defined. Older EPs stated that default would always point to the root of the target even if the object had a .default property.

Comments are closed.