From d0dd838521324a80fea069eb7f97945a8d0041d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 7 Feb 2022 00:02:52 +0100 Subject: fix(compat): ESM resolver for package subpath (#13599) --- cli/compat/esm_resolver.rs | 18 +++++++++++++++++- cli/compat/testdata/subpath/main.js | 2 ++ cli/compat/testdata/subpath/node_modules/foo/index.js | 0 .../testdata/subpath/node_modules/foo/package.json | 4 ++++ cli/compat/testdata/subpath/node_modules/foo/server.js | 0 5 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 cli/compat/testdata/subpath/main.js create mode 100644 cli/compat/testdata/subpath/node_modules/foo/index.js create mode 100644 cli/compat/testdata/subpath/node_modules/foo/package.json create mode 100644 cli/compat/testdata/subpath/node_modules/foo/server.js (limited to 'cli') diff --git a/cli/compat/esm_resolver.rs b/cli/compat/esm_resolver.rs index 13eb1efe6..357a95de8 100644 --- a/cli/compat/esm_resolver.rs +++ b/cli/compat/esm_resolver.rs @@ -903,7 +903,7 @@ fn parse_package_name( } let package_name = if let Some(index) = separator_index { - specifier[0..=index].to_string() + specifier[0..index].to_string() } else { specifier.to_string() }; @@ -1195,6 +1195,22 @@ mod tests { assert_eq!(actual.to_result().unwrap(), expected); } + #[test] + fn package_subpath() { + let cwd = testdir("subpath"); + let main = Url::from_file_path(cwd.join("main.js")).unwrap(); + let actual = node_resolve("foo", main.as_str(), &cwd).unwrap(); + let expected = + Url::from_file_path(cwd.join("node_modules/foo/index.js")).unwrap(); + matches!(actual, ResolveResponse::CommonJs(_)); + assert_eq!(actual.to_result().unwrap(), expected); + let actual = node_resolve("foo/server.js", main.as_str(), &cwd).unwrap(); + let expected = + Url::from_file_path(cwd.join("node_modules/foo/server.js")).unwrap(); + matches!(actual, ResolveResponse::CommonJs(_)); + assert_eq!(actual.to_result().unwrap(), expected); + } + #[test] fn basic_deps() { let cwd = testdir("basic_deps"); diff --git a/cli/compat/testdata/subpath/main.js b/cli/compat/testdata/subpath/main.js new file mode 100644 index 000000000..ed1fe019e --- /dev/null +++ b/cli/compat/testdata/subpath/main.js @@ -0,0 +1,2 @@ +import "foo"; +import "foo/server.js"; diff --git a/cli/compat/testdata/subpath/node_modules/foo/index.js b/cli/compat/testdata/subpath/node_modules/foo/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/cli/compat/testdata/subpath/node_modules/foo/package.json b/cli/compat/testdata/subpath/node_modules/foo/package.json new file mode 100644 index 000000000..26f2d565a --- /dev/null +++ b/cli/compat/testdata/subpath/node_modules/foo/package.json @@ -0,0 +1,4 @@ +{ + "name": "foo", + "main": "index.js" +} \ No newline at end of file diff --git a/cli/compat/testdata/subpath/node_modules/foo/server.js b/cli/compat/testdata/subpath/node_modules/foo/server.js new file mode 100644 index 000000000..e69de29bb -- cgit v1.2.3