tsconfig-paths
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -10,2 +10,6 @@ # Change Log | ||
## [4.1.0] - 2022-08-06 | ||
- Add support for nested main field selectors #. See PR [#218](https://github.com/dividab/tsconfig-paths/pull/218). Thanks to [@aaronadamsCA](https://github.com/aaronadamsCA) for this PR! | ||
## [4.0.0] - 2022-05-02 | ||
@@ -12,0 +16,0 @@ |
@@ -9,3 +9,3 @@ export interface OneTest { | ||
}; | ||
readonly mainFields?: string[]; | ||
readonly mainFields?: (string | string[])[]; | ||
readonly addMatchAll?: boolean; | ||
@@ -12,0 +12,0 @@ readonly existingFiles: ReadonlyArray<string>; |
@@ -138,2 +138,13 @@ "use strict"; | ||
{ | ||
name: "should resolve nested main fields", | ||
absoluteBaseUrl: "/root/", | ||
paths: { "lib/*": ["location/*"] }, | ||
mainFields: [["esnext", "main"]], | ||
packageJson: { esnext: { main: "./main.js" } }, | ||
existingFiles: [(0, path_1.join)("/root", "location", "mylibjs", "main.js")], | ||
extensions: [".ts", ".js"], | ||
requestedModule: "lib/mylibjs", | ||
expectedPath: (0, path_1.join)("/root", "location", "mylibjs", "main.js"), | ||
}, | ||
{ | ||
name: "should ignore advanced field mappings in package.json", | ||
@@ -140,0 +151,0 @@ absoluteBaseUrl: "/root/", |
@@ -153,2 +153,7 @@ "use strict"; | ||
}); | ||
it("It should throw an error including the file path when encountering invalid JSON5", function () { | ||
expect(function () { | ||
return (0, tsconfig_loader_1.loadTsconfig)("/root/dir1/tsconfig.json", function (path) { return path === "/root/dir1/tsconfig.json"; }, function (_) { return "{\n \"compilerOptions\": {\n }"; }); | ||
}).toThrowError("/root/dir1/tsconfig.json is malformed JSON5: invalid end of input at 3:12"); | ||
}); | ||
it("It should load a config with extends and overwrite all options", function () { | ||
@@ -155,0 +160,0 @@ var firstConfig = { |
@@ -7,3 +7,3 @@ import * as TsConfigLoader2 from "./tsconfig-loader"; | ||
}; | ||
mainFields?: Array<string>; | ||
mainFields?: (string | string[])[]; | ||
addMatchAll?: boolean; | ||
@@ -25,3 +25,3 @@ } | ||
}; | ||
mainFields?: Array<string>; | ||
mainFields?: (string | string[])[]; | ||
addMatchAll?: boolean; | ||
@@ -28,0 +28,0 @@ } |
@@ -5,3 +5,3 @@ /** | ||
export interface PackageJson { | ||
[key: string]: string; | ||
[key: string]: string | PackageJson; | ||
} | ||
@@ -8,0 +8,0 @@ /** |
@@ -17,6 +17,6 @@ import * as MappingEntry from "./mapping-entry"; | ||
[key: string]: Array<string>; | ||
}, mainFields?: string[], addMatchAll?: boolean): MatchPathAsync; | ||
}, mainFields?: (string | string[])[], addMatchAll?: boolean): MatchPathAsync; | ||
/** | ||
* See the sync version for docs. | ||
*/ | ||
export declare function matchFromAbsolutePathsAsync(absolutePathMappings: ReadonlyArray<MappingEntry.MappingEntry>, requestedModule: string, readJson: Filesystem.ReadJsonAsync | undefined, fileExists: Filesystem.FileExistsAsync | undefined, extensions: readonly string[] | undefined, callback: MatchPathAsyncCallback, mainFields?: string[]): void; | ||
export declare function matchFromAbsolutePathsAsync(absolutePathMappings: ReadonlyArray<MappingEntry.MappingEntry>, requestedModule: string, readJson: Filesystem.ReadJsonAsync | undefined, fileExists: Filesystem.FileExistsAsync | undefined, extensions: readonly string[] | undefined, callback: MatchPathAsyncCallback, mainFields?: (string | string[])[]): void; |
@@ -43,3 +43,6 @@ "use strict"; | ||
}; | ||
var mainFieldMapping = packageJson[mainFields[index]]; | ||
var mainFieldSelector = mainFields[index]; | ||
var mainFieldMapping = typeof mainFieldSelector === "string" | ||
? packageJson[mainFieldSelector] | ||
: mainFieldSelector.reduce(function (obj, key) { return obj[key]; }, packageJson); | ||
if (typeof mainFieldMapping !== "string") { | ||
@@ -46,0 +49,0 @@ // Skip mappings that are not pointers to replacement files |
@@ -14,3 +14,3 @@ import * as Filesystem from "./filesystem"; | ||
* @param paths The paths as specified in tsconfig. | ||
* @param mainFields A list of package.json field names to try when resolving module files. | ||
* @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names. | ||
* @param addMatchAll Add a match-all "*" rule if none is present | ||
@@ -21,3 +21,3 @@ * @returns a function that can resolve paths. | ||
[key: string]: Array<string>; | ||
}, mainFields?: string[], addMatchAll?: boolean): MatchPath; | ||
}, mainFields?: (string | string[])[], addMatchAll?: boolean): MatchPath; | ||
/** | ||
@@ -31,5 +31,5 @@ * Finds a path from tsconfig that matches a module load request. | ||
* @param extensions File extensions to probe for (useful for testing). | ||
* @param mainFields A list of package.json field names to try when resolving module files. | ||
* @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names. | ||
* @returns the found path, or undefined if no path was found. | ||
*/ | ||
export declare function matchFromAbsolutePaths(absolutePathMappings: ReadonlyArray<MappingEntry.MappingEntry>, requestedModule: string, readJson?: Filesystem.ReadJsonSync, fileExists?: Filesystem.FileExistsSync, extensions?: Array<string>, mainFields?: string[]): string | undefined; | ||
export declare function matchFromAbsolutePaths(absolutePathMappings: ReadonlyArray<MappingEntry.MappingEntry>, requestedModule: string, readJson?: Filesystem.ReadJsonSync, fileExists?: Filesystem.FileExistsSync, extensions?: Array<string>, mainFields?: (string | string[])[]): string | undefined; |
@@ -13,3 +13,3 @@ "use strict"; | ||
* @param paths The paths as specified in tsconfig. | ||
* @param mainFields A list of package.json field names to try when resolving module files. | ||
* @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names. | ||
* @param addMatchAll Add a match-all "*" rule if none is present | ||
@@ -35,3 +35,3 @@ * @returns a function that can resolve paths. | ||
* @param extensions File extensions to probe for (useful for testing). | ||
* @param mainFields A list of package.json field names to try when resolving module files. | ||
* @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names. | ||
* @returns the found path, or undefined if no path was found. | ||
@@ -53,4 +53,6 @@ */ | ||
for (var index = 0; index < mainFields.length; index++) { | ||
var mainFieldName = mainFields[index]; | ||
var candidateMapping = packageJson[mainFieldName]; | ||
var mainFieldSelector = mainFields[index]; | ||
var candidateMapping = typeof mainFieldSelector === "string" | ||
? packageJson[mainFieldSelector] | ||
: mainFieldSelector.reduce(function (obj, key) { return obj[key]; }, packageJson); | ||
if (candidateMapping && typeof candidateMapping === "string") { | ||
@@ -57,0 +59,0 @@ var candidateFilePath = path.join(path.dirname(packageJsonPath), candidateMapping); |
@@ -92,3 +92,9 @@ "use strict"; | ||
var cleanedJson = StripBom(configString); | ||
var config = JSON5.parse(cleanedJson); | ||
var config; | ||
try { | ||
config = JSON5.parse(cleanedJson); | ||
} | ||
catch (e) { | ||
throw new Error("".concat(configFilePath, " is malformed ").concat(e.message)); | ||
} | ||
var extendedConfig = config.extends; | ||
@@ -95,0 +101,0 @@ if (extendedConfig) { |
{ | ||
"name": "tsconfig-paths", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Load node modules according to tsconfig paths, in run-time or via API.", | ||
@@ -19,2 +19,5 @@ "main": "lib/index.js", | ||
], | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"devDependencies": { | ||
@@ -21,0 +24,0 @@ "@types/jest": "^27.0.3", |
@@ -149,3 +149,3 @@ # tsconfig-paths | ||
paths: { [key: string]: Array<string> }; | ||
mainFields?: Array<string>; | ||
mainFields?: (string | string[])[]; | ||
addMatchAll?: boolean; | ||
@@ -206,3 +206,3 @@ cwd?: string; | ||
* @param paths The paths as specified in tsconfig. | ||
* @param mainFields A list of package.json field names to try when resolving module files. | ||
* @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names. | ||
* @param addMatchAll Add a match-all "*" rule if none is present | ||
@@ -214,3 +214,3 @@ * @returns a function that can resolve paths. | ||
paths: { [key: string]: Array<string> }, | ||
mainFields: string[] = ["main"], | ||
mainFields: (string | string[])[] = ["main"], | ||
addMatchAll: boolean = true | ||
@@ -232,3 +232,3 @@ ): MatchPath { | ||
* @param extensions File extensions to probe for (useful for testing). | ||
* @param mainFields A list of package.json field names to try when resolving module files. | ||
* @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names. | ||
* @returns the found path, or undefined if no path was found. | ||
@@ -242,3 +242,3 @@ */ | ||
extensions: Array<string> = Object.keys(require.extensions), | ||
mainFields: string[] = ["main"] | ||
mainFields: (string | string[])[] = ["main"] | ||
): string | undefined { | ||
@@ -245,0 +245,0 @@ ``` |
@@ -10,3 +10,3 @@ import { join, dirname } from "path"; | ||
readonly paths: { [key: string]: Array<string> }; | ||
readonly mainFields?: string[]; | ||
readonly mainFields?: (string | string[])[]; | ||
readonly addMatchAll?: boolean; | ||
@@ -154,2 +154,13 @@ readonly existingFiles: ReadonlyArray<string>; | ||
{ | ||
name: "should resolve nested main fields", | ||
absoluteBaseUrl: "/root/", | ||
paths: { "lib/*": ["location/*"] }, | ||
mainFields: [["esnext", "main"]], | ||
packageJson: { esnext: { main: "./main.js" } }, | ||
existingFiles: [join("/root", "location", "mylibjs", "main.js")], | ||
extensions: [".ts", ".js"], | ||
requestedModule: "lib/mylibjs", | ||
expectedPath: join("/root", "location", "mylibjs", "main.js"), | ||
}, | ||
{ | ||
name: "should ignore advanced field mappings in package.json", | ||
@@ -156,0 +167,0 @@ absoluteBaseUrl: "/root/", |
@@ -210,2 +210,16 @@ import { | ||
it("It should throw an error including the file path when encountering invalid JSON5", () => { | ||
expect(() => | ||
loadTsconfig( | ||
"/root/dir1/tsconfig.json", | ||
(path) => path === "/root/dir1/tsconfig.json", | ||
(_) => `{ | ||
"compilerOptions": { | ||
}` | ||
) | ||
).toThrowError( | ||
"/root/dir1/tsconfig.json is malformed JSON5: invalid end of input at 3:12" | ||
); | ||
}); | ||
it("It should load a config with extends and overwrite all options", () => { | ||
@@ -212,0 +226,0 @@ const firstConfig = { |
@@ -7,3 +7,3 @@ import * as TsConfigLoader2 from "./tsconfig-loader"; | ||
paths: { [key: string]: Array<string> }; | ||
mainFields?: Array<string>; | ||
mainFields?: (string | string[])[]; | ||
addMatchAll?: boolean; | ||
@@ -28,3 +28,3 @@ } | ||
paths: { [key: string]: Array<string> }; | ||
mainFields?: Array<string>; | ||
mainFields?: (string | string[])[]; | ||
addMatchAll?: boolean; | ||
@@ -31,0 +31,0 @@ } |
@@ -7,3 +7,3 @@ import * as fs from "fs"; | ||
export interface PackageJson { | ||
[key: string]: string; | ||
[key: string]: string | PackageJson; | ||
} | ||
@@ -10,0 +10,0 @@ |
@@ -30,3 +30,3 @@ import * as path from "path"; | ||
paths: { [key: string]: Array<string> }, | ||
mainFields: string[] = ["main"], | ||
mainFields: (string | string[])[] = ["main"], | ||
addMatchAll: boolean = true | ||
@@ -68,3 +68,3 @@ ): MatchPathAsync { | ||
callback: MatchPathAsyncCallback, | ||
mainFields: string[] = ["main"] | ||
mainFields: (string | string[])[] = ["main"] | ||
): void { | ||
@@ -93,3 +93,3 @@ const tryPaths = TryPath.getPathsToTry( | ||
packageJson: Filesystem.PackageJson, | ||
mainFields: string[], | ||
mainFields: (string | string[])[], | ||
packageJsonPath: string, | ||
@@ -114,3 +114,7 @@ fileExistsAsync: Filesystem.FileExistsAsync, | ||
const mainFieldMapping = packageJson[mainFields[index]]; | ||
const mainFieldSelector = mainFields[index]; | ||
const mainFieldMapping = | ||
typeof mainFieldSelector === "string" | ||
? packageJson[mainFieldSelector] | ||
: mainFieldSelector.reduce((obj, key) => obj[key], packageJson); | ||
if (typeof mainFieldMapping !== "string") { | ||
@@ -143,3 +147,3 @@ // Skip mappings that are not pointers to replacement files | ||
index: number = 0, | ||
mainFields: string[] = ["main"] | ||
mainFields: (string | string[])[] = ["main"] | ||
): void { | ||
@@ -146,0 +150,0 @@ const tryPath = tryPaths[index]; |
@@ -23,3 +23,3 @@ import * as path from "path"; | ||
* @param paths The paths as specified in tsconfig. | ||
* @param mainFields A list of package.json field names to try when resolving module files. | ||
* @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names. | ||
* @param addMatchAll Add a match-all "*" rule if none is present | ||
@@ -31,3 +31,3 @@ * @returns a function that can resolve paths. | ||
paths: { [key: string]: Array<string> }, | ||
mainFields: string[] = ["main"], | ||
mainFields: (string | string[])[] = ["main"], | ||
addMatchAll: boolean = true | ||
@@ -65,3 +65,3 @@ ): MatchPath { | ||
* @param extensions File extensions to probe for (useful for testing). | ||
* @param mainFields A list of package.json field names to try when resolving module files. | ||
* @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names. | ||
* @returns the found path, or undefined if no path was found. | ||
@@ -75,3 +75,3 @@ */ | ||
extensions: Array<string> = Object.keys(require.extensions), | ||
mainFields: string[] = ["main"] | ||
mainFields: (string | string[])[] = ["main"] | ||
): string | undefined { | ||
@@ -93,3 +93,3 @@ const tryPaths = TryPath.getPathsToTry( | ||
packageJson: Filesystem.PackageJson, | ||
mainFields: string[], | ||
mainFields: (string | string[])[], | ||
packageJsonPath: string, | ||
@@ -99,4 +99,7 @@ fileExists: Filesystem.FileExistsSync | ||
for (let index = 0; index < mainFields.length; index++) { | ||
const mainFieldName = mainFields[index]; | ||
const candidateMapping = packageJson[mainFieldName]; | ||
const mainFieldSelector = mainFields[index]; | ||
const candidateMapping = | ||
typeof mainFieldSelector === "string" | ||
? packageJson[mainFieldSelector] | ||
: mainFieldSelector.reduce((obj, key) => obj[key], packageJson); | ||
if (candidateMapping && typeof candidateMapping === "string") { | ||
@@ -120,3 +123,3 @@ const candidateFilePath = path.join( | ||
fileExists: Filesystem.FileExistsSync, | ||
mainFields: string[] = ["main"] | ||
mainFields: (string | string[])[] = ["main"] | ||
): string | undefined { | ||
@@ -123,0 +126,0 @@ for (const tryPath of tryPaths) { |
@@ -128,3 +128,8 @@ import * as path from "path"; | ||
const cleanedJson = StripBom(configString); | ||
const config: Tsconfig = JSON5.parse(cleanedJson); | ||
let config: Tsconfig; | ||
try { | ||
config = JSON5.parse(cleanedJson); | ||
} catch (e) { | ||
throw new Error(`${configFilePath} is malformed ${e.message}`); | ||
} | ||
let extendedConfig = config.extends; | ||
@@ -131,0 +136,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
203861
3581