load-tsconfig
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -1,21 +0,1 @@ | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
// src/index.ts | ||
@@ -26,3 +6,3 @@ import path from "path"; | ||
// node_modules/.pnpm/strip-json-comments@4.0.0/node_modules/strip-json-comments/index.js | ||
// node_modules/.pnpm/strip-json-comments@5.0.0/node_modules/strip-json-comments/index.js | ||
var singleComment = Symbol("singleComment"); | ||
@@ -41,3 +21,3 @@ var multiComment = Symbol("multiComment"); | ||
}; | ||
function stripJsonComments(jsonString, { whitespace = true } = {}) { | ||
function stripJsonComments(jsonString, { whitespace = true, trailingCommas = false } = {}) { | ||
if (typeof jsonString !== "string") { | ||
@@ -50,3 +30,5 @@ throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof jsonString}\``); | ||
let offset = 0; | ||
let buffer = ""; | ||
let result = ""; | ||
let commaIndex = -1; | ||
for (let index = 0; index < jsonString.length; index++) { | ||
@@ -65,3 +47,3 @@ const currentCharacter = jsonString[index]; | ||
if (!isInsideComment && currentCharacter + nextCharacter === "//") { | ||
result += jsonString.slice(offset, index); | ||
buffer += jsonString.slice(offset, index); | ||
offset = index; | ||
@@ -73,3 +55,3 @@ isInsideComment = singleComment; | ||
isInsideComment = false; | ||
result += strip(jsonString, offset, index); | ||
buffer += strip(jsonString, offset, index); | ||
offset = index; | ||
@@ -79,6 +61,6 @@ continue; | ||
isInsideComment = false; | ||
result += strip(jsonString, offset, index); | ||
buffer += strip(jsonString, offset, index); | ||
offset = index; | ||
} else if (!isInsideComment && currentCharacter + nextCharacter === "/*") { | ||
result += jsonString.slice(offset, index); | ||
buffer += jsonString.slice(offset, index); | ||
offset = index; | ||
@@ -91,8 +73,27 @@ isInsideComment = multiComment; | ||
isInsideComment = false; | ||
result += strip(jsonString, offset, index + 1); | ||
buffer += strip(jsonString, offset, index + 1); | ||
offset = index + 1; | ||
continue; | ||
} else if (trailingCommas && !isInsideComment) { | ||
if (commaIndex !== -1) { | ||
if (currentCharacter === "}" || currentCharacter === "]") { | ||
buffer += jsonString.slice(offset, index); | ||
result += strip(buffer, 0, 1) + buffer.slice(1); | ||
buffer = ""; | ||
offset = index; | ||
commaIndex = -1; | ||
} else if (currentCharacter !== " " && currentCharacter !== " " && currentCharacter !== "\r" && currentCharacter !== "\n") { | ||
buffer += jsonString.slice(offset, index); | ||
offset = index; | ||
commaIndex = -1; | ||
} | ||
} else if (currentCharacter === ",") { | ||
result += buffer + jsonString.slice(offset, index); | ||
buffer = ""; | ||
offset = index; | ||
commaIndex = index; | ||
} | ||
} | ||
} | ||
return result + (isInsideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset)); | ||
return result + buffer + (isInsideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset)); | ||
} | ||
@@ -117,2 +118,7 @@ | ||
return file; | ||
if (!file.endsWith(".json")) { | ||
const fileWithExt = file + ".json"; | ||
if (fs.existsSync(fileWithExt)) | ||
return fileWithExt; | ||
} | ||
dir = path.dirname(dir); | ||
@@ -135,4 +141,4 @@ } | ||
}; | ||
var loadTsConfigInternal = (dir = process.cwd(), name = "tsconfig.json", files = [], isExtends = false) => { | ||
var _a; | ||
var loadTsConfigInternal = (dir = process.cwd(), name = "tsconfig.json", isExtends = false) => { | ||
var _a, _b; | ||
dir = path.resolve(dir); | ||
@@ -143,17 +149,37 @@ const id = isExtends ? resolveTsConfigFromExtends(dir, name) : resolveTsConfigFromFile(dir, name); | ||
const data = jsoncParse(fs.readFileSync(id, "utf-8")); | ||
files.unshift(id); | ||
const configDir = path.dirname(id); | ||
if ((_a = data.compilerOptions) == null ? void 0 : _a.baseUrl) { | ||
data.compilerOptions.baseUrl = path.join(configDir, data.compilerOptions.baseUrl); | ||
data.compilerOptions.baseUrl = path.join( | ||
configDir, | ||
data.compilerOptions.baseUrl | ||
); | ||
} | ||
let extendsFiles = []; | ||
if (data.extends) { | ||
const parentConfig = loadTsConfigInternal(configDir, data.extends, files, true); | ||
if (parentConfig) { | ||
Object.assign(data, __spreadProps(__spreadValues(__spreadValues({}, parentConfig.data), data), { | ||
compilerOptions: __spreadValues(__spreadValues({}, parentConfig.data.compilerOptions), data.compilerOptions) | ||
})); | ||
const extendsList = Array.isArray(data.extends) ? data.extends : [data.extends]; | ||
const extendsData = {}; | ||
for (const name2 of extendsList) { | ||
const parentConfig = loadTsConfigInternal(configDir, name2, true); | ||
if (parentConfig) { | ||
Object.assign(extendsData, { | ||
...parentConfig == null ? void 0 : parentConfig.data, | ||
compilerOptions: { | ||
...extendsData.compilerOptions, | ||
...(_b = parentConfig == null ? void 0 : parentConfig.data) == null ? void 0 : _b.compilerOptions | ||
} | ||
}); | ||
extendsFiles.push(...parentConfig.files); | ||
} | ||
} | ||
Object.assign(data, { | ||
...extendsData, | ||
...data, | ||
compilerOptions: { | ||
...extendsData.compilerOptions, | ||
...data.compilerOptions | ||
} | ||
}); | ||
} | ||
delete data.extends; | ||
return { path: id, data, files }; | ||
return { path: id, data, files: [...extendsFiles, id] }; | ||
}; | ||
@@ -160,0 +186,0 @@ var loadTsConfig = (dir, name) => loadTsConfigInternal(dir, name); |
{ | ||
"name": "load-tsconfig", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Load tsconfig.json", | ||
@@ -21,3 +21,3 @@ "publishConfig": { | ||
"build-fast": "tsup src/index.ts --format esm,cjs --target node12.20.0", | ||
"build": "pnpm build-fast -- --dts-resolve", | ||
"build": "pnpm build-fast --dts-resolve", | ||
"test": "npm run build-fast && vitest run", | ||
@@ -29,9 +29,9 @@ "prepublishOnly": "pnpm build" | ||
"@egoist/prettier-config": "1.0.0", | ||
"@types/node": "14.18.9", | ||
"kanpai": "0.10.1", | ||
"prettier": "2.5.1", | ||
"strip-json-comments": "4.0.0", | ||
"tsup": "5.11.11", | ||
"typescript": "4.5.5", | ||
"vitest": "0.2.5" | ||
"@types/node": "18.15.3", | ||
"kanpai": "0.11.0", | ||
"prettier": "2.8.4", | ||
"strip-json-comments": "5.0.0", | ||
"tsup": "6.6.3", | ||
"typescript": "5.0.2", | ||
"vitest": "0.29.3" | ||
}, | ||
@@ -38,0 +38,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17306
387
5