Repository: webpack/enhanced-resolve (js)

Issue:
not working with local package.json
I created minimal webpack project:
```
mkdir -p test/lib
cd test
npm init -f
npm i webpack webpack-cli
touch lib/index.js
```
`package.json`:
```
{
    "name": "webpack-imports",
    "version": "1.0.0",
    "type": "module",
    "imports": {
        "#index": "./lib/index.js"
    },
    "devDependencies": {
        "webpack": "^5.36.0",
        "webpack-cli": "^4.6.0"
    }
}
```
`lib/main.js`
```
import "#index";
```

Then run:
```
npx webpack build --entry ./lib/main.js
```

Error:
```
assets by status 126 bytes [cached] 1 asset
./lib/main.js 17 bytes [built] [code generated]

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value.
Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/

ERROR in ./lib/main.js 1:0-16
Module not found: Error: Can't resolve '#index' in '/mnt/hgfs/projects/work/tmp/webpack-imports/lib'
resolve '#index' in '/mnt/hgfs/projects/work/tmp/webpack-imports/lib'
  using description file: /mnt/hgfs/projects/work/tmp/webpack-imports/package.json (relative path: ./lib)
    Field 'browser' doesn't contain a valid alias configuration

webpack 5.36.0 compiled with 1 error and 1 warning in 200 ms
```

_Originally posted by @zdm in https://github.com/webpack/webpack/discussions/13254#discussioncomment-669831_

Pull request:
allow to resolve imports from a subpath
fixes #290

Patch:
diff --git a/lib/ImportsFieldPlugin.js b/lib/ImportsFieldPlugin.js
index e5b9877..8be64a0 100644
--- a/lib/ImportsFieldPlugin.js
+++ b/lib/ImportsFieldPlugin.js
@@ -54,15 +54,9 @@ module.exports = class ImportsFieldPlugin {
 			.getHook(this.source)
 			.tapAsync("ImportsFieldPlugin", (request, resolveContext, callback) => {
 				// When there is no description file, abort
-				if (!request.descriptionFilePath) return callback();
-
-				if (
-					// When the description file is inherited from parent, abort
-					// (There is no description file inside of this package)
-					request.relativePath !== "." ||
-					request.request === undefined
-				)
+				if (!request.descriptionFilePath || request.request === undefined) {
 					return callback();
+				}
 
 				const remainingRequest =
 					request.request + request.query + request.fragment;


Test patch:
diff --git a/test/fixtures/imports-field/dir/b.js b/test/fixtures/imports-field/dir/b.js
new file mode 100644
index 0000000..e69de29
diff --git a/test/importsField.js b/test/importsField.js
index 0f8e1c7..a075a31 100644
--- a/test/importsField.js
+++ b/test/importsField.js
@@ -1196,6 +1196,21 @@ describe("ImportsFieldPlugin", () => {
 		});
 	});
 
+	it("should resolve using imports field instead of self-referencing for a subpath", done => {
+		resolver.resolve(
+			{},
+			path.resolve(fixture, "dir"),
+			"#imports-field",
+			{},
+			(err, result) => {
+				if (err) return done(err);
+				if (!result) throw new Error("No result");
+				result.should.equal(path.resolve(fixture, "b.js"));
+				done();
+			}
+		);
+	});
+
 	it("should resolve out of package scope", done => {
 		resolver.resolve({}, fixture, "#b", {}, (err, result) => {
 			if (err) return done(err);