|
- "use strict";
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- const index_1 = require("./index");
- const url_1 = require("url");
- const path_1 = require("path");
- const assert = require("assert");
- const { createResolve } = require('../dist-raw/node-esm-resolve-implementation');
-
- function registerAndCreateEsmHooks(opts) {
-
- const tsNodeInstance = index_1.register(opts);
-
- const nodeResolveImplementation = createResolve(Object.assign(Object.assign({}, index_1.getExtensions(tsNodeInstance.config)), { preferTsExts: tsNodeInstance.options.preferTsExts }));
- return { resolve, getFormat, transformSource };
- function isFileUrlOrNodeStyleSpecifier(parsed) {
-
- const { protocol } = parsed;
- return protocol === null || protocol === 'file:';
- }
- function resolve(specifier, context, defaultResolve) {
- return __awaiter(this, void 0, void 0, function* () {
- const defer = () => __awaiter(this, void 0, void 0, function* () {
- const r = yield defaultResolve(specifier, context, defaultResolve);
- return r;
- });
- const parsed = url_1.parse(specifier);
- const { pathname, protocol, hostname } = parsed;
- if (!isFileUrlOrNodeStyleSpecifier(parsed)) {
- return defer();
- }
- if (protocol !== null && protocol !== 'file:') {
- return defer();
- }
-
- if (hostname) {
-
- return defer();
- }
-
- return nodeResolveImplementation.defaultResolve(specifier, context, defaultResolve);
- });
- }
- function getFormat(url, context, defaultGetFormat) {
- return __awaiter(this, void 0, void 0, function* () {
- const defer = (overrideUrl = url) => defaultGetFormat(overrideUrl, context, defaultGetFormat);
- const parsed = url_1.parse(url);
- if (!isFileUrlOrNodeStyleSpecifier(parsed)) {
- return defer();
- }
- const { pathname } = parsed;
- assert(pathname !== null, 'ESM getFormat() hook: URL should never have null pathname');
-
- const ext = path_1.posix.extname(pathname);
- if (ext === '.ts' || ext === '.tsx' || ext === '.jsx') {
- return defer(url_1.format(Object.assign(Object.assign({}, parsed), { pathname: pathname + '.js' })));
- }
- return defer();
- });
- }
- function transformSource(source, context, defaultTransformSource) {
- return __awaiter(this, void 0, void 0, function* () {
- const defer = () => defaultTransformSource(source, context, defaultTransformSource);
- const sourceAsString = typeof source === 'string' ? source : source.toString('utf8');
- const { url } = context;
- const parsed = url_1.parse(url);
- if (!isFileUrlOrNodeStyleSpecifier(parsed)) {
- return defer();
- }
- const { pathname } = parsed;
- if (pathname === null || !path_1.posix.isAbsolute(pathname)) {
-
- return defer();
- }
-
- const fileName = pathname;
- if (tsNodeInstance.ignored(fileName)) {
- return defer();
- }
- const emittedJs = tsNodeInstance.compile(sourceAsString, fileName);
- return { source: emittedJs };
- });
- }
- }
- exports.registerAndCreateEsmHooks = registerAndCreateEsmHooks;
|