esm.js 4.7 KB

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