configure.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. 'use strict';
  8. /* eslint-disable indent */
  9. module.exports = function(options) {
  10. options = Object.assign({
  11. autoImport: true,
  12. inlineRequires: process.env.NODE_ENV === 'test',
  13. objectAssign: true,
  14. rewriteModules: null, // {map: ?{[module: string]: string}, prefix: ?string}
  15. stripDEV: false,
  16. target: 'js',
  17. }, options);
  18. if (options.target !== 'js' && options.target !== 'flow') {
  19. throw new Error('options.target must be one of "js" or "flow".');
  20. }
  21. // Always enable these. These will overlap with some transforms (which also
  22. // enable the corresponding syntax, eg Flow), but these are the minimal
  23. // additional syntaxes that need to be enabled so we can minimally transform
  24. // to .js.flow files as well.
  25. let presetSets = [
  26. [
  27. require('@babel/plugin-syntax-class-properties'),
  28. require('@babel/plugin-syntax-flow'),
  29. require('@babel/plugin-syntax-jsx'),
  30. require('babel-plugin-syntax-trailing-function-commas'),
  31. require('@babel/plugin-syntax-object-rest-spread'),
  32. options.autoImport ? require('./plugins/auto-importer') : null,
  33. options.rewriteModules ?
  34. [require('./plugins/rewrite-modules'), options.rewriteModules || {}] :
  35. null,
  36. ],
  37. [
  38. options.inlineRequires ? require('./plugins/inline-requires') : null,
  39. options.stripDEV ? require('./plugins/dev-expression') : null,
  40. ]
  41. ];
  42. // We only want to add declarations for flow transforms and not for js. So we
  43. // have to do this separate from above.
  44. if (options.target === 'flow') {
  45. presetSets[0].push(require('./plugins/dev-declaration'));
  46. }
  47. // Enable everything else for js.
  48. if (options.target === 'js') {
  49. presetSets[0] = presetSets[0].concat([
  50. require('@babel/plugin-transform-template-literals'),
  51. require('@babel/plugin-transform-literals'),
  52. require('@babel/plugin-transform-function-name'),
  53. require('@babel/plugin-transform-arrow-functions'),
  54. require('@babel/plugin-transform-block-scoped-functions'),
  55. require('@babel/plugin-proposal-class-properties'),
  56. [require('@babel/plugin-transform-classes'), {loose: true}],
  57. require('@babel/plugin-transform-object-super'),
  58. require('@babel/plugin-transform-shorthand-properties'),
  59. require('@babel/plugin-transform-computed-properties'),
  60. require('@babel/plugin-transform-for-of'),
  61. [require('@babel/plugin-transform-spread'), {loose: true}],
  62. require('@babel/plugin-transform-parameters'),
  63. [require('@babel/plugin-transform-destructuring'), {loose: true}],
  64. require('@babel/plugin-transform-block-scoping'),
  65. require('@babel/plugin-transform-modules-commonjs'),
  66. require('@babel/plugin-transform-member-expression-literals'),
  67. require('@babel/plugin-transform-property-literals'),
  68. require('@babel/plugin-transform-flow-strip-types'),
  69. require('@babel/plugin-proposal-object-rest-spread'),
  70. require('@babel/plugin-transform-react-display-name'),
  71. require('@babel/plugin-transform-react-jsx'),
  72. // Don't enable this plugin unless we're compiling JS, even if the option is true
  73. options.objectAssign ? require('./plugins/object-assign') : null,
  74. ]);
  75. }
  76. // Use two passes to circumvent bug with auto-importer and inline-requires.
  77. const passPresets = presetSets.map(function(plugins) {
  78. return {
  79. plugins: plugins.filter(function(plugin) {
  80. return plugin != null;
  81. }),
  82. };
  83. });
  84. return {
  85. passPerPreset: true,
  86. presets: passPresets,
  87. };
  88. };