Skip to content

Commit 35cb714

Browse files
author
Guillaume Chau
committed
fix(invoke): merge data in config transforms
1 parent 49d56db commit 35cb714

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

packages/@vue/cli/lib/Generator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ module.exports = class Generator {
8282
const res = transform(
8383
value,
8484
checkExisting,
85-
this.files
85+
this.files,
86+
this.context
8687
)
8788
const { content, filename } = res
8889
this.files[filename] = content

packages/@vue/cli/lib/util/configTransforms.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
const extendJSConfig = require('./extendJSConfig')
22
const stringifyJS = require('./stringifyJS')
3+
const { loadModule } = require('./module')
4+
const merge = require('deepmerge')
35

46
function makeJSTransform (filename) {
5-
return function transformToJS (value, checkExisting, files) {
7+
return function transformToJS (value, checkExisting, files, context) {
68
if (checkExisting && files[filename]) {
9+
// Merge data
10+
let changedData = {}
11+
try {
12+
const originalData = loadModule(filename, context, true)
13+
// We merge only the modified keys
14+
Object.keys(value).forEach(key => {
15+
changedData[key] = merge(originalData[key], value[key])
16+
})
17+
} catch (e) {
18+
changedData = value
19+
}
20+
// Write
721
return {
822
filename,
9-
content: extendJSConfig(value, files[filename])
23+
content: extendJSConfig(changedData, files[filename])
1024
}
1125
} else {
1226
return {
@@ -23,7 +37,7 @@ function makeJSONTransform (filename) {
2337
if (checkExisting && files[filename]) {
2438
existing = JSON.parse(files[filename])
2539
}
26-
value = Object.assign(existing, value)
40+
value = merge(existing, value)
2741
return {
2842
filename,
2943
content: JSON.stringify(value, null, 2)
@@ -32,10 +46,10 @@ function makeJSONTransform (filename) {
3246
}
3347

3448
function makeMutliExtensionJSONTransform (filename, preferJS) {
35-
return function transformToMultiExtensions (value, checkExisting, files) {
49+
return function transformToMultiExtensions (value, checkExisting, files, context) {
3650
function defaultTransform () {
3751
if (preferJS) {
38-
return makeJSTransform(`${filename}.js`)(value, false, files)
52+
return makeJSTransform(`${filename}.js`)(value, false, files, context)
3953
} else {
4054
return makeJSONTransform(filename)(value, false, files)
4155
}
@@ -50,7 +64,7 @@ function makeMutliExtensionJSONTransform (filename, preferJS) {
5064
} else if (files[`${filename}.json`]) {
5165
return makeJSONTransform(`${filename}.json`)(value, checkExisting, files)
5266
} else if (files[`${filename}.js`]) {
53-
return makeJSTransform(`${filename}.js`)(value, checkExisting, files)
67+
return makeJSTransform(`${filename}.js`)(value, checkExisting, files, context)
5468
} else if (files[`${filename}.yaml`]) {
5569
return transformYAML(value, `${filename}.yaml`, files[`${filename}.yaml`])
5670
} else if (files[`${filename}.yml`]) {
@@ -66,7 +80,7 @@ function transformYAML (value, filename, source) {
6680
const existing = yaml.safeLoad(source)
6781
return {
6882
filename,
69-
content: yaml.safeDump(Object.assign(existing, value))
83+
content: yaml.safeDump(merge(existing, value))
7084
}
7185
}
7286

0 commit comments

Comments
 (0)