Case I:
I have a file called data.json
{
"a": 1,
"b": 2,
...
}
Which I imported/required in index.js
as shown below
import data from './data.json';
console.log(data["a"]);
Everything works fine but the whole data.json is dumped into index.js when bundled with webpack hence file size increased with property values I don't need.
Case II:
However, if I do the following in index.js
const a = {
"a": 1,
"b": 2,
...
}
console.log(data["a"]);
Everything is fine (The value 1 get logged) and no unnecessary properties of the json variable a were dumped. I think webpack's Tree Shaking is working here (I'm not sure)
My Question: How can I achieve case II (no unnecessary properties) by requiring/importing the file like it's done in case I?
I was thinking if there's a way to treat the content of require as a value and not a dependency so webpack or any other bundler doesn't include it in the bundled file.
PS: I'm bundling for WEB