You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
3.7 KiB

4 years ago
  1. # babel-merge
  2. `babel-merge` merges multiple Babel configuration objects into a single copy.
  3. Plugin and preset objects and arrays will be merged together.
  4. [![NPM version][npm-image]][npm-url] [![NPM downloads][npm-downloads]][npm-url]
  5. _Note: **options** to plugins and presets **will not be merged**, but instead
  6. replaced by the last matching item's options. This makes the behavior consistent
  7. with how Babel works._
  8. ## Requirements
  9. - Node.js v6.10+
  10. - Yarn or npm client
  11. ## Installation
  12. `babel-merge` can be installed via the Yarn or npm clients.
  13. #### Yarn
  14. ```bash
  15. ❯ yarn add babel-merge
  16. ```
  17. #### npm
  18. ```bash
  19. ❯ npm install --save babel-merge
  20. ```
  21. ## Usage
  22. * __merge(a, b, _options_)__
  23. * __merge.all([a, b, ..., z], _options_)__
  24. Where `a`, `b`, `z` are [Babel configuration objects](https://babeljs.io/docs/usage/api/#options) and `options` is a [deepmerge](https://github.com/KyleAMathews/deepmerge#api) options object.
  25. ```js
  26. const merge = require('babel-merge');
  27. const together = merge(
  28. {
  29. presets: [
  30. ['@babel/preset-env', {
  31. targets: {
  32. browsers: ['latest 1 Chrome']
  33. }
  34. }]
  35. ]
  36. },
  37. {
  38. presets: [
  39. ['@babel/preset-env', {
  40. targets: {
  41. browsers: ['latest 1 Firefox']
  42. }
  43. }]
  44. ]
  45. }
  46. )
  47. console.log(together);
  48. {
  49. presets: [
  50. ['@babel/preset-env', {
  51. targets: {
  52. browsers: [
  53. 'latest 1 Firefox'
  54. ]
  55. }
  56. }]
  57. ]
  58. }
  59. ```
  60. If a pathname was used in an earlier merge, you can still merge by exact name:
  61. ```js
  62. const merge = require('babel-merge');
  63. const together = merge(
  64. {
  65. presets: [
  66. [require.resolve('@babel/preset-env'), {
  67. targets: {
  68. browsers: ['latest 1 Chrome']
  69. }
  70. }]
  71. ]
  72. },
  73. {
  74. presets: [
  75. ['@babel/preset-env', {
  76. targets: {
  77. browsers: ['latest 1 Firefox']
  78. }
  79. }]
  80. ]
  81. }
  82. )
  83. console.log(together);
  84. {
  85. presets: [
  86. ['/Users/me/code/app/node_modules/@babel/preset-env/lib/index.js', {
  87. targets: {
  88. browsers: [
  89. 'latest 1 Firefox'
  90. ]
  91. }
  92. }]
  93. ]
  94. }
  95. ```
  96. Even works for plugins and presets within environments:
  97. ```js
  98. const merge = require('babel-merge');
  99. const together = merge(
  100. {
  101. env: {
  102. development: {
  103. presets: [
  104. [require.resolve('@babel/preset-env'), {
  105. targets: {
  106. browsers: ['latest 1 Chrome']
  107. }
  108. }]
  109. ]
  110. }
  111. }
  112. },
  113. {
  114. env: {
  115. development: {
  116. presets: [
  117. ['@babel/preset-env', {
  118. targets: {
  119. browsers: ['latest 1 Firefox']
  120. }
  121. }]
  122. ]
  123. }
  124. }
  125. }
  126. )
  127. console.log(together);
  128. {
  129. env: {
  130. development: {
  131. presets: [
  132. ['/Users/me/code/app/node_modules/@babel/preset-env/lib/index.js', {
  133. targets: {
  134. browsers: [
  135. 'latest 1 Firefox'
  136. ]
  137. }
  138. }]
  139. ]
  140. }
  141. }
  142. }
  143. ```
  144. Order is preserved between non-option plugins and presets and ones with options:
  145. ```js
  146. const merge = require('babel-merge');
  147. const together = merge(
  148. {
  149. plugins: [
  150. 'module:fast-async',
  151. '@babel/plugin-syntax-dynamic-import'
  152. ]
  153. },
  154. {
  155. plugins: [
  156. '@babel/plugin-proposal-object-rest-spread',
  157. ['module:fast-async', { spec: true }],
  158. '@babel/plugin-proposal-class-properties'
  159. ]
  160. }
  161. )
  162. console.log(together);
  163. {
  164. plugins: [
  165. ['module:fast-async', { spec: true }],
  166. '@babel/plugin-syntax-dynamic-import',
  167. '@babel/plugin-proposal-object-rest-spread',
  168. '@babel/plugin-proposal-class-properties'
  169. ]
  170. }
  171. ```
  172. [npm-image]: https://img.shields.io/npm/v/babel-merge.svg
  173. [npm-downloads]: https://img.shields.io/npm/dt/babel-merge.svg
  174. [npm-url]: https://npmjs.org/package/babel-merge