Skip to content

Commit 281c799

Browse files
authored
feat: generate vue.config.js with defineConfig wrapper (#6817)
1 parent ae95731 commit 281c799

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

packages/@vue/cli/__tests__/Generator.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,9 @@ test('api: addConfigTransform transform vue warn', async () => {
10121012
extractConfigFiles: true
10131013
})
10141014

1015-
expect(fs.readFileSync('/vue.config.js', 'utf-8')).toMatch(`module.exports = {\n lintOnSave: 'default'\n}`)
1015+
expect(fs.readFileSync('/vue.config.js', 'utf-8')).toMatch(
1016+
`const { defineConfig } = require('@vue/cli-service')\nmodule.exports = defineConfig({\n lintOnSave: 'default'\n})\n`
1017+
)
10161018
expect(logs.warn.some(([msg]) => {
10171019
return msg.match(/Reserved config transform 'vue'/)
10181020
})).toBe(true)
@@ -1103,7 +1105,9 @@ test('extract config files', async () => {
11031105
})
11041106

11051107
const js = v => `module.exports = ${stringifyJS(v, null, 2)}`
1106-
expect(fs.readFileSync('/vue.config.js', 'utf-8')).toMatch(js(configs.vue))
1108+
expect(fs.readFileSync('/vue.config.js', 'utf-8')).toMatch(
1109+
`const { defineConfig } = require('@vue/cli-service')\nmodule.exports = defineConfig(${stringifyJS(configs.vue, null, 2)})`
1110+
)
11071111
expect(fs.readFileSync('/babel.config.js', 'utf-8')).toMatch(js(configs.babel))
11081112
expect(fs.readFileSync('/postcss.config.js', 'utf-8')).toMatch(js(configs.postcss))
11091113
expect(fs.readFileSync('/.eslintrc.js', 'utf-8')).toMatch(js(configs.eslintConfig))

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ test(`basic`, () => {
2828
)
2929
})
3030

31+
test(`defineConfig`, () => {
32+
const value = {
33+
foo: true,
34+
css: {
35+
modules: true
36+
}
37+
}
38+
const source =
39+
`const { defineConfig } = require('@vue/cli-service')
40+
module.exports = defineConfig({
41+
foo: false,
42+
css: {
43+
modules: false
44+
}
45+
})`
46+
expect(extend(value, source)).toMatch(
47+
`const { defineConfig } = require('@vue/cli-service')
48+
module.exports = defineConfig({
49+
foo: true,
50+
css: {
51+
modules: true
52+
}
53+
})`
54+
)
55+
})
56+
3157
test(`adding new property`, () => {
3258
const value = {
3359
foo: true

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const transformJS = {
1818
return null
1919
}
2020
},
21-
write: ({ value, existing, source }) => {
21+
write: ({ value, existing, source, filename }) => {
2222
if (existing) {
2323
// We merge only the modified keys
2424
const changedData = {}
@@ -34,6 +34,11 @@ const transformJS = {
3434
}
3535
})
3636
return extendJSConfig(changedData, source)
37+
} else if (filename === 'vue.config.js') {
38+
return (
39+
`const { defineConfig } = require('@vue/cli-service')\n` +
40+
`module.exports = defineConfig(${stringifyJS(value, null, 2)})`
41+
)
3742
} else {
3843
return `module.exports = ${stringifyJS(value, null, 2)}`
3944
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,24 @@ module.exports = function extendJSConfig (value, source) {
1414
node.left.object.name === 'module' &&
1515
node.left.property.name === 'exports'
1616
) {
17-
if (node.right.type === 'ObjectExpression') {
18-
augmentExports(node.right)
19-
} else if (node.right.type === 'Identifier') {
17+
let theExports = node.right
18+
if (
19+
theExports.type === 'CallExpression' &&
20+
theExports.callee.type === 'Identifier' &&
21+
theExports.callee.name === 'defineConfig'
22+
) {
23+
theExports = theExports.arguments[0]
24+
}
25+
26+
if (theExports.type === 'ObjectExpression') {
27+
augmentExports(theExports)
28+
} else if (theExports.type === 'Identifier') {
2029
// do a second pass
21-
exportsIdentifier = node.right.name
30+
exportsIdentifier = theExports.name
2231
}
2332
return false
2433
}
34+
2535
this.traverse(path)
2636
}
2737
})

0 commit comments

Comments
 (0)