Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

94 lines
5.0KB

  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. // If file has .ts, .tsx, or .jsx extension, then ask node how it would treat this file if it were .js
  62. const ext = path_1.posix.extname(pathname);
  63. if (ext === '.ts' || ext === '.tsx' || ext === '.jsx') {
  64. return defer(url_1.format(Object.assign(Object.assign({}, parsed), { pathname: pathname + '.js' })));
  65. }
  66. return defer();
  67. });
  68. }
  69. function transformSource(source, context, defaultTransformSource) {
  70. return __awaiter(this, void 0, void 0, function* () {
  71. const defer = () => defaultTransformSource(source, context, defaultTransformSource);
  72. const sourceAsString = typeof source === 'string' ? source : source.toString('utf8');
  73. const { url } = context;
  74. const parsed = url_1.parse(url);
  75. if (!isFileUrlOrNodeStyleSpecifier(parsed)) {
  76. return defer();
  77. }
  78. const { pathname } = parsed;
  79. if (pathname === null || !path_1.posix.isAbsolute(pathname)) {
  80. // If we are meant to handle this URL, then it has already been resolved to an absolute path by our resolver hook
  81. return defer();
  82. }
  83. // Assigning to a new variable so it's clear that we have stopped thinking of it as a URL, and started using it like a native FS path
  84. const fileName = pathname;
  85. if (tsNodeInstance.ignored(fileName)) {
  86. return defer();
  87. }
  88. const emittedJs = tsNodeInstance.compile(sourceAsString, fileName);
  89. return { source: emittedJs };
  90. });
  91. }
  92. }
  93. exports.registerAndCreateEsmHooks = registerAndCreateEsmHooks;
  94. //# sourceMappingURL=esm.js.map