targets.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // copied from @vue/babel-preset-app
  2. const { semver } = require('@vue/cli-shared-utils')
  3. const { default: getTargets } = require('@babel/helper-compilation-targets')
  4. // See the result at <https://github.com/babel/babel/blob/v7.13.15/packages/babel-compat-data/data/native-modules.json>
  5. const allModuleTargets = getTargets(
  6. { esmodules: true },
  7. { ignoreBrowserslistConfig: true }
  8. )
  9. function getIntersectionTargets (targets, constraintTargets) {
  10. const intersection = Object.keys(constraintTargets).reduce(
  11. (results, browser) => {
  12. // exclude the browsers that the user does not need
  13. if (!targets[browser]) {
  14. return results
  15. }
  16. // if the user-specified version is higher the minimum version that supports esmodule, than use it
  17. results[browser] = semver.gt(
  18. semver.coerce(constraintTargets[browser]),
  19. semver.coerce(targets[browser])
  20. )
  21. ? constraintTargets[browser]
  22. : targets[browser]
  23. return results
  24. },
  25. {}
  26. )
  27. return intersection
  28. }
  29. function getModuleTargets (targets) {
  30. // use the intersection of modern mode browsers and user defined targets config
  31. return getIntersectionTargets(targets, allModuleTargets)
  32. }
  33. function doAllTargetsSupportModule (targets) {
  34. const browserList = Object.keys(targets)
  35. return browserList.every(browserName => {
  36. if (!allModuleTargets[browserName]) {
  37. return false
  38. }
  39. return semver.gte(
  40. semver.coerce(targets[browserName]),
  41. semver.coerce(allModuleTargets[browserName])
  42. )
  43. })
  44. }
  45. // get browserslist targets in current working directory
  46. const projectTargets = getTargets()
  47. const projectModuleTargets = getModuleTargets(projectTargets)
  48. const allProjectTargetsSupportModule = doAllTargetsSupportModule(projectTargets)
  49. module.exports = {
  50. getTargets,
  51. getModuleTargets,
  52. getIntersectionTargets,
  53. doAllTargetsSupportModule,
  54. projectTargets,
  55. projectModuleTargets,
  56. allProjectTargetsSupportModule
  57. }