Skip to content

Commit

Permalink
feat: support . in exports field
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 1, 2021
1 parent a5ee117 commit c857018
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 2 deletions.
4 changes: 2 additions & 2 deletions e2e/resolve-conditions/resolver.js
Expand Up @@ -25,10 +25,10 @@ function createPathFilter(conditions) {

return (
resolveExports(pkg, path, {
// `resolve.exports adds `node` unless `browser` is `false`, so let's add this ugly thing
// `resolve.exports` adds `node` unless `browser` is `true`, so let's add this ugly thing
browser: conditions.includes('browser'),
conditions,
// `resolve.exports adds `import` unless `require` is `false`, so let's add this ugly thing
// `resolve.exports` adds `import` unless `require` is `true`, so let's add this ugly thing
require: conditions.includes('require'),
}) || relativePath
);
Expand Down
1 change: 1 addition & 0 deletions packages/jest-resolve/package.json
Expand Up @@ -23,6 +23,7 @@
"jest-util": "^27.2.4",
"jest-validate": "^27.2.4",
"resolve": "^1.20.0",
"resolve.exports": "^1.0.2",
"slash": "^3.0.0"
},
"devDependencies": {
Expand Down
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions packages/jest-resolve/src/__tests__/resolve.test.ts
Expand Up @@ -146,6 +146,43 @@ describe('findNodeModule', () => {
}),
);
});

describe('conditions', () => {
const conditionsRoot = path.resolve(__dirname, '../__mocks__/conditions');

test('resolves without exports, just main', () => {
const result = Resolver.findNodeModule('main', {
basedir: conditionsRoot,
conditions: ['require'],
});

expect(result).toEqual(
path.resolve(conditionsRoot, './node_modules/main/file.js'),
);
});

test('resolves with import', () => {
const result = Resolver.findNodeModule('import', {
basedir: conditionsRoot,
conditions: ['import'],
});

expect(result).toEqual(
path.resolve(conditionsRoot, './node_modules/import/file.js'),
);
});

test('resolves with require', () => {
const result = Resolver.findNodeModule('require', {
basedir: conditionsRoot,
conditions: ['require'],
});

expect(result).toEqual(
path.resolve(conditionsRoot, './node_modules/require/file.js'),
);
});
});
});

describe('resolveModule', () => {
Expand Down
34 changes: 34 additions & 0 deletions packages/jest-resolve/src/defaultResolver.ts
Expand Up @@ -8,6 +8,10 @@
import * as fs from 'graceful-fs';
import pnpResolver from 'jest-pnp-resolver';
import {Opts as ResolveOpts, sync as resolveSync} from 'resolve';
import {
Options as ResolveExportsOptions,
resolve as resolveExports,
} from 'resolve.exports';
import type {Config} from '@jest/types';
import {tryRealpath} from 'jest-util';

Expand Down Expand Up @@ -43,6 +47,8 @@ export default function defaultResolver(
...options,
isDirectory,
isFile,
packageFilter:
options.packageFilter ?? createPackageFilter(options.conditions),
preserveSymlinks: false,
readPackageSync,
realpathSync,
Expand Down Expand Up @@ -149,3 +155,31 @@ function realpathSync(file: Config.Path): Config.Path {
function readPackageSync(_: unknown, file: Config.Path): PkgJson {
return readPackageCached(file);
}

function createPackageFilter(conditions?: Array<string>) {
const options: ResolveExportsOptions = conditions
? {
// `resolve.exports` adds `node` unless `browser` is `true`, so let's add this ugly thing
browser: conditions.includes('browser'),
conditions,
// `resolve.exports` adds `import` unless `require` is `true`, so let's add this ugly thing
require: conditions.includes('require'),
}
: // no conditions were passed - let's assume this is Jest internal and it should be `require`
{browser: false, require: true};

function attemptExportsFallback(pkg: PkgJson) {
try {
return resolveExports(pkg, '.', options);
} catch {
return undefined;
}
}

return function packageFilter(pkg: PkgJson) {
return {
...pkg,
main: pkg.main ?? attemptExportsFallback(pkg),
};
};
}
8 changes: 8 additions & 0 deletions yarn.lock
Expand Up @@ -13026,6 +13026,7 @@ fsevents@^1.2.7:
jest-util: ^27.2.4
jest-validate: ^27.2.4
resolve: ^1.20.0
resolve.exports: ^1.0.2
slash: ^3.0.0
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -18944,6 +18945,13 @@ react-native@0.64.0:
languageName: node
linkType: hard

"resolve.exports@npm:^1.0.2":
version: 1.0.2
resolution: "resolve.exports@npm:1.0.2"
checksum: 012a46e3ae41c53762abf5b50ea1b4adf2de617bbea1dbc7bf6e609c1ceaedee7782acbc92d443951d5dd0c3a8fb1090ce73285a9ccc24b530e33b5e09ae196f
languageName: node
linkType: hard

"resolve@^1.1.6, resolve@^1.10.0, resolve@^1.14.2, resolve@^1.15.0, resolve@^1.19.0, resolve@^1.20.0, resolve@^1.3.2":
version: 1.20.0
resolution: "resolve@npm:1.20.0"
Expand Down

0 comments on commit c857018

Please sign in to comment.