mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-03-13 12:40:18 -05:00
feat: do not write undefined fields to config files (#3643)
closes #3058
This commit is contained in:
@@ -79,3 +79,29 @@ module.exports = {
|
||||
}`
|
||||
)
|
||||
})
|
||||
|
||||
test(`add a new undefined property`, () => {
|
||||
const value = {
|
||||
foo: undefined
|
||||
}
|
||||
const source = `module.exports = {
|
||||
bar: 123
|
||||
}`
|
||||
|
||||
expect(extend(value, source)).toMatch(source)
|
||||
})
|
||||
|
||||
test(`change an existing property to undefined`, () => {
|
||||
const value = {
|
||||
foo: undefined
|
||||
}
|
||||
const source = `module.exports = {
|
||||
foo: 123,
|
||||
bar: 456
|
||||
}`
|
||||
expect(extend(value, source)).toMatch(
|
||||
`module.exports = {
|
||||
bar: 456
|
||||
}`
|
||||
)
|
||||
})
|
||||
|
||||
@@ -45,13 +45,21 @@ module.exports = function extendJSConfig (value, source) {
|
||||
const props = valueAST.program.body[0].expression.properties
|
||||
const existingProps = node.properties
|
||||
for (const prop of props) {
|
||||
const isUndefinedProp =
|
||||
prop.value.type === 'Identifier' && prop.value.name === 'undefined'
|
||||
|
||||
const existing = existingProps.findIndex(p => {
|
||||
return !p.computed && p.key.name === prop.key.name
|
||||
})
|
||||
if (existing > -1) {
|
||||
// replace
|
||||
existingProps[existing].value = prop.value
|
||||
} else {
|
||||
|
||||
// remove `undefined` props
|
||||
if (isUndefinedProp) {
|
||||
existingProps.splice(existing, 1)
|
||||
}
|
||||
} else if (!isUndefinedProp) {
|
||||
// append
|
||||
existingProps.push(prop)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user