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.

390 lines
8.5 KiB

4 years ago
  1. import assert from 'assert';
  2. import * as babel from '@babel/core';
  3. import omit from 'object.omit';
  4. import babelMerge from '../src';
  5. function formatBabelConfig({ file, options }) {
  6. return options ? [file.resolved, options] : file.resolved;
  7. }
  8. describe('babel-merge', () => {
  9. it('should deeply merge preset options', () => {
  10. assert.deepStrictEqual(
  11. babelMerge(
  12. {
  13. presets: [
  14. ['@babel/env', {
  15. targets: {
  16. browsers: [
  17. 'latest 1 Chrome'
  18. ]
  19. }
  20. }]
  21. ]
  22. },
  23. {
  24. presets: [
  25. ['@babel/env', {
  26. targets: {
  27. browsers: [
  28. 'latest 1 Firefox'
  29. ]
  30. }
  31. }]
  32. ]
  33. }
  34. ),
  35. {
  36. presets: [
  37. [require.resolve('@babel/preset-env'), {
  38. targets: {
  39. browsers: [
  40. 'latest 1 Firefox'
  41. ]
  42. }
  43. }]
  44. ]
  45. }
  46. );
  47. });
  48. it('should merge by resolved name', () => {
  49. assert.deepStrictEqual(
  50. babelMerge(
  51. {
  52. presets: [
  53. [require.resolve('@babel/preset-env'), {
  54. targets: {
  55. browsers: [
  56. 'latest 1 Chrome'
  57. ]
  58. }
  59. }]
  60. ]
  61. },
  62. {
  63. presets: [
  64. ['@babel/env', {
  65. targets: {
  66. browsers: [
  67. 'latest 1 Firefox'
  68. ]
  69. }
  70. }]
  71. ]
  72. }
  73. ),
  74. {
  75. presets: [
  76. [require.resolve('@babel/preset-env'), {
  77. targets: {
  78. browsers: [
  79. 'latest 1 Firefox'
  80. ]
  81. }
  82. }]
  83. ]
  84. }
  85. );
  86. });
  87. it('should merge env options', () => {
  88. assert.deepStrictEqual(
  89. babelMerge(
  90. {
  91. env: {
  92. development: {
  93. presets: [
  94. [require.resolve('@babel/preset-env'), {
  95. targets: {
  96. browsers: [
  97. 'latest 1 Chrome'
  98. ]
  99. }
  100. }]
  101. ]
  102. }
  103. }
  104. },
  105. {
  106. env: {
  107. development: {
  108. presets: [
  109. ['@babel/env', {
  110. targets: {
  111. browsers: [
  112. 'latest 1 Firefox'
  113. ]
  114. }
  115. }]
  116. ]
  117. }
  118. }
  119. }
  120. ),
  121. {
  122. env: {
  123. development: {
  124. presets: [
  125. [require.resolve('@babel/preset-env'), {
  126. targets: {
  127. browsers: [
  128. 'latest 1 Firefox'
  129. ]
  130. }
  131. }]
  132. ]
  133. }
  134. }
  135. }
  136. );
  137. });
  138. it('should preserve plugin / preset order', () => {
  139. assert.deepStrictEqual(
  140. babelMerge(
  141. {
  142. presets: [
  143. './test/local-preset'
  144. ],
  145. plugins: [
  146. 'module:fast-async',
  147. '@babel/plugin-syntax-dynamic-import',
  148. './test/local-plugin'
  149. ]
  150. },
  151. {
  152. presets: [
  153. '@babel/env'
  154. ],
  155. plugins: [
  156. ['./test/local-plugin', { foo: 'bar' }],
  157. '@babel/plugin-proposal-object-rest-spread',
  158. ['module:fast-async', { spec: true }],
  159. '@babel/plugin-proposal-class-properties'
  160. ]
  161. }
  162. ),
  163. {
  164. presets: [
  165. require.resolve('./local-preset'),
  166. require.resolve('@babel/preset-env')
  167. ],
  168. plugins: [
  169. [require.resolve('fast-async'), { 'spec': true }],
  170. require.resolve('@babel/plugin-syntax-dynamic-import'),
  171. [require.resolve('./local-plugin'), { foo: 'bar' }],
  172. require.resolve('@babel/plugin-proposal-object-rest-spread'),
  173. require.resolve('@babel/plugin-proposal-class-properties')
  174. ]
  175. }
  176. );
  177. });
  178. it('should merge an array of config objects', () => {
  179. assert.deepStrictEqual(
  180. babelMerge.all([
  181. {
  182. presets: [
  183. require.resolve('@babel/preset-env')
  184. ]
  185. },
  186. {
  187. presets: [
  188. '@babel/preset-env'
  189. ]
  190. },
  191. {
  192. presets: [
  193. '@babel/env'
  194. ]
  195. }
  196. ]),
  197. {
  198. presets: [
  199. require.resolve('@babel/preset-env')
  200. ]
  201. }
  202. );
  203. });
  204. it('should dedupe merged arrays', () => {
  205. assert.deepStrictEqual(
  206. babelMerge.all([
  207. {
  208. presets: [
  209. [require.resolve('@babel/preset-env'), {
  210. targets: {
  211. browsers: [
  212. 'latest 1 Chrome'
  213. ]
  214. }
  215. }]
  216. ]
  217. },
  218. {
  219. presets: [
  220. ['@babel/preset-env', {
  221. targets: {
  222. browsers: [
  223. 'latest 1 Chrome'
  224. ]
  225. }
  226. }]
  227. ]
  228. },
  229. {
  230. presets: [
  231. ['@babel/env', {
  232. targets: {
  233. browsers: [
  234. 'latest 1 Chrome'
  235. ]
  236. }
  237. }]
  238. ]
  239. }
  240. ]),
  241. {
  242. presets: [
  243. [require.resolve('@babel/preset-env'), {
  244. targets: {
  245. browsers: [
  246. 'latest 1 Chrome'
  247. ]
  248. }
  249. }]
  250. ]
  251. }
  252. );
  253. });
  254. it('should support ES6+ data structures', () => {
  255. const a = {
  256. Map: new Map([['a', 'a']]),
  257. Set: new Set(['a']),
  258. WeakMap: new WeakMap([[{ a: true }, 'a']]),
  259. WeakSet: new WeakSet([{ a: true }])
  260. };
  261. const b = {
  262. Map: new Map([['b', 'b']]),
  263. Set: new Set(['b']),
  264. WeakMap: new WeakMap([[{ b: true }, 'b']]),
  265. WeakSet: new WeakSet([{ b: true }])
  266. };
  267. const c = {
  268. Map: new Map([['c', 'c']]),
  269. Set: new Set(['c']),
  270. WeakMap: new WeakMap([[{ c: true }, 'c']]),
  271. WeakSet: new WeakSet([{ c: true }])
  272. };
  273. assert.deepStrictEqual(
  274. babelMerge.all([
  275. { presets: [[require.resolve('@babel/preset-env'), a]] },
  276. { presets: [['@babel/preset-env', b]] },
  277. { presets: [['@babel/env', c]] }
  278. ]),
  279. {
  280. presets: [
  281. [require.resolve('@babel/preset-env'), c]
  282. ]
  283. }
  284. );
  285. });
  286. it('should support deepmerge option overrides', () => {
  287. assert.deepStrictEqual(
  288. babelMerge(
  289. {
  290. presets: [
  291. ['@babel/env', {
  292. targets: {
  293. browsers: new Set()
  294. }
  295. }]
  296. ]
  297. },
  298. undefined,
  299. { isMergeableObject: () => true }
  300. ),
  301. {
  302. presets: [
  303. [require.resolve('@babel/preset-env'), {
  304. targets: {
  305. browsers: {}
  306. }
  307. }]
  308. ]
  309. }
  310. );
  311. assert.deepStrictEqual(
  312. babelMerge.all(
  313. [{
  314. presets: [
  315. ['@babel/env', {
  316. targets: {
  317. browsers: new Set()
  318. }
  319. }]
  320. ]
  321. }],
  322. { isMergeableObject: () => true }
  323. ),
  324. {
  325. presets: [
  326. [require.resolve('@babel/preset-env'), {
  327. targets: {
  328. browsers: {}
  329. }
  330. }]
  331. ]
  332. }
  333. );
  334. });
  335. it("should mirror babel's merge behavior", () => {
  336. function getOverrides() {
  337. return {
  338. presets: [
  339. ['./test/local-preset', { foo: 'bar' }],
  340. [
  341. '@babel/env',
  342. {
  343. targets: {
  344. browsers: ['>= 0.25%', 'not dead']
  345. }
  346. }
  347. ]
  348. ],
  349. plugins: [
  350. '@babel/plugin-proposal-object-rest-spread',
  351. ['module:fast-async', { spec: true }],
  352. '@babel/plugin-proposal-class-properties'
  353. ]
  354. };
  355. }
  356. const { options: { presets, plugins } } = babel.loadPartialConfig({
  357. ...getOverrides(),
  358. configFile: require.resolve('./.babelrc.test')
  359. });
  360. delete require.cache[require.resolve('./.babelrc.test')];
  361. const babelrc = require('./.babelrc.test');
  362. assert.deepStrictEqual(
  363. {
  364. presets: presets.map(formatBabelConfig),
  365. plugins: plugins.map(formatBabelConfig)
  366. },
  367. omit(babelMerge.all([babelrc, babelrc.env.test, getOverrides()]), ['env'])
  368. );
  369. });
  370. });