tsconfig-paths
Advanced tools
Comparing version 3.4.2 to 3.5.0
@@ -10,2 +10,8 @@ # Change Log | ||
## [3.5.0] - 2018-07-28 | ||
### Added | ||
Add support for trailing commas in tsconfig.json (use JSON5 to parse). See issue [#51](https://github.com/dividab/tsconfig-paths/issues/51), and PR [#58](https://github.com/dividab/tsconfig-paths/pull/58). Thanks to [@jshado1](https://github.com/jshado1) for this addition! | ||
## [3.4.2] - 2018-06-30 | ||
@@ -12,0 +18,0 @@ |
@@ -29,2 +29,2 @@ import * as TsConfigLoader from "./tsconfig-loader"; | ||
export declare function loadConfig(cwd?: string): ConfigLoaderResult; | ||
export declare function configLoader({cwd, explicitParams, tsConfigLoader}: ConfigLoaderParams): ConfigLoaderResult; | ||
export declare function configLoader({ cwd, explicitParams, tsConfigLoader }: ConfigLoaderParams): ConfigLoaderResult; |
@@ -28,4 +28,4 @@ "use strict"; | ||
function readJsonFromDiskAsync(path, | ||
// tslint:disable-next-line:no-any | ||
callback) { | ||
// tslint:disable-next-line:no-any | ||
callback) { | ||
fs.readFile(path, "utf8", function (err, result) { | ||
@@ -32,0 +32,0 @@ // If error, assume file did not exist |
@@ -11,6 +11,6 @@ import * as Filesystem from "./filesystem"; | ||
* Creates a function that can resolve paths according to tsconfig paths property. | ||
* @param tsConfigPath The paths where tsconfig.json is located. | ||
* @param baseUrl The baseUrl specified in tsconfig. | ||
* @param paths The paths specified in tsconfig. | ||
* @param absoluteBaseUrl Absolute version of baseUrl as specified in tsconfig. | ||
* @param paths The paths as specified in tsconfig. | ||
* @param mainFields A list of package.json field names to try when resolving module files. | ||
* @returns a function that can resolve paths. | ||
*/ | ||
@@ -17,0 +17,0 @@ export declare function createMatchPath(absoluteBaseUrl: string, paths: { |
@@ -9,6 +9,6 @@ "use strict"; | ||
* Creates a function that can resolve paths according to tsconfig paths property. | ||
* @param tsConfigPath The paths where tsconfig.json is located. | ||
* @param baseUrl The baseUrl specified in tsconfig. | ||
* @param paths The paths specified in tsconfig. | ||
* @param absoluteBaseUrl Absolute version of baseUrl as specified in tsconfig. | ||
* @param paths The paths as specified in tsconfig. | ||
* @param mainFields A list of package.json field names to try when resolving module files. | ||
* @returns a function that can resolve paths. | ||
*/ | ||
@@ -15,0 +15,0 @@ function createMatchPath(absoluteBaseUrl, paths, mainFields) { |
@@ -25,5 +25,4 @@ /** | ||
} | ||
export declare function tsConfigLoader({getEnv, cwd, loadSync}: TsConfigLoaderParams): TsConfigLoaderResult; | ||
export declare function resolveConfigPath(cwd: string, filename?: string): string | undefined; | ||
export declare function tsConfigLoader({ getEnv, cwd, loadSync }: TsConfigLoaderParams): TsConfigLoaderResult; | ||
export declare function walkForTsConfig(directory: string, existsSync?: (path: string) => boolean): string | undefined; | ||
export declare function loadTsconfig(configFilePath: string, existsSync?: (path: string) => boolean, readFileSync?: (filename: string) => string): Tsconfig | undefined; |
@@ -6,4 +6,4 @@ "use strict"; | ||
var deepmerge = require("deepmerge"); | ||
var StripJsonComments = require("strip-json-comments"); | ||
// tslint:disable-next-line:no-require-imports | ||
// tslint:disable:no-require-imports | ||
var JSON5 = require("json5"); | ||
var StripBom = require("strip-bom"); | ||
@@ -48,3 +48,2 @@ function tsConfigLoader(_a) { | ||
} | ||
exports.resolveConfigPath = resolveConfigPath; | ||
function walkForTsConfig(directory, existsSync) { | ||
@@ -73,4 +72,4 @@ if (existsSync === void 0) { existsSync = fs.existsSync; } | ||
var configString = readFileSync(configFilePath); | ||
var cleanedJson = StripBom(StripJsonComments(configString)); | ||
var config = JSON.parse(cleanedJson); | ||
var cleanedJson = StripBom(configString); | ||
var config = JSON5.parse(cleanedJson); | ||
var extendedConfig = config.extends; | ||
@@ -77,0 +76,0 @@ if (extendedConfig) { |
{ | ||
"name": "tsconfig-paths", | ||
"version": "3.4.2", | ||
"version": "3.5.0", | ||
"description": "Load node modules according to tsconfig paths, in run-time or via API.", | ||
@@ -32,6 +32,7 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@types/json5": "^0.0.29", | ||
"deepmerge": "^2.0.1", | ||
"json5": "^1.0.1", | ||
"minimist": "^1.2.0", | ||
"strip-bom": "^3.0.0", | ||
"strip-json-comments": "^2.0.1" | ||
"strip-bom": "^3.0.0" | ||
}, | ||
@@ -38,0 +39,0 @@ "scripts": { |
@@ -20,3 +20,5 @@ # tsconfig-paths | ||
``` | ||
or | ||
``` | ||
@@ -29,5 +31,7 @@ npm install --save-dev tsconfig-paths | ||
### With node | ||
`node -r tsconfig-paths/register main.js` | ||
### With ts-node | ||
`ts-node -r tsconfig-paths/register main.ts` | ||
@@ -42,2 +46,3 @@ | ||
### With mocha and ts-node | ||
As of Mocha >= 4.0.0 the `--compiler` was [deprecated](https://github.com/mochajs/mocha/wiki/compilers-deprecation). Instead `--require` should be used. You also have to specify a glob that includes `.ts` files because mocha looks after files with `.js` extension by default. | ||
@@ -49,4 +54,10 @@ | ||
### Bootstrap tsconfig-paths with explicit params | ||
### With other commands | ||
As long as the command has something similar to a `--require` option that can load a module before it starts, tsconfig-paths should be able to work with it. | ||
## Bootstraping with explicit params | ||
If you want more granular control over tsconfig-paths you can bootstrap it. This can be useful if you for instance have compiled with `tsc` to another directory where `tsconfig.json` doesn't exists. | ||
```javascript | ||
@@ -58,6 +69,7 @@ const tsConfig = require("./tsconfig.json"); | ||
tsConfigPaths.register({ | ||
baseUrl, | ||
paths: tsConfig.compilerOptions.paths | ||
baseUrl, | ||
paths: tsConfig.compilerOptions.paths | ||
}); | ||
``` | ||
Then run with: | ||
@@ -79,9 +91,10 @@ | ||
* `-P, --project [path]` Path to TypeScript JSON project file (`TS_NODE_PROJECT`) | ||
- `-P, --project [path]` Path to TypeScript JSON project file (`TS_NODE_PROJECT`) | ||
## Config loading process | ||
1. Use explicit params passed to register | ||
2. Use `process.env.TS_NODE_PROJECT` to resolve tsConfig.json and the specified baseUrl and paths. | ||
3. Resolves tsconfig.json from current working directory and the specified baseUrl and paths. | ||
1. Use explicit params passed to register | ||
2. Use `process.env.TS_NODE_PROJECT` to resolve tsConfig.json and the specified baseUrl and paths. | ||
3. Resolves tsconfig.json from current working directory and the specified baseUrl and paths. | ||
## Programmatic use | ||
@@ -91,6 +104,27 @@ | ||
- [register](#register) | ||
- [loadConfig](#loadConfig) | ||
- [createMatchPath](#createMatchPath) / [createMatchPathAsync](#createMatchPathAsync) | ||
- [matchFromAbsolutePaths](#matchFromAbsolutePaths) / [matchFromAbsolutePathsAsync](#matchFromAbsolutePathsAsync) | ||
### register | ||
```typescript | ||
export interface ExplicitParams { | ||
baseUrl: string; | ||
paths: { [key: string]: Array<string> }; | ||
} | ||
/** | ||
* Installs a custom module load function that can adhere to paths in tsconfig. | ||
*/ | ||
export function register(explicitParams: ExplicitParams): void; | ||
``` | ||
This function will patch the node's module loading so it will look for modules in paths specified by tsconfig.json. | ||
### loadConfig | ||
```typescript | ||
export function loadConfig(cwd: string = process.cwd()): ConfigLoaderResult | ||
export function loadConfig(cwd: string = process.cwd()): ConfigLoaderResult; | ||
@@ -132,6 +166,6 @@ export type ConfigLoaderResult = | ||
* Creates a function that can resolve paths according to tsconfig paths property. | ||
* @param tsConfigPath The paths where tsconfig.json is located. | ||
* @param baseUrl The baseUrl specified in tsconfig. | ||
* @param paths The paths specified in tsconfig. | ||
* @param absoluteBaseUrl Absolute version of baseUrl as specified in tsconfig. | ||
* @param paths The paths as specified in tsconfig. | ||
* @param mainFields A list of package.json field names to try when resolving module files. | ||
* @returns a function that can resolve paths. | ||
*/ | ||
@@ -143,3 +177,2 @@ export function createMatchPath( | ||
): MatchPath { | ||
``` | ||
@@ -146,0 +179,0 @@ |
@@ -119,2 +119,16 @@ import { assert } from "chai"; | ||
it("It should load a config with trailing commas", () => { | ||
const config = { compilerOptions: { baseUrl: "hej" } }; | ||
const res = loadTsconfig( | ||
"/root/dir1/tsconfig.json", | ||
path => path === "/root/dir1/tsconfig.json", | ||
_ => `{ | ||
"compilerOptions": { | ||
"baseUrl": "hej", | ||
}, | ||
}` | ||
); | ||
assert.deepEqual(res, config); | ||
}); | ||
it("It should load a config with extends and overwrite baseUrl", () => { | ||
@@ -121,0 +135,0 @@ const firstConfig = { |
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
151603
1564
216
5
+ Added@types/json5@^0.0.29
+ Addedjson5@^1.0.1
+ Added@types/json5@0.0.29(transitive)
+ Addedjson5@1.0.2(transitive)
- Removedstrip-json-comments@^2.0.1
- Removedstrip-json-comments@2.0.1(transitive)