babel.config.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. module.exports = function(api) {
  3. const env = api.env();
  4. const includeCoverage = process.env.BABEL_COVERAGE === "true";
  5. const envOpts = {
  6. loose: true,
  7. modules: false,
  8. exclude: ["transform-typeof-symbol"],
  9. };
  10. let convertESM = true;
  11. let ignoreLib = true;
  12. switch (env) {
  13. // Configs used during bundling builds.
  14. case "babel-parser":
  15. case "standalone":
  16. convertESM = false;
  17. ignoreLib = false;
  18. break;
  19. case "production":
  20. // Config during builds before publish.
  21. envOpts.targets = {
  22. node: "6.9",
  23. };
  24. break;
  25. case "development":
  26. envOpts.debug = true;
  27. envOpts.targets = {
  28. node: "current",
  29. };
  30. break;
  31. case "test":
  32. envOpts.targets = {
  33. node: "current",
  34. };
  35. break;
  36. }
  37. const config = {
  38. comments: false,
  39. ignore: [
  40. // These may not be strictly necessary with the newly-limited scope of
  41. // babelrc searching, but including them for now because we had them
  42. // in our .babelignore before.
  43. "packages/*/test/fixtures",
  44. ignoreLib ? "packages/*/lib" : null,
  45. "packages/babel-standalone/babel.js",
  46. "packages/babel-preset-env-standalone/babel-preset-env.js",
  47. ].filter(Boolean),
  48. presets: [["@babel/env", envOpts]],
  49. plugins: [
  50. // TODO: Use @babel/preset-flow when
  51. // https://github.com/babel/babel/issues/7233 is fixed
  52. "@babel/plugin-transform-flow-strip-types",
  53. ["@babel/proposal-class-properties", { loose: true }],
  54. "@babel/proposal-export-namespace-from",
  55. "@babel/proposal-numeric-separator",
  56. [
  57. "@babel/proposal-object-rest-spread",
  58. { useBuiltIns: true, loose: true },
  59. ],
  60. // Explicitly use the lazy version of CommonJS modules.
  61. convertESM ? ["@babel/transform-modules-commonjs", { lazy: true }] : null,
  62. ].filter(Boolean),
  63. overrides: [
  64. {
  65. test: "packages/babel-parser",
  66. plugins: [
  67. "babel-plugin-transform-charcodes",
  68. ["@babel/transform-for-of", { assumeArray: true }],
  69. ],
  70. },
  71. {
  72. test: "./packages/babel-register",
  73. plugins: [
  74. // Override the root options to disable lazy imports for babel-register
  75. // because otherwise the require hook will try to lazy-import things
  76. // leading to dependency cycles.
  77. convertESM ? "@babel/transform-modules-commonjs" : null,
  78. ].filter(Boolean),
  79. },
  80. ],
  81. };
  82. // we need to do this as long as we do not test everything from source
  83. if (includeCoverage) {
  84. config.auxiliaryCommentBefore = "istanbul ignore next";
  85. config.plugins.push("babel-plugin-istanbul");
  86. }
  87. return config;
  88. };