resolveUserConfig.js 2.1 KB

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