Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,37 @@ myResolver.resolve(

#### Resolver Options

| Field | Default | Description |
| ------------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| alias | [] | A list of module alias configurations or an object which maps key to value |
| aliasFields | [] | A list of alias fields in description files |
| extensionAlias | {} | An object which maps extension to extension aliases |
| cachePredicate | function() { return true }; | A function which decides whether a request should be cached or not. An object is passed to the function with `path` and `request` properties. |
| cacheWithContext | true | If unsafe cache is enabled, includes `request.context` in the cache key |
| conditionNames | [] | A list of exports field condition names |
| descriptionFiles | ["package.json"] | A list of description files to read from |
| enforceExtension | false | Enforce that a extension from extensions must be used |
| exportsFields | ["exports"] | A list of exports fields in description files |
| extensions | [".js", ".json", ".node"] | A list of extensions which should be tried for files |
| fallback | [] | Same as `alias`, but only used if default resolving fails |
| fileSystem | | The file system which should be used |
| fullySpecified | false | Request passed to resolve is already fully specified and extensions or main files are not resolved for it (they are still resolved for internal requests) |
| mainFields | ["main"] | A list of main fields in description files |
| mainFiles | ["index"] | A list of main files in directories |
| modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
| plugins | [] | A list of additional resolve plugins which should be applied |
| resolver | undefined | A prepared Resolver to which the plugins are attached |
| resolveToContext | false | Resolve to a context instead of a file |
| preferRelative | false | Prefer to resolve module requests as relative request and fallback to resolving as module |
| preferAbsolute | false | Prefer to resolve server-relative urls as absolute paths before falling back to resolve in roots |
| restrictions | [] | A list of resolve restrictions |
| roots | [] | A list of root paths |
| symlinks | true | Whether to resolve symlinks to their symlinked location |
| tsconfig | false | TypeScript config for paths mapping. Can be `false` (disabled), `true` (use default `tsconfig.json`), a string path to `tsconfig.json`, or an object with `configFile` and `references` options. |
| tsconfig.configFile | tsconfig.json | Path to the tsconfig.json file |
| tsconfig.references | [] | Project references. `'auto'` to load from tsconfig, or an array of paths to referenced projects |
| unsafeCache | false | Use this cache object to unsafely cache the successful requests |
| Field | Default | Description |
| ------------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| alias | [] | A list of module alias configurations or an object which maps key to value |
| aliasFields | [] | A list of alias fields in description files |
| extensionAlias | {} | An object which maps extension to extension aliases |
| cachePredicate | function() { return true }; | A function which decides whether a request should be cached or not. An object is passed to the function with `path` and `request` properties. |
| cacheWithContext | true | If unsafe cache is enabled, includes `request.context` in the cache key |
| conditionNames | [] | A list of exports field condition names |
| descriptionFiles | ["package.json"] | A list of description files to read from |
| enforceExtension | false | Enforce that a extension from extensions must be used |
| exportsFields | ["exports"] | A list of exports fields in description files |
| extensions | [".js", ".json", ".node"] | A list of extensions which should be tried for files |
| fallback | [] | Same as `alias`, but only used if default resolving fails |
| fileSystem | | The file system which should be used |
| fullySpecified | false | Request passed to resolve is already fully specified and extensions or main files are not resolved for it (they are still resolved for internal requests) |
| mainFields | ["main"] | A list of main fields in description files |
| mainFiles | ["index"] | A list of main files in directories |
| modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
| plugins | [] | A list of additional resolve plugins which should be applied |
| resolver | undefined | A prepared Resolver to which the plugins are attached |
| resolveToContext | false | Resolve to a context instead of a file |
| preferRelative | false | Prefer to resolve module requests as relative request and fallback to resolving as module |
| preferAbsolute | false | Prefer to resolve server-relative urls as absolute paths before falling back to resolve in roots |
| restrictions | [] | A list of resolve restrictions |
| roots | [] | A list of root paths |
| symlinks | true | Whether to resolve symlinks to their symlinked location |
| tsconfig | false | TypeScript config for paths mapping. Can be `false` (disabled), `true` (use default `tsconfig.json`), a string path to `tsconfig.json`, or an object with `configFile`, `references`, and `baseUrl` options. |
| tsconfig.configFile | tsconfig.json | Path to the tsconfig.json file |
| tsconfig.references | [] | Project references. `'auto'` to load from tsconfig, or an array of paths to referenced projects |
| tsconfig.baseUrl | undefined | Override baseUrl from tsconfig.json. If provided, this value will be used instead of the baseUrl in the tsconfig file |
| unsafeCache | false | Use this cache object to unsafely cache the successful requests |

## Plugins

Expand Down
1 change: 1 addition & 0 deletions lib/ResolverFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const { PathType, getType } = require("./util/path");
* @typedef {object} TsconfigOptions
* @property {string=} configFile A relative path to the tsconfig file based on cwd, or an absolute path of tsconfig file
* @property {string[] | "auto"=} references References to other tsconfig files. 'auto' inherits from TypeScript config, or an array of relative/absolute paths
* @property {string=} baseUrl Override baseUrl from tsconfig.json. If provided, this value will be used instead of the baseUrl in the tsconfig file
*/

/**
Expand Down
9 changes: 8 additions & 1 deletion lib/TsconfigPathsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,17 @@ module.exports = class TsconfigPathsPlugin {
} else {
this.references = [];
}
/** @type {string | undefined} */
this.baseUrl = configFileOrOptions.baseUrl;
} else {
this.configFile =
configFileOrOptions === true
? DEFAULT_CONFIG_FILE
: /** @type {string} */ (configFileOrOptions);
/** @type {TsconfigReference[] | "auto"} */
this.references = [];
/** @type {string | undefined} */
this.baseUrl = undefined;
}
}

