-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
Copy pathresolveUserConfig.js
83 lines (74 loc) · 2.09 KB
/
resolveUserConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const path = require('path')
const { chalk, warn, error } = require('@vue/cli-shared-utils')
const { validate } = require('../options')
function ensureSlash (config, key) {
const val = config[key]
if (typeof val === 'string') {
config[key] = val.replace(/([^/])$/, '$1/')
}
}
function removeSlash (config, key) {
if (typeof config[key] === 'string') {
config[key] = config[key].replace(/\/$/g, '')
}
}
module.exports = function resolveUserConfig ({
inlineOptions,
pkgConfig,
fileConfig,
fileConfigPath
}) {
if (fileConfig) {
if (typeof fileConfig === 'function') {
fileConfig = fileConfig()
}
if (!fileConfig || typeof fileConfig !== 'object') {
throw new Error(
`Error loading ${chalk.bold(fileConfigPath)}: ` +
`should export an object or a function that returns object.`
)
}
}
// package.vue
if (pkgConfig && typeof pkgConfig !== 'object') {
throw new Error(
`Error loading Vue CLI config in ${chalk.bold(`package.json`)}: ` +
`the "vue" field should be an object.`
)
}
let resolved, resolvedFrom
if (fileConfig) {
const configFileName = path.basename(fileConfigPath)
if (pkgConfig) {
warn(
`"vue" field in package.json ignored ` +
`due to presence of ${chalk.bold(configFileName)}.`
)
warn(
`You should migrate it into ${chalk.bold(configFileName)} ` +
`and remove it from package.json.`
)
}
resolved = fileConfig
resolvedFrom = configFileName
} else if (pkgConfig) {
resolved = pkgConfig
resolvedFrom = '"vue" field in package.json'
} else {
resolved = inlineOptions || {}
resolvedFrom = 'inline options'
}
// normalize some options
if (resolved.publicPath !== 'auto') {
ensureSlash(resolved, 'publicPath')
}
if (typeof resolved.publicPath === 'string') {
resolved.publicPath = resolved.publicPath.replace(/^\.\//, '')
}
removeSlash(resolved, 'outputDir')
// validate options
validate(resolved, msg => {
error(`Invalid options in ${chalk.bold(resolvedFrom)}: ${msg}`)
})
return resolved
}