esm.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.registerAndCreateEsmHooks = void 0;
  13. const index_1 = require("./index");
  14. const url_1 = require("url");
  15. const path_1 = require("path");
  16. const assert = require("assert");
  17. const { createResolve } = require('../dist-raw/node-esm-resolve-implementation');
  18. // Note: On Windows, URLs look like this: file:///D:/dev/@TypeStrong/ts-node-examples/foo.ts
  19. function registerAndCreateEsmHooks(opts) {
  20. // Automatically performs registration just like `-r ts-node/register`
  21. const tsNodeInstance = index_1.register(Object.assign(Object.assign({}, opts), { experimentalEsmLoader: true }));
  22. // Custom implementation that considers additional file extensions and automatically adds file extensions
  23. const nodeResolveImplementation = createResolve(Object.assign(Object.assign({}, index_1.getExtensions(tsNodeInstance.config)), { preferTsExts: tsNodeInstance.options.preferTsExts }));
  24. return { resolve, getFormat, transformSource };
  25. function isFileUrlOrNodeStyleSpecifier(parsed) {
  26. // We only understand file:// URLs, but in node, the specifier can be a node-style `./foo` or `foo`
  27. const { protocol } = parsed;
  28. return protocol === null || protocol === 'file:';
  29. }
  30. function resolve(specifier, context, defaultResolve) {
  31. return __awaiter(this, void 0, void 0, function* () {
  32. const defer = () => __awaiter(this, void 0, void 0, function* () {
  33. const r = yield defaultResolve(specifier, context, defaultResolve);
  34. return r;
  35. });
  36. const parsed = url_1.parse(specifier);
  37. const { pathname, protocol, hostname } = parsed;
  38. if (!isFileUrlOrNodeStyleSpecifier(parsed)) {
  39. return defer();
  40. }
  41. if (protocol !== null && protocol !== 'file:') {
  42. return defer();
  43. }
  44. // Malformed file:// URL? We should always see `null` or `''`
  45. if (hostname) {
  46. // TODO file://./foo sets `hostname` to `'.'`. Perhaps we should special-case this.
  47. return defer();
  48. }
  49. // pathname is the path to be resolved
  50. return nodeResolveImplementation.defaultResolve(specifier, context, defaultResolve);
  51. });
  52. }
  53. function getFormat(url, context, defaultGetFormat) {
  54. return __awaiter(this, void 0, void 0, function* () {
  55. const defer = (overrideUrl = url) => defaultGetFormat(overrideUrl, context, defaultGetFormat);
  56. const parsed = url_1.parse(url);
  57. if (!isFileUrlOrNodeStyleSpecifier(parsed)) {
  58. return defer();
  59. }
  60. const { pathname } = parsed;
  61. assert(pathname !== null, 'ESM getFormat() hook: URL should never have null pathname');
  62. const nativePath = url_1.fileURLToPath(url);
  63. // If file has .ts, .tsx, or .jsx extension, then ask node how it would treat this file if it were .js
  64. const ext = path_1.extname(nativePath);
  65. if (ext !== '.js' && !tsNodeInstance.ignored(nativePath)) {
  66. return defer(url_1.format(url_1.pathToFileURL(nativePath + '.js')));
  67. }
  68. return defer();
  69. });
  70. }
  71. function transformSource(source, context, defaultTransformSource) {
  72. return __awaiter(this, void 0, void 0, function* () {
  73. const defer = () => defaultTransformSource(source, context, defaultTransformSource);
  74. const sourceAsString = typeof source === 'string' ? source : source.toString('utf8');
  75. const { url } = context;
  76. const parsed = url_1.parse(url);
  77. if (!isFileUrlOrNodeStyleSpecifier(parsed)) {
  78. return defer();
  79. }
  80. const nativePath = url_1.fileURLToPath(url);
  81. if (tsNodeInstance.ignored(nativePath)) {
  82. return defer();
  83. }
  84. const emittedJs = tsNodeInstance.compile(sourceAsString, nativePath);
  85. return { source: emittedJs };
  86. });
  87. }
  88. }
  89. exports.registerAndCreateEsmHooks = registerAndCreateEsmHooks;
  90. //# sourceMappingURL=esm.js.map