Skip to content

Commit f5b174f

Browse files
authored
feat: allow vue.config.js to return a function (#3499)
closes #3213
1 parent 8562d3e commit f5b174f

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

packages/@vue/cli-service/__tests__/Service.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ test('load project options from vue.config.js', () => {
154154
expect(service.projectOptions.lintOnSave).toBe(false)
155155
})
156156

157+
test('load project options from vue.config.js', () => {
158+
process.env.VUE_CLI_SERVICE_CONFIG_PATH = `/vue.config.js`
159+
fs.writeFileSync('/vue.config.js', '') // only to ensure fs.existsSync returns true
160+
jest.mock('/vue.config.js', () => function () { return { lintOnSave: false } }, { virtual: true })
161+
mockPkg({
162+
vue: {
163+
lintOnSave: true
164+
}
165+
})
166+
const service = createMockService()
167+
fs.unlinkSync('/vue.config.js')
168+
delete process.env.VUE_CLI_SERVICE_CONFIG_PATH
169+
// vue.config.js has higher priority
170+
expect(service.projectOptions.lintOnSave).toBe(false)
171+
})
172+
157173
test('api: registerCommand', () => {
158174
let args
159175
const service = createMockService([{

packages/@vue/cli-service/lib/Service.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,14 @@ module.exports = class Service {
281281
if (fs.existsSync(configPath)) {
282282
try {
283283
fileConfig = require(configPath)
284+
285+
if (typeof fileConfig === 'function') {
286+
fileConfig = fileConfig()
287+
}
288+
284289
if (!fileConfig || typeof fileConfig !== 'object') {
285290
error(
286-
`Error loading ${chalk.bold('vue.config.js')}: should export an object.`
291+
`Error loading ${chalk.bold('vue.config.js')}: should export an object or a function that returns object.`
287292
)
288293
fileConfig = null
289294
}

packages/@vue/cli-service/types/ProjectOptions.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,5 @@ export interface ProjectOptions {
5656

5757
pluginOptions?: object;
5858
}
59+
60+
export type ConfigFunction = () => ProjectOptions
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { ProjectOptions } from './ProjectOptions'
1+
export { ProjectOptions, ConfigFunction } from './ProjectOptions'

0 commit comments

Comments
 (0)