Skip to content

Commit f1bdf73

Browse files
authored
refactor: simplify config loading by skip fs.existsSync check (vuejs#5305)
Use error code thrown by `require` directly. This also simplifies module mocking in unit test.
1 parent 5b1709a commit f1bdf73

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

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

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
jest.mock('fs')
2-
jest.mock('/vue.config.js', () => ({ lintOnSave: false }), { virtual: true })
32
jest.mock('vue-cli-plugin-foo', () => () => {}, { virtual: true })
43

54
const fs = require('fs')
@@ -125,32 +124,25 @@ test('keep publicPath when empty', () => {
125124
})
126125

127126
test('load project options from vue.config.js', () => {
128-
process.env.VUE_CLI_SERVICE_CONFIG_PATH = `/vue.config.js`
129-
fs.writeFileSync('/vue.config.js', `module.exports = { lintOnSave: false }`)
127+
jest.mock(path.resolve('/', 'vue.config.js'), () => ({ lintOnSave: false }), { virtual: true })
130128
mockPkg({
131129
vue: {
132130
lintOnSave: 'default'
133131
}
134132
})
135133
const service = createMockService()
136-
fs.unlinkSync('/vue.config.js')
137-
delete process.env.VUE_CLI_SERVICE_CONFIG_PATH
138134
// vue.config.js has higher priority
139135
expect(service.projectOptions.lintOnSave).toBe(false)
140136
})
141137

142-
test('load project options from vue.config.js', () => {
143-
process.env.VUE_CLI_SERVICE_CONFIG_PATH = `/vue.config.js`
144-
fs.writeFileSync('/vue.config.js', '') // only to ensure fs.existsSync returns true
138+
test('load project options from vue.config.js as a function', () => {
145139
jest.mock('/vue.config.js', () => function () { return { lintOnSave: false } }, { virtual: true })
146140
mockPkg({
147141
vue: {
148142
lintOnSave: 'default'
149143
}
150144
})
151145
const service = createMockService()
152-
fs.unlinkSync('/vue.config.js')
153-
delete process.env.VUE_CLI_SERVICE_CONFIG_PATH
154146
// vue.config.js has higher priority
155147
expect(service.projectOptions.lintOnSave).toBe(false)
156148
})

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

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const fs = require('fs')
21
const path = require('path')
32
const debug = require('debug')
43
const merge = require('webpack-merge')
@@ -305,21 +304,21 @@ module.exports = class Service {
305304
process.env.VUE_CLI_SERVICE_CONFIG_PATH ||
306305
path.resolve(this.context, 'vue.config.js')
307306
)
308-
if (fs.existsSync(configPath)) {
309-
try {
310-
fileConfig = require(configPath)
307+
try {
308+
fileConfig = require(configPath)
311309

312-
if (typeof fileConfig === 'function') {
313-
fileConfig = fileConfig()
314-
}
310+
if (typeof fileConfig === 'function') {
311+
fileConfig = fileConfig()
312+
}
315313

316-
if (!fileConfig || typeof fileConfig !== 'object') {
317-
error(
318-
`Error loading ${chalk.bold('vue.config.js')}: should export an object or a function that returns object.`
319-
)
320-
fileConfig = null
321-
}
322-
} catch (e) {
314+
if (!fileConfig || typeof fileConfig !== 'object') {
315+
error(
316+
`Error loading ${chalk.bold('vue.config.js')}: should export an object or a function that returns object.`
317+
)
318+
fileConfig = null
319+
}
320+
} catch (e) {
321+
if (e.code !== 'MODULE_NOT_FOUND') {
323322
error(`Error loading ${chalk.bold('vue.config.js')}:`)
324323
throw e
325324
}

0 commit comments

Comments
 (0)