Skip to content

Commit cb11397

Browse files
authored
feat: do not write undefined fields to config files (#3643)
closes #3058
1 parent a02ef39 commit cb11397

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/@vue/cli/lib/util/__tests__/extendJSConfig.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,29 @@ module.exports = {
7979
}`
8080
)
8181
})
82+
83+
test(`add a new undefined property`, () => {
84+
const value = {
85+
foo: undefined
86+
}
87+
const source = `module.exports = {
88+
bar: 123
89+
}`
90+
91+
expect(extend(value, source)).toMatch(source)
92+
})
93+
94+
test(`change an existing property to undefined`, () => {
95+
const value = {
96+
foo: undefined
97+
}
98+
const source = `module.exports = {
99+
foo: 123,
100+
bar: 456
101+
}`
102+
expect(extend(value, source)).toMatch(
103+
`module.exports = {
104+
bar: 456
105+
}`
106+
)
107+
})

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,21 @@ module.exports = function extendJSConfig (value, source) {
4545
const props = valueAST.program.body[0].expression.properties
4646
const existingProps = node.properties
4747
for (const prop of props) {
48+
const isUndefinedProp =
49+
prop.value.type === 'Identifier' && prop.value.name === 'undefined'
50+
4851
const existing = existingProps.findIndex(p => {
4952
return !p.computed && p.key.name === prop.key.name
5053
})
5154
if (existing > -1) {
5255
// replace
5356
existingProps[existing].value = prop.value
54-
} else {
57+
58+
// remove `undefined` props
59+
if (isUndefinedProp) {
60+
existingProps.splice(existing, 1)
61+
}
62+
} else if (!isUndefinedProp) {
5563
// append
5664
existingProps.push(prop)
5765
}

0 commit comments

Comments
 (0)