Expand Down Expand Up @@ -330,10 +334,13 @@ module.exports = class TsconfigPathsPlugin {
const compilerOptions = config.compilerOptions || {};
const mainContext = dirname(absTsconfigPath);

const baseUrl =
this.baseUrl !== undefined ? this.baseUrl : compilerOptions.baseUrl;

const main = tsconfigPathsToResolveOptions(
mainContext,
compilerOptions.paths || {},
compilerOptions.baseUrl,
baseUrl,
);
/** @type {{ [baseUrl: string]: TsconfigPathsData }} */
const refs = {};
Expand Down
57 changes: 57 additions & 0 deletions test/tsconfig-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,63 @@ describe("TsconfigPathsPlugin", () => {
});
});

it("should override baseUrl from tsconfig with option", (done) => {
const resolver = ResolverFactory.createResolver({
fileSystem,
extensions: [".ts", ".tsx"],
mainFields: ["browser", "main"],
mainFiles: ["index"],
tsconfig: {
configFile: path.join(baseExampleDir, "tsconfig.json"),
baseUrl: "./src", // Override baseUrl from tsconfig (which is ".")
},
});

resolver.resolve(
{},
baseExampleDir,
"utils/date", // This should resolve relative to baseExampleDir/src (the overridden baseUrl)
{},
(err, result) => {
if (err) return done(err);
if (!result) return done(new Error("No result"));
expect(result).toEqual(
path.join(baseExampleDir, "src", "utils", "date.ts"),
);
done();
},
);
});

it("should use baseUrl from tsconfig when not overridden", (done) => {
const resolver = ResolverFactory.createResolver({
fileSystem,
extensions: [".ts", ".tsx"],
mainFields: ["browser", "main"],
mainFiles: ["index"],
tsconfig: {
configFile: path.join(baseExampleDir, "tsconfig.json"),
// baseUrl not specified, should use from tsconfig (which is ".")
},
});

// With baseUrl from tsconfig (.), modules should resolve relative to baseExampleDir
resolver.resolve(
{},
baseExampleDir,
"src/utils/date", // This should resolve relative to baseExampleDir (the tsconfig baseUrl)
{},
(err, result) => {
if (err) return done(err);
if (!result) return done(new Error("No result"));
expect(result).toEqual(
path.join(baseExampleDir, "src", "utils", "date.ts"),
);
done();
},
);
});

describe("TypeScript Project References", () => {
it("should support tsconfig object format with configFile", (done) => {
const resolver = ResolverFactory.createResolver({
Expand Down
6 changes: 6 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,11 @@ declare interface TsconfigOptions {
* References to other tsconfig files. 'auto' inherits from TypeScript config, or an array of relative/absolute paths
*/
references?: string[] | "auto";

/**
* Override baseUrl from tsconfig.json. If provided, this value will be used instead of the baseUrl in the tsconfig file
*/
baseUrl?: string;
}
declare interface TsconfigPathsData {
/**
Expand Down Expand Up @@ -1638,6 +1643,7 @@ declare class TsconfigPathsPlugin {
constructor(configFileOrOptions: string | true | TsconfigOptions);
configFile: string;
references: "auto" | TsconfigReference[];
baseUrl?: string;
apply(resolver: Resolver): void;
}
declare interface TsconfigReference {
Expand Down