polyfillsPlugin.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const { addSideEffect } = require('@babel/helper-module-imports')
  2. // slightly modifiled from @babel/preset-env/src/utils
  3. // use an absolute path for core-js modules, to fix conflicts of different core-js versions
  4. function getModulePath (mod, useAbsolutePath) {
  5. const modPath =
  6. mod === 'regenerator-runtime'
  7. ? 'regenerator-runtime/runtime'
  8. : `core-js/modules/${mod}`
  9. return useAbsolutePath ? require.resolve(modPath) : modPath
  10. }
  11. function createImport (path, mod, useAbsolutePath) {
  12. return addSideEffect(path, getModulePath(mod, useAbsolutePath))
  13. }
  14. // add polyfill imports to the first file encountered.
  15. module.exports = (
  16. { types },
  17. { polyfills, entryFiles = [], useAbsolutePath }
  18. ) => {
  19. return {
  20. name: 'vue-cli-inject-polyfills',
  21. visitor: {
  22. Program (path, state) {
  23. if (!entryFiles.includes(state.filename)) {
  24. return
  25. }
  26. // imports are injected in reverse order
  27. polyfills
  28. .slice()
  29. .reverse()
  30. .forEach(p => {
  31. createImport(path, p, useAbsolutePath)
  32. })
  33. }
  34. }
  35. }
  36. }