injectCaller.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. const babel = require("@babel/core");
  3. module.exports = function injectCaller(opts, target) {
  4. if (!supportsCallerOption()) return opts;
  5. return Object.assign({}, opts, {
  6. caller: Object.assign({
  7. name: "babel-loader",
  8. // Provide plugins with insight into webpack target.
  9. // https://github.com/babel/babel-loader/issues/787
  10. target,
  11. // Webpack >= 2 supports ESM and dynamic import.
  12. supportsStaticESM: true,
  13. supportsDynamicImport: true,
  14. // Webpack 5 supports TLA behind a flag. We enable it by default
  15. // for Babel, and then webpack will throw an error if the experimental
  16. // flag isn't enabled.
  17. supportsTopLevelAwait: true
  18. }, opts.caller)
  19. });
  20. }; // TODO: We can remove this eventually, I'm just adding it so that people have
  21. // a little time to migrate to the newer RCs of @babel/core without getting
  22. // hard-to-diagnose errors about unknown 'caller' options.
  23. let supportsCallerOptionFlag = undefined;
  24. function supportsCallerOption() {
  25. if (supportsCallerOptionFlag === undefined) {
  26. try {
  27. // Rather than try to match the Babel version, we just see if it throws
  28. // when passed a 'caller' flag, and use that to decide if it is supported.
  29. babel.loadPartialConfig({
  30. caller: undefined,
  31. babelrc: false,
  32. configFile: false
  33. });
  34. supportsCallerOptionFlag = true;
  35. } catch (err) {
  36. supportsCallerOptionFlag = false;
  37. }
  38. }
  39. return supportsCallerOptionFlag;
  40. }