Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support of imports field #13723

Merged
merged 8 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Empty file added copy.mjs
Empty file.
2 changes: 1 addition & 1 deletion packages/jest-resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"./package.json": "./package.json"
},
"dependencies": {
"@okikio/resolve.imports": "^1.0.0",
"chalk": "^4.0.0",
"graceful-fs": "^4.2.9",
"jest-haste-map": "workspace:^",
Expand All @@ -26,6 +25,7 @@
"jest-validate": "workspace:^",
"resolve": "^1.20.0",
"resolve.exports": "^1.1.0",
"resolve.imports": "^2.0.0",
"slash": "^3.0.0"
},
"devDependencies": {
Expand Down
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "array-import",
"imports": {
"#nested": {
"import": ["./node.mjs", "./node-2.mjs"],
"browser": ["./not-exist.cjs", "./browser.cjs"],
"require": ["not-exist-package", "./node.cjs"]
}
}
}
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "nested-import",
"imports": {
"#nested": {
"node": {
"import": "./node.mjs",
"require": "./node.cjs"
},
"default": "./browser.cjs"
}
}
}
45 changes: 45 additions & 0 deletions packages/jest-resolve/src/__tests__/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,51 @@ describe('findNodeModule', () => {
);
});

test('supports nested pattern', () => {
const result = Resolver.findNodeModule('#nested', {
basedir: path.resolve(importsRoot, './nested-import/index.cjs'),
conditions: ['node', 'require'],
});

expect(result).toEqual(
path.resolve(importsRoot, './nested-import/node.cjs'),
);
});

test('supports array pattern - resolve to first found', () => {
unional marked this conversation as resolved.
Show resolved Hide resolved
const result = Resolver.findNodeModule('#array-import', {
basedir: path.resolve(importsRoot, './array-import/index.cjs'),
conditions: ['import'],
});

expect(result).toEqual(
path.resolve(importsRoot, './array-import/node.mjs'),
);
});

test('supports array pattern - ignore not exist internal file', () => {
const result = Resolver.findNodeModule('#array-import', {
basedir: path.resolve(importsRoot, './array-import/index.cjs'),
conditions: ['browser'],
});

expect(result).toEqual(
path.resolve(importsRoot, './array-import/browser.cjs'),
);
});

test('supports array pattern - ignore not exist external module', () => {
// this is for optional dependency
const result = Resolver.findNodeModule('#array-import', {
basedir: path.resolve(importsRoot, './array-import/index.cjs'),
conditions: ['require'],
});

expect(result).toEqual(
path.resolve(importsRoot, './array-import/node.cjs'),
);
});

test('fails for non-existent mapping', () => {
expect(() => {
Resolver.findNodeModule('#something-else', {
Expand Down
35 changes: 25 additions & 10 deletions packages/jest-resolve/src/defaultResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
*/

import {dirname, isAbsolute, resolve as pathResolve} from 'path';
import {resolve as resolveImports} from '@okikio/resolve.imports';
import pnpResolver from 'jest-pnp-resolver';
import {SyncOpts as UpstreamResolveOptions, sync as resolveSync} from 'resolve';
import {
Options as ResolveExportsOptions,
resolve as resolveExports,
} from 'resolve.exports';
import {resolve as resolveImports} from 'resolve.imports';
import {
findClosestPackageJson,
isDirectory,
Expand Down Expand Up @@ -110,9 +110,7 @@ const defaultResolver: SyncResolver = (path, options) => {
realpathSync,
};

const pathToResolve = getPathInModule(path, resolveOptions);

const result = resolveSync(pathToResolve, resolveOptions);
const result = resolveByPathInModule(path, resolveOptions);

unional marked this conversation as resolved.
Show resolved Hide resolved
// Dereference symlinks to ensure we don't create a separate
// module instance depending on how it was referenced.
Expand All @@ -129,7 +127,7 @@ function readPackageSync(_: unknown, file: string): PackageJSON {
return readPackageCached(file);
}

function getPathInModule(
function resolveByPathInModule(
path: string,
options: UpstreamResolveOptionsWithConditions,
): string {
Expand All @@ -149,7 +147,11 @@ function getPathInModule(
const pkg = readPackageCached(closestPackageJson);

const resolved = resolveImports(
pkg,
{
base: options.basedir,
content: pkg,
path: closestPackageJson,
},
path,
createResolveOptions(options.conditions),
);
Expand All @@ -160,12 +162,25 @@ function getPathInModule(
);
}

if (resolved.startsWith('.')) {
return pathResolve(dirname(closestPackageJson), resolved);
const resolvedValues = Array.isArray(resolved) ? resolved : [resolved];

for (const resolved of resolvedValues) {
const resolvedPath = resolveByPath(resolved);
try {
return resolveSync(resolvedPath, options);
unional marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
unional marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
}

// this is an external module, re-resolve it
return defaultResolver(resolved, options);
function resolveByPath(resolved: string) {
if (resolved.startsWith('.')) {
return pathResolve(dirname(closestPackageJson!), resolved);
}

// this is an external module, re-resolve it
return defaultResolver(resolved, options);
}
}

const segments = path.split('/');
Expand Down
25 changes: 17 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3721,13 +3721,6 @@ __metadata:
languageName: node
linkType: hard

"@okikio/resolve.imports@npm:^1.0.0":
version: 1.0.0
resolution: "@okikio/resolve.imports@npm:1.0.0"
checksum: a06d347731b3c47e79125d346dd0172fda3cf20d138249f4ed23c5cc60b32f668f9bbab49698fc061ef5782d236622ff4cd0ce2b2520550273e863f55b687350
languageName: node
linkType: hard

"@pkgr/utils@npm:^2.3.1":
version: 2.3.1
resolution: "@pkgr/utils@npm:2.3.1"
Expand Down Expand Up @@ -12891,7 +12884,6 @@ __metadata:
version: 0.0.0-use.local
resolution: "jest-resolve@workspace:packages/jest-resolve"
dependencies:
"@okikio/resolve.imports": ^1.0.0
"@tsd/typescript": ^4.9.0
"@types/graceful-fs": ^4.1.3
"@types/pnpapi": ^0.0.2
Expand All @@ -12904,6 +12896,7 @@ __metadata:
jest-validate: "workspace:^"
resolve: ^1.20.0
resolve.exports: ^1.1.0
resolve.imports: ^2.0.0
slash: ^3.0.0
tsd-lite: ^0.6.0
languageName: unknown
Expand Down Expand Up @@ -16422,6 +16415,13 @@ __metadata:
languageName: node
linkType: hard

"pattern-key-compare@npm:^2.0.0":
version: 2.0.0
resolution: "pattern-key-compare@npm:2.0.0"
checksum: cded15070cddbc5ef7b97c1b91371bcf59ff931f8afe383d2323f935891e6c4517bd76154f5edc2b1ab53c6fbeeb0711b9db43af592064482d0a9ccf03dd507c
languageName: node
linkType: hard

"pend@npm:~1.2.0":
version: 1.2.0
resolution: "pend@npm:1.2.0"
Expand Down Expand Up @@ -18366,6 +18366,15 @@ __metadata:
languageName: node
linkType: hard

"resolve.imports@npm:^2.0.0":
version: 2.0.0
resolution: "resolve.imports@npm:2.0.0"
dependencies:
pattern-key-compare: ^2.0.0
checksum: ad914fcd84f656888ada484a45f4ff09429b99cd896a4a4a28bbc0676a795a528a7760125e297951d8b314584fb72da428a09d0e7f1d60d4d48bfb093d996609
languageName: node
linkType: hard

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