@visulima/tsconfig 1.1.21 → 1.1.22

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