fix(invoke): deep merge only plain objects

This commit is contained in:
Guillaume Chau
2018-06-18 10:29:33 +02:00
parent 35cb714b30
commit a7f3c2cc45
+11 -1
View File
@@ -3,6 +3,8 @@ const stringifyJS = require('./stringifyJS')
const { loadModule } = require('./module')
const merge = require('deepmerge')
const isObject = val => val && typeof val === 'object'
function makeJSTransform (filename) {
return function transformToJS (value, checkExisting, files, context) {
if (checkExisting && files[filename]) {
@@ -12,7 +14,15 @@ function makeJSTransform (filename) {
const originalData = loadModule(filename, context, true)
// We merge only the modified keys
Object.keys(value).forEach(key => {
changedData[key] = merge(originalData[key], value[key])
const originalValue = originalData[key]
const newValue = value[key]
if (Array.isArray(newValue)) {
changedData[key] = newValue
} else if (isObject(originalValue) && isObject(newValue)) {
changedData[key] = merge(originalValue, newValue)
} else {
changedData[key] = newValue
}
})
} catch (e) {
changedData = value