thur
thur

Reputation: 1074

How can I generate or import multiple json files in one, with webpack?

I have multiple json files, and want to transform they to one simple json file and use json-loader to require it, like this:

import dictionary from './distFile.json';

Example:
file1.json
{a:"b"}
file2.json
{b:"a"}
distFile.json should be:
{a:"b",b:"a"}

How can I do that?

Or there is anyway to do something like that:
import dictionary from ['./file1.json','./file2.json']; ??

Upvotes: 4

Views: 5726

Answers (1)

Andy Ray
Andy Ray

Reputation: 32056

Make a third file that you require both files in.

combined.js

import file1 from './file1.json';
import file2 from './file2.json';
export default { ...file1, ...file2 };

Then just import that file:

import combinedJson from './combined.js'

Upvotes: 10

Related Questions