index.js 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. import * as babel from "@babel/core";
  2. import { ImportInjector } from "../";
  3. function test(sourceType, opts, initializer, expectedCode) {
  4. if (typeof opts === "function") {
  5. expectedCode = initializer;
  6. initializer = opts;
  7. opts = null;
  8. }
  9. const result = babel.transform("", {
  10. cwd: __dirname,
  11. sourceType,
  12. filename: "example" + (sourceType === "module" ? ".mjs" : ".js"),
  13. babelrc: false,
  14. plugins: [
  15. function({ types: t }) {
  16. return {
  17. pre(file) {
  18. file.set("helpersNamespace", t.identifier("babelHelpers"));
  19. },
  20. visitor: {
  21. Program(path) {
  22. const manager = new ImportInjector(path, opts);
  23. const ref = initializer(manager);
  24. if (ref) path.pushContainer("body", t.expressionStatement(ref));
  25. },
  26. },
  27. };
  28. },
  29. ],
  30. });
  31. expect(result.code.replace(/\s+/g, " ").trim()).toBe(
  32. (expectedCode || "").replace(/\s+/g, " ").trim(),
  33. );
  34. }
  35. const testScript = test.bind(undefined, "script");
  36. const testModule = test.bind(undefined, "module");
  37. describe("@babel/helper-module-imports", () => {
  38. describe("namespace import", () => {
  39. const addNamespace = opts => m => m.addNamespace("source", opts);
  40. describe("loading an ES6 module", () => {
  41. const importedType = "es6";
  42. describe("using Node's interop", () => {
  43. const importingInterop = "node";
  44. it("should import", () => {
  45. testModule(
  46. { importingInterop, importedType },
  47. addNamespace(),
  48. `
  49. import * as _source from "source";
  50. _source;
  51. `,
  52. );
  53. });
  54. it("should import with a name hint", () => {
  55. testModule(
  56. { importingInterop, importedType },
  57. addNamespace({ nameHint: "hintedName" }),
  58. `
  59. import * as _hintedName from "source";
  60. _hintedName;
  61. `,
  62. );
  63. });
  64. });
  65. describe("using Babel's interop", () => {
  66. const importingInterop = "babel";
  67. it("should import", () => {
  68. testModule(
  69. { importingInterop, importedType },
  70. addNamespace(),
  71. `
  72. import * as _source from "source";
  73. _source;
  74. `,
  75. );
  76. });
  77. });
  78. describe("using a CommonJS loader", () => {
  79. it("should import", () => {
  80. expect(() => {
  81. testScript({ importedType }, addNamespace());
  82. }).toThrow("Cannot import an ES6 module from CommonJS");
  83. });
  84. });
  85. });
  86. describe("loading CommonJS with 'uncompiled'", () => {
  87. const importedInterop = "uncompiled";
  88. describe("using Node's interop", () => {
  89. const importingInterop = "node";
  90. it("should import", () => {
  91. testModule(
  92. { importingInterop, importedInterop },
  93. addNamespace(),
  94. `
  95. import _source from "source";
  96. _source;
  97. `,
  98. );
  99. });
  100. });
  101. describe("using Babel's interop", () => {
  102. const importingInterop = "babel";
  103. it("should import", () => {
  104. testModule(
  105. { importingInterop, importedInterop },
  106. addNamespace(),
  107. `
  108. import _source from "source";
  109. _source;
  110. `,
  111. );
  112. });
  113. });
  114. describe("using a CommonJS loader", () => {
  115. it("should import", () => {
  116. testScript(
  117. { importedInterop },
  118. addNamespace(),
  119. `
  120. var _source = require("source");
  121. _source;
  122. `,
  123. );
  124. });
  125. });
  126. });
  127. describe("loading CommonJS with 'compiled'", () => {
  128. const importedInterop = "compiled";
  129. describe("using Node's interop", () => {
  130. const importingInterop = "node";
  131. it("should import", () => {
  132. testModule(
  133. { importingInterop, importedInterop },
  134. addNamespace(),
  135. `
  136. import _source from "source";
  137. _source;
  138. `,
  139. );
  140. });
  141. });
  142. describe("using Babel's interop", () => {
  143. const importingInterop = "babel";
  144. it("should import", () => {
  145. testModule(
  146. { importingInterop, importedInterop },
  147. addNamespace(),
  148. `
  149. import * as _source from "source";
  150. _source;
  151. `,
  152. );
  153. });
  154. });
  155. describe("using a CommonJS loader", () => {
  156. it("should import", () => {
  157. testScript(
  158. { importedInterop },
  159. addNamespace(),
  160. `
  161. var _source = require("source");
  162. _source;
  163. `,
  164. );
  165. });
  166. });
  167. });
  168. describe("loading CommonJS with 'babel'", () => {
  169. const importedInterop = "babel";
  170. describe("using Node's interop", () => {
  171. const importingInterop = "node";
  172. it("should import", () => {
  173. testModule(
  174. { importingInterop, importedInterop },
  175. addNamespace(),
  176. `
  177. import _source$es6Default from "source";
  178. var _source = babelHelpers.interopRequireWildcard(_source$es6Default);
  179. _source;
  180. `,
  181. );
  182. });
  183. });
  184. describe("using Babel's interop", () => {
  185. const importingInterop = "babel";
  186. it("should import", () => {
  187. testModule(
  188. { importingInterop, importedInterop },
  189. addNamespace(),
  190. `
  191. import * as _source from "source";
  192. _source;
  193. `,
  194. );
  195. });
  196. });
  197. describe("using a CommonJS loader", () => {
  198. it("should import", () => {
  199. testScript(
  200. { importedInterop },
  201. addNamespace(),
  202. `
  203. var _source = babelHelpers.interopRequireWildcard(require("source"));
  204. _source;
  205. `,
  206. );
  207. });
  208. });
  209. });
  210. });
  211. describe("default imports", () => {
  212. const addDefault = opts => m => m.addDefault("source", opts);
  213. describe("loading an ES6 module", () => {
  214. const importedType = "es6";
  215. describe("using Node's interop", () => {
  216. const importingInterop = "node";
  217. it("should import", () => {
  218. testModule(
  219. { importingInterop, importedType },
  220. addDefault(),
  221. `
  222. import _default from "source";
  223. _default;
  224. `,
  225. );
  226. });
  227. it("should import with a name hint", () => {
  228. testModule(
  229. { importingInterop, importedType },
  230. addDefault({ nameHint: "hintedName" }),
  231. `
  232. import _hintedName from "source";
  233. _hintedName;
  234. `,
  235. );
  236. });
  237. });
  238. describe("using Babel's interop", () => {
  239. const importingInterop = "babel";
  240. it("should import", () => {
  241. testModule(
  242. { importingInterop, importedType },
  243. addDefault(),
  244. `
  245. import _default from "source";
  246. _default;
  247. `,
  248. );
  249. });
  250. it("should import with a name hint", () => {
  251. testModule(
  252. { importingInterop, importedType },
  253. addDefault({ nameHint: "hintedName" }),
  254. `
  255. import _hintedName from "source";
  256. _hintedName;
  257. `,
  258. );
  259. });
  260. });
  261. describe("using a CommonJS loader", () => {
  262. it("should import", () => {
  263. expect(() => {
  264. testScript({ importedType }, addDefault());
  265. }).toThrow("Cannot import an ES6 module from CommonJS");
  266. });
  267. });
  268. });
  269. describe("loading CommonJS with 'uncompiled'", () => {
  270. const importedInterop = "uncompiled";
  271. describe("using Node's interop", () => {
  272. const importingInterop = "node";
  273. it("should import", () => {
  274. testModule(
  275. { importingInterop, importedInterop },
  276. addDefault(),
  277. `
  278. import _default from "source";
  279. _default;
  280. `,
  281. );
  282. });
  283. it("should import with a name hint", () => {
  284. testModule(
  285. { importingInterop, importedInterop },
  286. addDefault({ nameHint: "hintedName" }),
  287. `
  288. import _hintedName from "source";
  289. _hintedName;
  290. `,
  291. );
  292. });
  293. });
  294. describe("using Babel's interop", () => {
  295. const importingInterop = "babel";
  296. it("should import", () => {
  297. testModule(
  298. { importingInterop, importedInterop },
  299. addDefault(),
  300. `
  301. import _default from "source";
  302. _default;
  303. `,
  304. );
  305. });
  306. it("should import with a name hint", () => {
  307. testModule(
  308. { importingInterop, importedInterop },
  309. addDefault({ nameHint: "hintedName" }),
  310. `
  311. import _hintedName from "source";
  312. _hintedName;
  313. `,
  314. );
  315. });
  316. });
  317. describe("using a CommonJS loader", () => {
  318. it("should import", () => {
  319. testScript(
  320. { importedInterop },
  321. addDefault(),
  322. `
  323. var _default = require("source");
  324. _default;
  325. `,
  326. );
  327. });
  328. it("should import with a name hint", () => {
  329. testScript(
  330. { importedInterop },
  331. addDefault({ nameHint: "hintedName" }),
  332. `
  333. var _hintedName = require("source");
  334. _hintedName;
  335. `,
  336. );
  337. });
  338. it("should fail to import with force-enabled liveness", () => {
  339. expect(() => {
  340. testScript(
  341. { importedInterop, ensureLiveReference: true },
  342. addDefault(),
  343. );
  344. }).toThrow("No live reference for commonjs default");
  345. });
  346. });
  347. });
  348. describe("loading CommonJS with 'compiled'", () => {
  349. const importedInterop = "compiled";
  350. describe("using Node's interop", () => {
  351. const importingInterop = "node";
  352. it("should import", () => {
  353. testModule(
  354. { importingInterop, importedInterop },
  355. addDefault(),
  356. `
  357. import _source from "source";
  358. _source.default;
  359. `,
  360. );
  361. });
  362. it("should import with a force-disabled context", () => {
  363. testModule(
  364. { importingInterop, importedInterop, ensureNoContext: true },
  365. addDefault(),
  366. `
  367. import _source from "source";
  368. 0, _source.default;
  369. `,
  370. );
  371. });
  372. });
  373. describe("using Babel's interop", () => {
  374. const importingInterop = "babel";
  375. it("should import", () => {
  376. testModule(
  377. { importingInterop, importedInterop },
  378. addDefault(),
  379. `
  380. import _default from "source";
  381. _default;
  382. `,
  383. );
  384. });
  385. it("should import with a name hint", () => {
  386. testModule(
  387. { importingInterop, importedInterop },
  388. addDefault({ nameHint: "hintedName" }),
  389. `
  390. import _hintedName from "source";
  391. _hintedName;
  392. `,
  393. );
  394. });
  395. });
  396. describe("using a CommonJS loader", () => {
  397. it("should import", () => {
  398. testScript(
  399. { importedInterop },
  400. addDefault(),
  401. `
  402. var _default = require("source").default;
  403. _default;
  404. `,
  405. );
  406. });
  407. it("should import with a name hint", () => {
  408. testScript(
  409. { importedInterop },
  410. addDefault({ nameHint: "hintedName" }),
  411. `
  412. var _hintedName = require("source").default;
  413. _hintedName;
  414. `,
  415. );
  416. });
  417. it("should import with force-enabled liveness", () => {
  418. testScript(
  419. { importedInterop, ensureLiveReference: true },
  420. addDefault(),
  421. `
  422. var _source = require("source");
  423. _source.default;
  424. `,
  425. );
  426. });
  427. });
  428. });
  429. describe("loading CommonJS with 'babel'", () => {
  430. const importedInterop = "babel";
  431. describe("using Node's interop", () => {
  432. const importingInterop = "node";
  433. it("should import", () => {
  434. testModule(
  435. { importingInterop, importedInterop },
  436. addDefault(),
  437. `
  438. import _source$es6Default from "source";
  439. var _source = babelHelpers.interopRequireDefault(_source$es6Default).default;
  440. _source;
  441. `,
  442. );
  443. });
  444. it("should import with a name hint", () => {
  445. testModule(
  446. { importingInterop, importedInterop },
  447. addDefault({ nameHint: "hintedName" }),
  448. `
  449. import _source$es6Default from "source";
  450. var _hintedName = babelHelpers.interopRequireDefault(_source$es6Default).default;
  451. _hintedName;
  452. `,
  453. );
  454. });
  455. it("should import with force-enabled liveness", () => {
  456. testModule(
  457. { importingInterop, importedInterop, ensureLiveReference: true },
  458. addDefault(),
  459. `
  460. import _source$es6Default from "source";
  461. var _source = babelHelpers.interopRequireDefault(_source$es6Default);
  462. _source.default;
  463. `,
  464. );
  465. });
  466. });
  467. describe("using Babel's interop", () => {
  468. const importingInterop = "babel";
  469. it("should import", () => {
  470. testModule(
  471. { importingInterop, importedInterop },
  472. addDefault(),
  473. `
  474. import _default from "source";
  475. _default;
  476. `,
  477. );
  478. });
  479. it("should import with a name hint", () => {
  480. testModule(
  481. { importingInterop, importedInterop },
  482. addDefault({ nameHint: "hintedName" }),
  483. `
  484. import _hintedName from "source";
  485. _hintedName;
  486. `,
  487. );
  488. });
  489. });
  490. describe("using a CommonJS loader", () => {
  491. it("should import", () => {
  492. testScript(
  493. { importedInterop },
  494. addDefault(),
  495. `
  496. var _default = babelHelpers.interopRequireDefault(require("source")).default;
  497. _default;
  498. `,
  499. );
  500. });
  501. it("should import with a name hint", () => {
  502. testScript(
  503. { importedInterop },
  504. addDefault({ nameHint: "hintedName" }),
  505. `
  506. var _hintedName = babelHelpers.interopRequireDefault(require("source")).default;
  507. _hintedName;
  508. `,
  509. );
  510. });
  511. it("should import with force-enabled liveness", () => {
  512. testScript(
  513. { importedInterop, ensureLiveReference: true },
  514. addDefault(),
  515. `
  516. var _source = babelHelpers.interopRequireDefault(require("source"));
  517. _source.default;
  518. `,
  519. );
  520. });
  521. });
  522. });
  523. });
  524. describe("named imports", () => {
  525. const addNamed = opts => m => m.addNamed("read", "source", opts);
  526. describe("loading an ES6 module", () => {
  527. const importedType = "es6";
  528. describe("using Node's interop", () => {
  529. const importingInterop = "node";
  530. it("should import", () => {
  531. testModule(
  532. { importingInterop, importedType },
  533. addNamed(),
  534. `
  535. import { read as _read } from "source";
  536. _read;
  537. `,
  538. );
  539. });
  540. it("should import with a name hint", () => {
  541. testModule(
  542. { importingInterop, importedType },
  543. addNamed({ nameHint: "hintedName" }),
  544. `
  545. import { read as _hintedName } from "source";
  546. _hintedName;
  547. `,
  548. );
  549. });
  550. });
  551. describe("using Babel's interop", () => {
  552. const importingInterop = "babel";
  553. it("should import", () => {
  554. testModule(
  555. { importingInterop, importedType },
  556. addNamed(),
  557. `
  558. import { read as _read } from "source";
  559. _read;
  560. `,
  561. );
  562. });
  563. it("should import with a name hint", () => {
  564. testModule(
  565. { importingInterop, importedType },
  566. addNamed({ nameHint: "hintedName" }),
  567. `
  568. import { read as _hintedName } from "source";
  569. _hintedName;
  570. `,
  571. );
  572. });
  573. });
  574. describe("using a CommonJS loader", () => {
  575. it("should import", () => {
  576. expect(() => {
  577. testScript({ importedType }, addNamed());
  578. }).toThrow("Cannot import an ES6 module from CommonJS");
  579. });
  580. });
  581. });
  582. describe("loading CommonJS with 'uncompiled'", () => {
  583. const importedInterop = "uncompiled";
  584. describe("using Node's interop", () => {
  585. const importingInterop = "node";
  586. it("should import", () => {
  587. testModule(
  588. { importingInterop, importedInterop },
  589. addNamed(),
  590. `
  591. import _source from "source";
  592. _source.read;
  593. `,
  594. );
  595. });
  596. it("should import with a force-disabled context", () => {
  597. testModule(
  598. { importingInterop, importedInterop, ensureNoContext: true },
  599. addNamed(),
  600. `
  601. import _source from "source";
  602. 0, _source.read;
  603. `,
  604. );
  605. });
  606. });
  607. describe("using Babel's interop", () => {
  608. const importingInterop = "babel";
  609. it("should import", () => {
  610. testModule(
  611. { importingInterop, importedInterop },
  612. addNamed(),
  613. `
  614. import { read as _read } from "source";
  615. _read;
  616. `,
  617. );
  618. });
  619. it("should import with a name hint", () => {
  620. testModule(
  621. { importingInterop, importedInterop },
  622. addNamed({ nameHint: "hintedName" }),
  623. `
  624. import { read as _hintedName } from "source";
  625. _hintedName;
  626. `,
  627. );
  628. });
  629. });
  630. describe("using a CommonJS loader", () => {
  631. it("should import", () => {
  632. testScript(
  633. { importedInterop },
  634. addNamed(),
  635. `
  636. var _read = require("source").read;
  637. _read;
  638. `,
  639. );
  640. });
  641. it("should import with a name hint", () => {
  642. testScript(
  643. { importedInterop },
  644. addNamed({ nameHint: "hintedName" }),
  645. `
  646. var _hintedName = require("source").read;
  647. _hintedName;
  648. `,
  649. );
  650. });
  651. it("should import with force-enabled liveness", () => {
  652. testScript(
  653. { importedInterop, ensureLiveReference: true },
  654. addNamed(),
  655. `
  656. var _source = require("source");
  657. _source.read;
  658. `,
  659. );
  660. });
  661. });
  662. });
  663. describe("loading CommonJS with 'compiled'", () => {
  664. const importedInterop = "compiled";
  665. describe("using Node's interop", () => {
  666. const importingInterop = "node";
  667. it("should import", () => {
  668. testModule(
  669. { importingInterop, importedInterop },
  670. addNamed(),
  671. `
  672. import _source from "source";
  673. _source.read;
  674. `,
  675. );
  676. });
  677. it("should import with a force-disabled context", () => {
  678. testModule(
  679. { importingInterop, importedInterop, ensureNoContext: true },
  680. addNamed(),
  681. `
  682. import _source from "source";
  683. 0, _source.read;
  684. `,
  685. );
  686. });
  687. });
  688. describe("using Babel's interop", () => {
  689. const importingInterop = "babel";
  690. it("should import", () => {
  691. testModule(
  692. { importingInterop, importedInterop },
  693. addNamed(),
  694. `
  695. import { read as _read } from "source";
  696. _read;
  697. `,
  698. );
  699. });
  700. it("should import with a name hint", () => {
  701. testModule(
  702. { importingInterop, importedInterop },
  703. addNamed({ nameHint: "hintedName" }),
  704. `
  705. import { read as _hintedName } from "source";
  706. _hintedName;
  707. `,
  708. );
  709. });
  710. });
  711. describe("using a CommonJS loader", () => {
  712. it("should import", () => {
  713. testScript(
  714. { importedInterop },
  715. addNamed(),
  716. `
  717. var _read = require("source").read;
  718. _read;
  719. `,
  720. );
  721. });
  722. it("should import with a name hint", () => {
  723. testScript(
  724. { importedInterop },
  725. addNamed({ nameHint: "hintedName" }),
  726. `
  727. var _hintedName = require("source").read;
  728. _hintedName;
  729. `,
  730. );
  731. });
  732. it("should import with force-enabled liveness", () => {
  733. testScript(
  734. { importedInterop, ensureLiveReference: true },
  735. addNamed(),
  736. `
  737. var _source = require("source");
  738. _source.read;
  739. `,
  740. );
  741. });
  742. });
  743. });
  744. describe("loading CommonJS with 'babel'", () => {
  745. const importedInterop = "babel";
  746. describe("using Node's interop", () => {
  747. const importingInterop = "node";
  748. it("should import", () => {
  749. testModule(
  750. { importingInterop, importedInterop },
  751. addNamed(),
  752. `
  753. import _source$es6Default from "source";
  754. _source$es6Default.read;
  755. `,
  756. );
  757. });
  758. });
  759. describe("using Babel's interop", () => {
  760. const importingInterop = "babel";
  761. it("should import", () => {
  762. testModule(
  763. { importingInterop, importedInterop },
  764. addNamed(),
  765. `
  766. import { read as _read } from "source";
  767. _read;
  768. `,
  769. );
  770. });
  771. it("should import with a name hint", () => {
  772. testModule(
  773. { importingInterop, importedInterop },
  774. addNamed({ nameHint: "hintedName" }),
  775. `
  776. import { read as _hintedName } from "source";
  777. _hintedName;
  778. `,
  779. );
  780. });
  781. });
  782. describe("using a CommonJS loader", () => {
  783. it("should import", () => {
  784. testScript(
  785. { importedInterop },
  786. addNamed(),
  787. `
  788. var _read = require("source").read;
  789. _read;
  790. `,
  791. );
  792. });
  793. it("should import with a name hint", () => {
  794. testScript(
  795. { importedInterop },
  796. addNamed({ nameHint: "hintedName" }),
  797. `
  798. var _hintedName = require("source").read;
  799. _hintedName;
  800. `,
  801. );
  802. });
  803. it("should import with force-enabled liveness", () => {
  804. testScript(
  805. { importedInterop, ensureLiveReference: true },
  806. addNamed(),
  807. `
  808. var _source = require("source");
  809. _source.read;
  810. `,
  811. );
  812. });
  813. });
  814. });
  815. });
  816. describe("side-effectful imports", () => {
  817. const addSideEffect = opts => m => m.addSideEffect("source", opts);
  818. describe("loading an ES6 module", () => {
  819. const importedType = "es6";
  820. describe("using Node's interop", () => {
  821. const importingInterop = "node";
  822. it("should import", () => {
  823. testModule(
  824. { importingInterop, importedType },
  825. addSideEffect(),
  826. `
  827. import "source";
  828. `,
  829. );
  830. });
  831. });
  832. describe("using Babel's interop", () => {
  833. const importingInterop = "babel";
  834. it("should import", () => {
  835. testModule(
  836. { importingInterop, importedType },
  837. addSideEffect(),
  838. `
  839. import "source";
  840. `,
  841. );
  842. });
  843. });
  844. describe("using a CommonJS loader", () => {
  845. it("should import", () => {
  846. expect(() => {
  847. testScript({ importedType }, addSideEffect());
  848. }).toThrow("Cannot import an ES6 module from CommonJS");
  849. });
  850. });
  851. });
  852. describe("loading CommonJS with 'uncompiled'", () => {
  853. const importedInterop = "uncompiled";
  854. describe("using Node's interop", () => {
  855. const importingInterop = "node";
  856. it("should import", () => {
  857. testModule(
  858. { importingInterop, importedInterop },
  859. addSideEffect(),
  860. `
  861. import "source";
  862. `,
  863. );
  864. });
  865. });
  866. describe("using Babel's interop", () => {
  867. const importingInterop = "babel";
  868. it("should import", () => {
  869. testModule(
  870. { importingInterop, importedInterop },
  871. addSideEffect(),
  872. `
  873. import "source";
  874. `,
  875. );
  876. });
  877. });
  878. describe("using a CommonJS loader", () => {
  879. it("should import", () => {
  880. testScript(
  881. { importedInterop },
  882. addSideEffect(),
  883. `
  884. require("source");
  885. `,
  886. );
  887. });
  888. });
  889. });
  890. describe("loading CommonJS with 'compiled'", () => {
  891. const importedInterop = "compiled";
  892. describe("using Node's interop", () => {
  893. const importingInterop = "node";
  894. it("should import", () => {
  895. testModule(
  896. { importingInterop, importedInterop },
  897. addSideEffect(),
  898. `
  899. import "source";
  900. `,
  901. );
  902. });
  903. });
  904. describe("using Babel's interop", () => {
  905. const importingInterop = "babel";
  906. it("should import", () => {
  907. testModule(
  908. { importingInterop, importedInterop },
  909. addSideEffect(),
  910. `
  911. import "source";
  912. `,
  913. );
  914. });
  915. });
  916. describe("using a CommonJS loader", () => {
  917. it("should import", () => {
  918. testScript(
  919. { importedInterop },
  920. addSideEffect(),
  921. `
  922. require("source");
  923. `,
  924. );
  925. });
  926. });
  927. });
  928. describe("loading CommonJS with 'babel'", () => {
  929. const importedInterop = "babel";
  930. describe("using Node's interop", () => {
  931. const importingInterop = "node";
  932. it("should import", () => {
  933. testModule(
  934. { importingInterop, importedInterop },
  935. addSideEffect(),
  936. `
  937. import "source";
  938. `,
  939. );
  940. });
  941. });
  942. describe("using Babel's interop", () => {
  943. const importingInterop = "babel";
  944. it("should import", () => {
  945. testModule(
  946. { importingInterop, importedInterop },
  947. addSideEffect(),
  948. `
  949. import "source";
  950. `,
  951. );
  952. });
  953. });
  954. describe("using a CommonJS loader", () => {
  955. it("should import", () => {
  956. testScript(
  957. { importedInterop },
  958. addSideEffect(),
  959. `
  960. require("source");
  961. `,
  962. );
  963. });
  964. });
  965. });
  966. });
  967. });