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