Skip to content

Commit

Permalink
feat: allow vue.config.js to return a function (#3499)
Browse files Browse the repository at this point in the history
closes #3213
  • Loading branch information
sodatea committed Feb 28, 2019
1 parent 8562d3e commit f5b174f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
16 changes: 16 additions & 0 deletions packages/@vue/cli-service/__tests__/Service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ test('load project options from vue.config.js', () => {
expect(service.projectOptions.lintOnSave).toBe(false)
})

test('load project options from vue.config.js', () => {
process.env.VUE_CLI_SERVICE_CONFIG_PATH = `/vue.config.js`
fs.writeFileSync('/vue.config.js', '') // only to ensure fs.existsSync returns true
jest.mock('/vue.config.js', () => function () { return { lintOnSave: false } }, { virtual: true })
mockPkg({
vue: {
lintOnSave: true
}
})
const service = createMockService()
fs.unlinkSync('/vue.config.js')
delete process.env.VUE_CLI_SERVICE_CONFIG_PATH
// vue.config.js has higher priority
expect(service.projectOptions.lintOnSave).toBe(false)
})

test('api: registerCommand', () => {
let args
const service = createMockService([{
Expand Down
7 changes: 6 additions & 1 deletion packages/@vue/cli-service/lib/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,14 @@ module.exports = class Service {
if (fs.existsSync(configPath)) {
try {
fileConfig = require(configPath)

if (typeof fileConfig === 'function') {
fileConfig = fileConfig()
}

if (!fileConfig || typeof fileConfig !== 'object') {
error(
`Error loading ${chalk.bold('vue.config.js')}: should export an object.`
`Error loading ${chalk.bold('vue.config.js')}: should export an object or a function that returns object.`
)
fileConfig = null
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@vue/cli-service/types/ProjectOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ export interface ProjectOptions {

pluginOptions?: object;
}

export type ConfigFunction = () => ProjectOptions
2 changes: 1 addition & 1 deletion packages/@vue/cli-service/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { ProjectOptions } from './ProjectOptions'
export { ProjectOptions, ConfigFunction } from './ProjectOptions'

0 comments on commit f5b174f

Please sign in to comment.