1 | ;
|
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.loadModule = exports.tsNodeRegister = void 0;
|
13 | const path_1 = require("path");
|
14 | const url_1 = require("url");
|
15 | const _tsNodeRegister = (() => {
|
16 | let lastTsConfig;
|
17 | return (tsConfig, logger) => {
|
18 | // Check if the function was previously called with the same tsconfig
|
19 | if (lastTsConfig && lastTsConfig !== tsConfig) {
|
20 | logger.warn(`Trying to register ts-node again with a different tsconfig - skipping the registration.
|
21 | tsconfig 1: ${lastTsConfig}
|
22 | tsconfig 2: ${tsConfig}`);
|
23 | }
|
24 | if (lastTsConfig) {
|
25 | return;
|
26 | }
|
27 | lastTsConfig = tsConfig;
|
28 | loadTsNode().register({
|
29 | project: tsConfig,
|
30 | compilerOptions: {
|
31 | module: 'CommonJS',
|
32 | types: [
|
33 | 'node', // NOTE: `node` is added because users scripts can also use pure node's packages as webpack or others
|
34 | ],
|
35 | },
|
36 | });
|
37 | const tsConfigPaths = loadTsConfigPaths();
|
38 | const result = tsConfigPaths.loadConfig(tsConfig);
|
39 | // The `loadConfig` returns a `ConfigLoaderResult` which must be guarded with
|
40 | // the `resultType` check.
|
41 | if (result.resultType === 'success') {
|
42 | const { absoluteBaseUrl: baseUrl, paths } = result;
|
43 | if (baseUrl && paths) {
|
44 | tsConfigPaths.register({ baseUrl, paths });
|
45 | }
|
46 | }
|
47 | };
|
48 | })();
|
49 | /**
|
50 | * check for TS node registration
|
51 | * @param file: file name or file directory are allowed
|
52 | * @todo tsNodeRegistration: require ts-node if file extension is TypeScript
|
53 | */
|
54 | function tsNodeRegister(file = '', tsConfig, logger) {
|
55 | if (file === null || file === void 0 ? void 0 : file.endsWith('.ts')) {
|
56 | // Register TS compiler lazily
|
57 | _tsNodeRegister(tsConfig, logger);
|
58 | }
|
59 | }
|
60 | exports.tsNodeRegister = tsNodeRegister;
|
61 | /**
|
62 | * This uses a dynamic import to load a module which may be ESM.
|
63 | * CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
|
64 | * will currently, unconditionally downlevel dynamic import into a require call.
|
65 | * require calls cannot load ESM code and will result in a runtime error. To workaround
|
66 | * this, a Function constructor is used to prevent TypeScript from changing the dynamic import.
|
67 | * Once TypeScript provides support for keeping the dynamic import this workaround can
|
68 | * be dropped.
|
69 | *
|
70 | * @param modulePath The path of the module to load.
|
71 | * @returns A Promise that resolves to the dynamically imported module.
|
72 | */
|
73 | function loadEsmModule(modulePath) {
|
74 | return new Function('modulePath', `return import(modulePath);`)(modulePath);
|
75 | }
|
76 | /**
|
77 | * Loads CJS and ESM modules based on extension
|
78 | * @param path path to the module
|
79 | * @returns
|
80 | */
|
81 | function loadModule(path) {
|
82 | return __awaiter(this, void 0, void 0, function* () {
|
83 | switch ((0, path_1.extname)(path)) {
|
84 | case '.mjs':
|
85 | // Load the ESM configuration file using the TypeScript dynamic import workaround.
|
86 | // Once TypeScript provides support for keeping the dynamic import this workaround can be
|
87 | // changed to a direct dynamic import.
|
88 | return (yield loadEsmModule((0, url_1.pathToFileURL)(path))).default;
|
89 | case '.cjs':
|
90 | return require(path);
|
91 | case '.ts':
|
92 | try {
|
93 | // If it's a TS file then there are 2 cases for exporing an object.
|
94 | // The first one is `export blah`, transpiled into `module.exports = { blah} `.
|
95 | // The second is `export default blah`, transpiled into `{ default: { ... } }`.
|
96 | return require(path).default || require(path);
|
97 | }
|
98 | catch (e) {
|
99 | if (e.code === 'ERR_REQUIRE_ESM') {
|
100 | // Load the ESM configuration file using the TypeScript dynamic import workaround.
|
101 | // Once TypeScript provides support for keeping the dynamic import this workaround can be
|
102 | // changed to a direct dynamic import.
|
103 | return (yield loadEsmModule((0, url_1.pathToFileURL)(path))).default;
|
104 | }
|
105 | throw e;
|
106 | }
|
107 | //.js
|
108 | default:
|
109 | // The file could be either CommonJS or ESM.
|
110 | // CommonJS is tried first then ESM if loading fails.
|
111 | try {
|
112 | return require(path);
|
113 | }
|
114 | catch (e) {
|
115 | if (e.code === 'ERR_REQUIRE_ESM') {
|
116 | // Load the ESM configuration file using the TypeScript dynamic import workaround.
|
117 | // Once TypeScript provides support for keeping the dynamic import this workaround can be
|
118 | // changed to a direct dynamic import.
|
119 | return (yield loadEsmModule((0, url_1.pathToFileURL)(path))).default;
|
120 | }
|
121 | throw e;
|
122 | }
|
123 | }
|
124 | });
|
125 | }
|
126 | exports.loadModule = loadModule;
|
127 | /**
|
128 | * Loads `ts-node` lazily. Moved to a separate function to declare
|
129 | * a return type, more readable than an inline variant.
|
130 | */
|
131 | function loadTsNode() {
|
132 | return require('ts-node');
|
133 | }
|
134 | /**
|
135 | * Loads `tsconfig-paths` lazily. Moved to a separate function to declare
|
136 | * a return type, more readable than an inline variant.
|
137 | */
|
138 | function loadTsConfigPaths() {
|
139 | return require('tsconfig-paths');
|
140 | }
|
141 | //# sourceMappingURL=utils.js.map |
\ | No newline at end of file |