loadFileConfig.js 920 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const fs = require('fs')
  2. const path = require('path')
  3. const { pathToFileURL } = require('url')
  4. const isFileEsm = require('is-file-esm')
  5. const { loadModule } = require('@vue/cli-shared-utils')
  6. module.exports = function loadFileConfig (context) {
  7. let fileConfig, fileConfigPath
  8. const possibleConfigPaths = [
  9. process.env.VUE_CLI_SERVICE_CONFIG_PATH,
  10. './vue.config.js',
  11. './vue.config.cjs',
  12. './vue.config.mjs'
  13. ]
  14. for (const p of possibleConfigPaths) {
  15. const resolvedPath = p && path.resolve(context, p)
  16. if (resolvedPath && fs.existsSync(resolvedPath)) {
  17. fileConfigPath = resolvedPath
  18. break
  19. }
  20. }
  21. if (fileConfigPath) {
  22. const { esm } = isFileEsm.sync(fileConfigPath)
  23. if (esm) {
  24. fileConfig = import(pathToFileURL(fileConfigPath))
  25. } else {
  26. fileConfig = loadModule(fileConfigPath, context)
  27. }
  28. }
  29. return {
  30. fileConfig,
  31. fileConfigPath
  32. }
  33. }