esmLoader.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __copyProps = (to, from, except, desc) => {
  9. if (from && typeof from === "object" || typeof from === "function") {
  10. for (let key of __getOwnPropNames(from))
  11. if (!__hasOwnProp.call(to, key) && key !== except)
  12. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  13. }
  14. return to;
  15. };
  16. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  17. // If the importer is in node compatibility mode or this is not an ESM
  18. // file that has been converted to a CommonJS file using a Babel-
  19. // compatible transform (i.e. "__esModule" has not been set), then set
  20. // "default" to the CommonJS "module.exports" for node compatibility.
  21. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  22. mod
  23. ));
  24. var import_fs = __toESM(require("fs"));
  25. var import_url = __toESM(require("url"));
  26. var import_compilationCache = require("./compilationCache");
  27. var import_portTransport = require("./portTransport");
  28. var import_transform = require("./transform");
  29. var import_util = require("../util");
  30. async function resolve(specifier, context, defaultResolve) {
  31. if (context.parentURL && context.parentURL.startsWith("file://")) {
  32. const filename = import_url.default.fileURLToPath(context.parentURL);
  33. const resolved = (0, import_transform.resolveHook)(filename, specifier);
  34. if (resolved !== void 0)
  35. specifier = import_url.default.pathToFileURL(resolved).toString();
  36. }
  37. const result = await defaultResolve(specifier, context, defaultResolve);
  38. if (result?.url && result.url.startsWith("file://"))
  39. (0, import_compilationCache.currentFileDepsCollector)()?.add(import_url.default.fileURLToPath(result.url));
  40. return result;
  41. }
  42. const kSupportedFormats = /* @__PURE__ */ new Map([
  43. ["commonjs", "commonjs"],
  44. ["module", "module"],
  45. ["commonjs-typescript", "commonjs"],
  46. ["module-typescript", "module"],
  47. [null, null],
  48. [void 0, void 0]
  49. ]);
  50. async function load(moduleUrl, context, defaultLoad) {
  51. if (!kSupportedFormats.has(context.format))
  52. return defaultLoad(moduleUrl, context, defaultLoad);
  53. if (!moduleUrl.startsWith("file://"))
  54. return defaultLoad(moduleUrl, context, defaultLoad);
  55. const filename = import_url.default.fileURLToPath(moduleUrl);
  56. if (!(0, import_transform.shouldTransform)(filename))
  57. return defaultLoad(moduleUrl, context, defaultLoad);
  58. const code = import_fs.default.readFileSync(filename, "utf-8");
  59. const transformed = (0, import_transform.transformHook)(code, filename, moduleUrl);
  60. if (transformed.serializedCache) {
  61. if (legacyWaitForSourceMaps)
  62. await transport?.send("pushToCompilationCache", { cache: transformed.serializedCache });
  63. else
  64. transport?.post("pushToCompilationCache", { cache: transformed.serializedCache });
  65. }
  66. return {
  67. format: kSupportedFormats.get(context.format) || ((0, import_util.fileIsModule)(filename) ? "module" : "commonjs"),
  68. source: transformed.code,
  69. shortCircuit: true
  70. };
  71. }
  72. let transport;
  73. let legacyWaitForSourceMaps = false;
  74. function initialize(data) {
  75. transport = createTransport(data?.port);
  76. legacyWaitForSourceMaps = !!process.env.PLAYWRIGHT_WAIT_FOR_SOURCE_MAPS;
  77. }
  78. function createTransport(port) {
  79. return new import_portTransport.PortTransport(port, async (method, params) => {
  80. if (method === "setSingleTSConfig") {
  81. (0, import_transform.setSingleTSConfig)(params.tsconfig);
  82. return;
  83. }
  84. if (method === "setTransformConfig") {
  85. (0, import_transform.setTransformConfig)(params.config);
  86. return;
  87. }
  88. if (method === "addToCompilationCache") {
  89. (0, import_compilationCache.addToCompilationCache)(params.cache);
  90. return;
  91. }
  92. if (method === "getCompilationCache")
  93. return { cache: (0, import_compilationCache.serializeCompilationCache)() };
  94. if (method === "startCollectingFileDeps") {
  95. (0, import_compilationCache.startCollectingFileDeps)();
  96. return;
  97. }
  98. if (method === "stopCollectingFileDeps") {
  99. (0, import_compilationCache.stopCollectingFileDeps)(params.file);
  100. return;
  101. }
  102. });
  103. }
  104. module.exports = { initialize, load, resolve };