1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 01:29:14 -05:00

fix(npm/types): resolve main entrypoint declaration file when no types entry (#16791)

Closes #16782
This commit is contained in:
David Sherret 2022-11-24 13:13:51 -05:00 committed by Bartek Iwańczuk
parent 31abacbe1a
commit 184c9b9b28
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
7 changed files with 43 additions and 13 deletions

View file

@ -306,6 +306,14 @@ itest!(types_entry_value_not_exists {
exit_code: 0,
});
itest!(types_no_types_entry {
args: "run --check=all npm/types_no_types_entry/main.ts",
output: "npm/types_no_types_entry/main.out",
envs: env_vars(),
http_server: true,
exit_code: 0,
});
#[test]
fn parallel_downloading() {
let (out, _err) = util::run_and_collect_output_with_args(

View file

@ -0,0 +1 @@
export function getValue(): 5;

View file

@ -0,0 +1 @@
module.exports.getValue = () => 5;

View file

@ -0,0 +1,5 @@
{
"name": "@denotest/types-no-types-entry",
"version": "1.0.0",
"main": "./dist/main.js"
}

View file

@ -0,0 +1,4 @@
Download http://localhost:4545/npm/registry/@denotest/types-no-types-entry
Download http://localhost:4545/npm/registry/@denotest/types-no-types-entry/1.0.0.tgz
Check file://[WILDCARD]/types_no_types_entry/main.ts
5

View file

@ -0,0 +1,4 @@
import { getValue } from "npm:@denotest/types-no-types-entry";
const result: 5 = getValue();
console.log(result);

View file

@ -806,19 +806,31 @@ pub fn legacy_main_resolve(
) -> Result<Option<PathBuf>, AnyError> {
let is_types = conditions == TYPES_CONDITIONS;
let maybe_main = if is_types {
package_json.types.as_ref()
match package_json.types.as_ref() {
Some(types) => Some(types),
None => {
// fallback to checking the main entrypoint for
// a corresponding declaration file
if let Some(main) = package_json.main(referrer_kind) {
let main = package_json.path.parent().unwrap().join(main).clean();
let path = path_to_declaration_path(main, referrer_kind);
if path.exists() {
return Ok(Some(path));
}
}
None
}
}
} else {
package_json.main(referrer_kind)
};
let mut guess;
if let Some(main) = maybe_main {
guess = package_json.path.parent().unwrap().join(main).clean();
let guess = package_json.path.parent().unwrap().join(main).clean();
if file_exists(&guess) {
return Ok(Some(guess));
}
let mut found = false;
// todo(dsherret): investigate exactly how node and typescript handles this
let endings = if is_types {
match referrer_kind {
@ -838,22 +850,17 @@ pub fn legacy_main_resolve(
vec![".js", "/index.js"]
};
for ending in endings {
guess = package_json
let guess = package_json
.path
.parent()
.unwrap()
.join(&format!("{}{}", main, ending))
.clean();
if file_exists(&guess) {
found = true;
break;
// TODO(bartlomieju): emitLegacyIndexDeprecation()
return Ok(Some(guess));
}
}
if found {
// TODO(bartlomieju): emitLegacyIndexDeprecation()
return Ok(Some(guess));
}
}
let index_file_names = if is_types {
@ -866,7 +873,7 @@ pub fn legacy_main_resolve(
vec!["index.js"]
};
for index_file_name in index_file_names {
guess = package_json
let guess = package_json
.path
.parent()
.unwrap()