@visulima/tsconfig 1.1.21 → 1.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,470 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
4
-
5
- const fs = require('@visulima/fs');
6
- const error = require('@visulima/fs/error');
7
- const path = require('@visulima/path');
8
- const utils = require('@visulima/path/utils');
9
- const jsoncParser = require('jsonc-parser');
10
- const node_fs = require('node:fs');
11
- const Module = require('node:module');
12
- const resolvePkgMaps = require('resolve-pkg-maps');
13
-
14
- const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
15
-
16
- const Module__default = /*#__PURE__*/_interopDefaultCompat(Module);
17
-
18
- var __defProp$1 = Object.defineProperty;
19
- var __name$1 = (target, value) => __defProp$1(target, "name", { value, configurable: true });
20
- const readJsonc$1 = /* @__PURE__ */ __name$1((jsonPath) => jsoncParser.parse(fs.readFileSync(jsonPath, { buffer: false })), "readJsonc");
21
- const getPnpApi = /* @__PURE__ */ __name$1(() => {
22
- const { findPnpApi } = Module__default;
23
- return findPnpApi?.(process.cwd());
24
- }, "getPnpApi");
25
- const resolveFromPackageJsonPath = /* @__PURE__ */ __name$1((packageJsonPath, subpath, ignoreExports, cache) => {
26
- const cacheKey = "resolveFromPackageJsonPath:" + packageJsonPath + ":" + subpath + ":" + (ignoreExports ? "yes" : "no");
27
- if (cache?.has(cacheKey)) {
28
- return cache.get(cacheKey);
29
- }
30
- const packageJson = readJsonc$1(packageJsonPath);
31
- if (!packageJson) {
32
- return void 0;
33
- }
34
- let resolvedPath = subpath || "tsconfig.json";
35
- if (!ignoreExports && packageJson.exports) {
36
- try {
37
- const [resolvedExport] = resolvePkgMaps.resolveExports(packageJson.exports, subpath, ["require", "types"]);
38
- resolvedPath = resolvedExport;
39
- } catch {
40
- return false;
41
- }
42
- } else if (!subpath && packageJson.tsconfig) {
43
- resolvedPath = packageJson.tsconfig;
44
- }
45
- resolvedPath = path.join(packageJsonPath, "..", resolvedPath);
46
- cache?.set(cacheKey, resolvedPath);
47
- return resolvedPath;
48
- }, "resolveFromPackageJsonPath");
49
- const PACKAGE_JSON = "package.json";
50
- const TS_CONFIG_JSON = "tsconfig.json";
51
- const resolveExtendsPath = /* @__PURE__ */ __name$1((requestedPath, directoryPath, cache) => {
52
- let filePath = requestedPath;
53
- if (requestedPath === "..") {
54
- filePath = path.join(filePath, TS_CONFIG_JSON);
55
- }
56
- if (requestedPath.startsWith(".")) {
57
- filePath = path.resolve(directoryPath, filePath);
58
- }
59
- if (path.isAbsolute(filePath)) {
60
- if (fs.isAccessibleSync(filePath)) {
61
- if (node_fs.statSync(filePath).isFile()) {
62
- return filePath;
63
- }
64
- } else if (!filePath.endsWith(".json")) {
65
- const jsonPath = `${filePath}.json`;
66
- if (fs.isAccessibleSync(jsonPath)) {
67
- return jsonPath;
68
- }
69
- }
70
- return void 0;
71
- }
72
- const [orgOrName, ...remaining] = requestedPath.split("/");
73
- const packageName = orgOrName.startsWith("@") ? orgOrName + "/" + remaining.shift() : orgOrName;
74
- const subpath = remaining.join("/");
75
- const pnpApi = getPnpApi();
76
- if (pnpApi) {
77
- const { resolveRequest: resolveWithPnp } = pnpApi;
78
- try {
79
- if (packageName === requestedPath) {
80
- const packageJsonPath2 = resolveWithPnp(path.join(packageName, PACKAGE_JSON), directoryPath);
81
- if (packageJsonPath2) {
82
- const resolvedPath = resolveFromPackageJsonPath(packageJsonPath2, subpath, false, cache);
83
- if (resolvedPath && fs.isAccessibleSync(resolvedPath)) {
84
- return resolvedPath;
85
- }
86
- }
87
- } else {
88
- let resolved;
89
- try {
90
- resolved = resolveWithPnp(requestedPath, directoryPath, { extensions: [".json"] });
91
- } catch {
92
- resolved = resolveWithPnp(path.join(requestedPath, TS_CONFIG_JSON), directoryPath);
93
- }
94
- if (resolved) {
95
- return resolved;
96
- }
97
- }
98
- } catch {
99
- }
100
- }
101
- const packagePath = fs.findUpSync(
102
- (directory) => {
103
- const path$1 = path.join(path.resolve(directory), "node_modules", packageName);
104
- if (fs.isAccessibleSync(path$1)) {
105
- return path.join("node_modules", packageName);
106
- }
107
- return void 0;
108
- },
109
- {
110
- cwd: directoryPath,
111
- type: "directory"
112
- }
113
- );
114
- if (!packagePath || !node_fs.statSync(packagePath).isDirectory()) {
115
- return void 0;
116
- }
117
- const packageJsonPath = path.join(packagePath, PACKAGE_JSON);
118
- if (fs.isAccessibleSync(packageJsonPath)) {
119
- const resolvedPath = resolveFromPackageJsonPath(packageJsonPath, subpath, false, cache);
120
- if (resolvedPath === false) {
121
- return void 0;
122
- }
123
- if (resolvedPath && fs.isAccessibleSync(resolvedPath) && node_fs.statSync(resolvedPath).isFile()) {
124
- return resolvedPath;
125
- }
126
- }
127
- const fullPackagePath = path.join(packagePath, subpath);
128
- const jsonExtension = fullPackagePath.endsWith(".json");
129
- if (!jsonExtension) {
130
- const fullPackagePathWithJson = fullPackagePath + ".json";
131
- if (fs.isAccessibleSync(fullPackagePathWithJson)) {
132
- return fullPackagePathWithJson;
133
- }
134
- }
135
- if (!fs.isAccessibleSync(fullPackagePath)) {
136
- return void 0;
137
- }
138
- if (node_fs.statSync(fullPackagePath).isDirectory()) {
139
- const fullPackageJsonPath = path.join(fullPackagePath, PACKAGE_JSON);
140
- if (fs.isAccessibleSync(fullPackageJsonPath)) {
141
- const resolvedPath = resolveFromPackageJsonPath(fullPackageJsonPath, "", true, cache);
142
- if (resolvedPath && fs.isAccessibleSync(resolvedPath)) {
143
- return resolvedPath;
144
- }
145
- }
146
- const tsconfigPath = path.join(fullPackagePath, TS_CONFIG_JSON);
147
- if (fs.isAccessibleSync(tsconfigPath)) {
148
- return tsconfigPath;
149
- }
150
- } else if (jsonExtension) {
151
- return fullPackagePath;
152
- }
153
- return void 0;
154
- }, "resolveExtendsPath");
155
-
156
- var __defProp = Object.defineProperty;
157
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
158
- const readJsonc = /* @__PURE__ */ __name((jsonPath) => jsoncParser.parse(fs.readFileSync(jsonPath, { buffer: false })), "readJsonc");
159
- const normalizePath = /* @__PURE__ */ __name((path$1) => {
160
- const namespacedPath = path.toNamespacedPath(path$1);
161
- return utils.isRelative(namespacedPath) ? namespacedPath : "./" + namespacedPath;
162
- }, "normalizePath");
163
- const filesProperties = ["files", "include", "exclude"];
164
- const resolveExtends = /* @__PURE__ */ __name((resolvedExtendsPath, fromDirectoryPath, circularExtendsTracker, options) => {
165
- if (circularExtendsTracker.has(resolvedExtendsPath)) {
166
- throw new Error(`Circularity detected while resolving configuration: ${resolvedExtendsPath}`);
167
- }
168
- circularExtendsTracker.add(resolvedExtendsPath);
169
- const extendsDirectoryPath = path.dirname(resolvedExtendsPath);
170
- const extendsConfig = internalParseTsConfig(resolvedExtendsPath, options, circularExtendsTracker);
171
- delete extendsConfig.references;
172
- const { compilerOptions } = extendsConfig;
173
- if (compilerOptions) {
174
- const { baseUrl } = compilerOptions;
175
- if (baseUrl && !baseUrl.startsWith(configDirectoryPlaceholder)) {
176
- compilerOptions.baseUrl = path.normalize(path.relative(fromDirectoryPath, path.join(extendsDirectoryPath, baseUrl))) || "./";
177
- }
178
- let { outDir } = compilerOptions;
179
- if (outDir) {
180
- if (!outDir.startsWith(configDirectoryPlaceholder)) {
181
- outDir = path.relative(fromDirectoryPath, path.join(extendsDirectoryPath, outDir));
182
- }
183
- compilerOptions.outDir = normalizePath(outDir.replace(configDirectoryPlaceholder + "/", "")) || "./";
184
- }
185
- }
186
- for (const property of filesProperties) {
187
- const filesList = extendsConfig[property];
188
- if (filesList) {
189
- extendsConfig[property] = filesList.map((file) => {
190
- if (file.startsWith(configDirectoryPlaceholder)) {
191
- return file;
192
- }
193
- if (path.isAbsolute(file)) {
194
- return file;
195
- }
196
- return path.relative(fromDirectoryPath, path.join(extendsDirectoryPath, file));
197
- });
198
- }
199
- }
200
- return extendsConfig;
201
- }, "resolveExtends");
202
- const internalParseTsConfig = /* @__PURE__ */ __name((tsconfigPath, options, circularExtendsTracker = /* @__PURE__ */ new Set()) => {
203
- let config;
204
- try {
205
- config = readJsonc(tsconfigPath) || {};
206
- } catch {
207
- throw new Error(`Cannot resolve tsconfig at path: ${tsconfigPath}`);
208
- }
209
- if (typeof config !== "object") {
210
- throw new SyntaxError(`Failed to parse tsconfig at: ${tsconfigPath}`);
211
- }
212
- const directoryPath = path.dirname(tsconfigPath);
213
- if (config.compilerOptions) {
214
- const { compilerOptions } = config;
215
- if (compilerOptions.paths && !compilerOptions.baseUrl) {
216
- compilerOptions[implicitBaseUrlSymbol] = directoryPath;
217
- }
218
- }
219
- if (config.extends) {
220
- const extendsPathList = Array.isArray(config.extends) ? config.extends : [config.extends];
221
- delete config.extends;
222
- for (const extendsPath of extendsPathList.reverse()) {
223
- const resolvedExtendsPath = resolveExtendsPath(extendsPath, directoryPath);
224
- if (!resolvedExtendsPath) {
225
- throw new error.NotFoundError("No such file or directory, for '" + extendsPath + "' found.");
226
- }
227
- const extendsConfig = resolveExtends(resolvedExtendsPath, directoryPath, new Set(circularExtendsTracker), options);
228
- if (extendsConfig.compilerOptions?.rootDir !== void 0 && !extendsConfig.compilerOptions.rootDir.startsWith(configDirectoryPlaceholder)) {
229
- extendsConfig.compilerOptions.rootDir = path.join(path.dirname(resolvedExtendsPath), extendsConfig.compilerOptions.rootDir);
230
- }
231
- const merged = {
232
- ...extendsConfig,
233
- ...config,
234
- compilerOptions: {
235
- ...extendsConfig.compilerOptions,
236
- ...config.compilerOptions
237
- }
238
- };
239
- if (extendsConfig.watchOptions) {
240
- merged.watchOptions = {
241
- ...extendsConfig.watchOptions,
242
- ...config.watchOptions
243
- };
244
- }
245
- config = merged;
246
- }
247
- }
248
- if (config.compilerOptions) {
249
- const { compilerOptions } = config;
250
- for (const property of ["baseUrl", "rootDir"]) {
251
- const unresolvedPath = compilerOptions[property];
252
- if (unresolvedPath && !unresolvedPath.startsWith(configDirectoryPlaceholder)) {
253
- const resolvedBaseUrl = path.resolve(directoryPath, unresolvedPath);
254
- compilerOptions[property] = normalizePath(path.relative(directoryPath, resolvedBaseUrl));
255
- }
256
- }
257
- for (const outputField of ["outDir", "declarationDir"]) {
258
- let outputPath = compilerOptions[outputField];
259
- if (outputPath) {
260
- if (!Array.isArray(config.exclude)) {
261
- config.exclude = [];
262
- }
263
- let excludePath = outputPath;
264
- if (!path.isAbsolute(excludePath)) {
265
- excludePath = path.join(directoryPath, excludePath);
266
- }
267
- excludePath = excludePath.replace(configDirectoryPlaceholder, "");
268
- if (!config.exclude.includes(excludePath)) {
269
- config.exclude.push(excludePath);
270
- }
271
- if (!outputPath.startsWith(configDirectoryPlaceholder)) {
272
- outputPath = normalizePath(outputPath);
273
- }
274
- compilerOptions[outputField] = outputPath;
275
- }
276
- }
277
- } else {
278
- config.compilerOptions = {};
279
- }
280
- if (config.include) {
281
- config.include = config.include.map((element) => path.normalize(element));
282
- if (config.files) {
283
- delete config.files;
284
- }
285
- } else if (config.files) {
286
- config.files = config.files.map((file) => file.startsWith(configDirectoryPlaceholder) ? file : normalizePath(file));
287
- }
288
- if (config.watchOptions) {
289
- const { watchOptions } = config;
290
- if (watchOptions.excludeDirectories) {
291
- watchOptions.excludeDirectories = watchOptions.excludeDirectories.map((excludePath) => path.resolve(directoryPath, excludePath));
292
- }
293
- }
294
- if (config.compilerOptions?.lib) {
295
- config.compilerOptions.lib = config.compilerOptions.lib.map((library) => library.toLowerCase());
296
- }
297
- if (config.compilerOptions.module) {
298
- config.compilerOptions.module = config.compilerOptions.module.toLowerCase();
299
- }
300
- if (config.compilerOptions.target) {
301
- config.compilerOptions.target = config.compilerOptions.target.toLowerCase();
302
- }
303
- return config;
304
- }, "internalParseTsConfig");
305
- const interpolateConfigDirectory = /* @__PURE__ */ __name((filePath, configDirectory) => {
306
- if (filePath.startsWith(configDirectoryPlaceholder)) {
307
- return path.normalize(path.join(configDirectory, filePath.slice(configDirectoryPlaceholder.length)));
308
- }
309
- return void 0;
310
- }, "interpolateConfigDirectory");
311
- const compilerFieldsWithConfigDirectory = ["outDir", "declarationDir", "outFile", "rootDir", "baseUrl", "tsBuildInfoFile"];
312
- const tsCompatibleWrapper = /* @__PURE__ */ __name((config, options) => {
313
- if (config.compilerOptions === void 0) {
314
- return config;
315
- }
316
- if (["5.4", "5.5", "5.6", "5.7", "5.8", "true"].includes(String(options?.tscCompatible))) {
317
- if (config.compilerOptions.esModuleInterop === void 0 && (config.compilerOptions.module === "node16" || config.compilerOptions.module === "nodenext" || config.compilerOptions.module === "preserve")) {
318
- config.compilerOptions.esModuleInterop = true;
319
- }
320
- if ((config.compilerOptions.esModuleInterop || config.compilerOptions.module === "system" || config.compilerOptions.moduleResolution === "bundler") && config.compilerOptions.allowSyntheticDefaultImports === void 0) {
321
- config.compilerOptions.allowSyntheticDefaultImports = true;
322
- }
323
- if (config?.compilerOptions.moduleDetection === void 0 && config.compilerOptions.module && ["node16", "nodenext"].includes(config.compilerOptions.module)) {
324
- config.compilerOptions.moduleDetection = "force";
325
- }
326
- if (config.compilerOptions.moduleResolution === void 0) {
327
- let moduleResolution = "classic";
328
- if (config.compilerOptions.module !== void 0) {
329
- switch ((config.compilerOptions?.module).toLocaleLowerCase()) {
330
- case "commonjs": {
331
- moduleResolution = "node10";
332
- break;
333
- }
334
- case "node16": {
335
- moduleResolution = "node16";
336
- break;
337
- }
338
- case "nodenext": {
339
- moduleResolution = "nodenext";
340
- break;
341
- }
342
- case "preserve": {
343
- moduleResolution = "bundler";
344
- break;
345
- }
346
- }
347
- }
348
- if (moduleResolution !== "classic") {
349
- config.compilerOptions.moduleResolution = moduleResolution;
350
- }
351
- }
352
- if (["5.7", "5.8", "true"].includes(String(options?.tscCompatible)) && config.compilerOptions.moduleResolution) {
353
- let resolvePackageJson = false;
354
- if (["bundler", "node16", "nodenext"].includes(config.compilerOptions.moduleResolution.toLocaleLowerCase())) {
355
- resolvePackageJson = true;
356
- }
357
- if (config.compilerOptions.resolvePackageJsonExports === void 0 && resolvePackageJson) {
358
- config.compilerOptions.resolvePackageJsonExports = true;
359
- }
360
- if (config.compilerOptions.resolvePackageJsonImports === void 0 && resolvePackageJson) {
361
- config.compilerOptions.resolvePackageJsonImports = true;
362
- }
363
- }
364
- if (config.compilerOptions.target === void 0) {
365
- let target = "es5";
366
- if (config.compilerOptions.module === "node16") {
367
- target = "es2022";
368
- } else if (config.compilerOptions.module === "nodenext") {
369
- target = "esnext";
370
- }
371
- if (target !== "es5") {
372
- config.compilerOptions.target = target;
373
- }
374
- }
375
- if (config.compilerOptions.useDefineForClassFields === void 0 && config.compilerOptions.target && (config.compilerOptions.target.includes("es202") || config.compilerOptions.target === "esnext")) {
376
- config.compilerOptions.useDefineForClassFields = true;
377
- }
378
- }
379
- if (["5.6", "5.7", "5.8", "true"].includes(String(options?.tscCompatible)) && config.compilerOptions.strict && config.compilerOptions.strictBuiltinIteratorReturn === void 0) {
380
- config.compilerOptions.strictBuiltinIteratorReturn = true;
381
- }
382
- if (["5.4", "5.5", "5.6", "5.7", "5.8", "true"].includes(String(options?.tscCompatible))) {
383
- if (config.compilerOptions.strict) {
384
- config.compilerOptions.noImplicitAny = config.compilerOptions.noImplicitAny ?? true;
385
- config.compilerOptions.noImplicitThis = config.compilerOptions.noImplicitThis ?? true;
386
- config.compilerOptions.strictNullChecks = config.compilerOptions.strictNullChecks ?? true;
387
- config.compilerOptions.strictFunctionTypes = config.compilerOptions.strictFunctionTypes ?? true;
388
- config.compilerOptions.strictBindCallApply = config.compilerOptions.strictBindCallApply ?? true;
389
- config.compilerOptions.strictPropertyInitialization = config.compilerOptions.strictPropertyInitialization ?? true;
390
- config.compilerOptions.alwaysStrict = config.compilerOptions.alwaysStrict ?? true;
391
- }
392
- if (config.compilerOptions.useDefineForClassFields === void 0 && config.compilerOptions.target) {
393
- let useDefineForClassFields = false;
394
- if (config.compilerOptions.target.includes("es202") || config.compilerOptions.target === "esnext") {
395
- useDefineForClassFields = true;
396
- }
397
- if (useDefineForClassFields) {
398
- config.compilerOptions.useDefineForClassFields = true;
399
- }
400
- }
401
- if (config.compilerOptions.strict && config.compilerOptions.useUnknownInCatchVariables === void 0) {
402
- config.compilerOptions.useUnknownInCatchVariables = true;
403
- }
404
- if (config.compilerOptions.isolatedModules) {
405
- config.compilerOptions.preserveConstEnums = config.compilerOptions.preserveConstEnums ?? true;
406
- }
407
- }
408
- if (config.compileOnSave === false) {
409
- delete config.compileOnSave;
410
- }
411
- return config;
412
- }, "tsCompatibleWrapper");
413
- const configDirectoryPlaceholder = "${configDir}";
414
- const implicitBaseUrlSymbol = Symbol("implicitBaseUrl");
415
- const readTsConfig = /* @__PURE__ */ __name((tsconfigPath, options) => {
416
- const resolvedTsconfigPath = path.resolve(tsconfigPath);
417
- const config = internalParseTsConfig(resolvedTsconfigPath, options);
418
- const configDirectory = path.dirname(resolvedTsconfigPath);
419
- const { compilerOptions } = config;
420
- if (compilerOptions) {
421
- for (const property of compilerFieldsWithConfigDirectory) {
422
- const value = compilerOptions[property];
423
- if (value) {
424
- const resolvedPath = interpolateConfigDirectory(value, configDirectory);
425
- compilerOptions[property] = resolvedPath ? normalizePath(path.relative(configDirectory, resolvedPath)) : value;
426
- }
427
- }
428
- for (const property of ["rootDirs", "typeRoots"]) {
429
- const value = compilerOptions[property];
430
- if (value) {
431
- compilerOptions[property] = value.map((v) => {
432
- const resolvedPath = interpolateConfigDirectory(v, configDirectory);
433
- return resolvedPath ? normalizePath(path.relative(configDirectory, resolvedPath)) : v;
434
- });
435
- }
436
- }
437
- const { paths } = compilerOptions;
438
- if (paths) {
439
- for (const name of Object.keys(paths)) {
440
- paths[name] = paths[name].map((filePath) => interpolateConfigDirectory(filePath, configDirectory) ?? filePath);
441
- }
442
- }
443
- if (compilerOptions.outDir) {
444
- compilerOptions.outDir = compilerOptions.outDir.replace(configDirectoryPlaceholder, "");
445
- }
446
- }
447
- for (const property of filesProperties) {
448
- const value = config[property];
449
- if (value) {
450
- config[property] = value.map((filePath) => {
451
- const interpolate = interpolateConfigDirectory(filePath, configDirectory);
452
- if (interpolate) {
453
- return interpolate;
454
- }
455
- if (property === "files" && utils.isRelative(filePath)) {
456
- return filePath;
457
- }
458
- if (property === "include" && utils.isRelative(filePath)) {
459
- return path.join(configDirectory, filePath);
460
- }
461
- return path.normalize(filePath);
462
- });
463
- }
464
- }
465
- return tsCompatibleWrapper(config, options);
466
- }, "readTsConfig");
467
-
468
- exports.configDirectoryPlaceholder = configDirectoryPlaceholder;
469
- exports.implicitBaseUrlSymbol = implicitBaseUrlSymbol;
470
- exports.readTsConfig = readTsConfig;