injectCaller.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. };
  21. // TODO: We can remove this eventually, I'm just adding it so that people have
  22. // a little time to migrate to the newer RCs of @babel/core without getting
  23. // hard-to-diagnose errors about unknown 'caller' options.
  24. let supportsCallerOptionFlag = undefined;
  25. function supportsCallerOption() {
  26. if (supportsCallerOptionFlag === undefined) {
  27. try {
  28. // Rather than try to match the Babel version, we just see if it throws
  29. // when passed a 'caller' flag, and use that to decide if it is supported.
  30. babel.loadPartialConfig({
  31. caller: undefined,
  32. babelrc: false,
  33. configFile: false
  34. });
  35. supportsCallerOptionFlag = true;
  36. } catch (err) {
  37. supportsCallerOptionFlag = false;
  38. }
  39. }
  40. return supportsCallerOptionFlag;
  41. }