Skip to content

Commit

Permalink
Consolidate to just defaultResolver in resolver options
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Aug 31, 2021
1 parent f7dd81b commit 179224d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
5 changes: 2 additions & 3 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -793,8 +793,7 @@ The options object provided to resolvers has the shape:
```json
{
"basedir": string,
"defaultResolver": "function(request, options) -> string",
"defaultResolverAsync": "function(request, options) -> Promise<string>",
"defaultResolver": "function(request, options) -> string | Promise<string>",
"extensions": [string],
"moduleDirectory": [string],
"paths": [string],
Expand All @@ -803,7 +802,7 @@ The options object provided to resolvers has the shape:
}
```

Note: the defaultResolver passed as an option is the Jest default resolver which might be useful when you write your custom one. It takes the same arguments as your custom one, e.g. `(request, options)`.
Note: the defaultResolver passed as an option is the Jest default resolver which might be useful when you write your custom one. It takes the same arguments as your custom one, e.g. `(request, options)` and returns a string for sync resolvers and a promise for async resolvers.

For example, if you want to respect Browserify's [`"browser"` field](https://github.com/browserify/browserify-handbook/blob/master/readme.markdown#browser-field), you can use the following configuration:

Expand Down
17 changes: 8 additions & 9 deletions packages/jest-resolve/src/defaultResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export type ResolverOptions = {
packageFilter?: (pkg: any, pkgfile: string) => any;
};

type ResolverOptionsAsync = ResolverOptions & {
defaultResolverAsync: typeof defaultResolverAsync;
type ResolverOptionsAsync = Omit<ResolverOptions, 'defaultResolver'> & {
defaultResolver: typeof defaultResolverAsync;
};

// https://github.com/facebook/jest/pull/10617
Expand Down Expand Up @@ -54,17 +54,14 @@ export function defaultResolver(
return realpathSync(result);
}

export function defaultResolverAsync(
export async function defaultResolverAsync(
path: Config.Path,
options: ResolverOptionsAsync,
): Promise<Config.Path> {
// Yarn 2 adds support to `resolve` automatically so the pnpResolver is only
// needed for Yarn 1 which implements version 1 of the pnp spec
if (process.versions.pnp === '1') {
// QUESTION: do we need an async version of pnpResolver?
// It seems ugly to require a default sync resolver in the async method,
// just to deal with this.
return Promise.resolve(pnpResolver(path, options));
return Promise.resolve(await pnpResolver(path, options));
}

return new Promise((resolve, reject) => {
Expand All @@ -85,7 +82,9 @@ export function defaultResolverAsync(
* getBaseResolveOptions returns resolution options that are shared by both the
* synch and async resolution functions.
*/
function getBaseResolveOptions(options: ResolverOptions) {
function getBaseResolveOptions(
options: ResolverOptions | ResolverOptionsAsync,
) {
return {
basedir: options.basedir,
extensions: options.extensions,
Expand All @@ -112,7 +111,7 @@ function getSyncResolveOptions(options: ResolverOptions): SyncOpts {
/**
* getAsyncResolveOptions returns resolution options that are used asynchronously.
*/
function getAsyncResolveOptions(options: ResolverOptions): AsyncOpts {
function getAsyncResolveOptions(options: ResolverOptionsAsync): AsyncOpts {
return {
...getBaseResolveOptions(options),
isDirectory: isDirectoryAsync,
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-resolve/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ export default class Resolver {
const result = await resolver(path, {
basedir: options.basedir,
browser: options.browser,
defaultResolver,
defaultResolverAsync,
defaultResolver: defaultResolverAsync,
extensions: options.extensions,
moduleDirectory: options.moduleDirectory,
paths: paths ? (nodePaths || []).concat(paths) : nodePaths,
Expand Down

0 comments on commit 179224d

Please sign in to comment.