load-babel-config.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const findBabelConfig = require('find-babel-config')
  2. const logger = require('./logger')
  3. const cache = require('./cache')
  4. const deprecate = require('./deprecate')
  5. const path = require('path')
  6. const { readFileSync, existsSync } = require('fs')
  7. module.exports = function getBabelConfig (vueJestConfig, filePath) {
  8. const find = () => {
  9. const { file, config } = findBabelConfig.sync(filePath || process.cwd())
  10. if (!file) {
  11. logger.info('no .babelrc found, skipping babel compilation')
  12. cache.set('babel-config', false)
  13. return
  14. }
  15. return config
  16. }
  17. const cachedConfig = cache.get('babel-config')
  18. if (cachedConfig) {
  19. return cachedConfig
  20. } else if (cachedConfig === false) {
  21. return
  22. } else {
  23. let babelConfig
  24. if (vueJestConfig.babelRcFile) {
  25. deprecate.replace('babelRcFile', 'babelConfig')
  26. babelConfig = JSON.parse(readFileSync(vueJestConfig.babelRcFile))
  27. } else if (vueJestConfig.hasOwnProperty('babelConfig')) {
  28. switch (typeof vueJestConfig.babelConfig) {
  29. case 'string':
  30. // a path to a config file is being passed in; load it
  31. babelConfig = require(vueJestConfig.babelConfig)
  32. break
  33. case 'boolean':
  34. // if babelConfig is true, search for it. If false, will end up
  35. // returning undefined which results in no babel processing
  36. if (vueJestConfig.babelConfig === true) {
  37. babelConfig = find()
  38. }
  39. break
  40. case 'object':
  41. default:
  42. // support for inline babel options
  43. babelConfig = vueJestConfig.babelConfig
  44. break
  45. }
  46. } else if (existsSync('babel.config.js')) {
  47. babelConfig = require(path.resolve('babel.config.js'))
  48. } else {
  49. babelConfig = find()
  50. }
  51. if (babelConfig) {
  52. cache.set('babel-config', babelConfig)
  53. }
  54. return babelConfig
  55. }
  56. }