tettusud / merge-jsons-webpack-plugin

This plugin is used to merge json files into single json file,using glob or file names
https://npmjs.com/package/merge-jsons-webpack-plugin
Apache License 2.0
36 stars 20 forks source link

deep merge? #1

Closed leonardoporro closed 7 years ago

leonardoporro commented 7 years ago

Hi @tettusud, First of all, thanks for your plugin, it is very useful.

For my app, I need to merge several i18n json files, but internally they may refer to the same root. e.g.: validations.i18n.json: core: { validators: { required: "This field is required" }} commands.i18n.json: core: { commands: { ok: "OK", cancel: "Cancel" } }

If I merge that, the Core object is overwritten. I modified the plugin locally, by adding a function like this:

mergeDeep(target, source) { if (typeof target == "object" && typeof source == "object") { for (const key in source) { if (typeof source[key] == "object") { if (!target[key]) Object.assign(target, { [key]: {} }); this.mergeDeep(target[key], source[key]); } else { Object.assign(target, { [key]: source[key] }); } } } return target; }

Maybe it is useful to you. Best regard, Leo

tettusud commented 7 years ago

Thank you very much @leonardoporro for the feedback and also for code contributions. I have updated the code, you may take a look and review it. As am still using es5 i cannot use Object.assign as of now , so I directly added key to target instead of Obect assign (since its just json for now) I can clean it up later.