1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(node): handle empty 'main' entry in pkg json (#23155)

This commit is contained in:
David Sherret 2024-04-01 02:07:11 -04:00 committed by GitHub
parent b8af46e007
commit 8698492128
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 43 additions and 4 deletions

View file

@ -213,12 +213,13 @@ impl PackageJson {
Ok(package_json)
}
pub fn main(&self, referrer_kind: NodeModuleKind) -> Option<&String> {
if referrer_kind == NodeModuleKind::Esm && self.typ == "module" {
pub fn main(&self, referrer_kind: NodeModuleKind) -> Option<&str> {
let main = if referrer_kind == NodeModuleKind::Esm && self.typ == "module" {
self.module.as_ref().or(self.main.as_ref())
} else {
self.main.as_ref()
}
};
main.map(|m| m.trim()).filter(|m| !m.is_empty())
}
pub fn specifier(&self) -> ModuleSpecifier {

View file

@ -1232,7 +1232,7 @@ impl NodeResolver {
) -> Result<Option<ModuleSpecifier>, AnyError> {
let maybe_main = if mode.is_types() {
match package_json.types.as_ref() {
Some(types) => Some(types),
Some(types) => Some(types.as_str()),
None => {
// fallback to checking the main entrypoint for
// a corresponding declaration file

View file

@ -0,0 +1,10 @@
{
"steps": [{
"args": "check index.ts",
"output": "check.out",
"exitCode": 1
}, {
"args": "run index.ts",
"output": "run.out"
}]
}

View file

@ -0,0 +1,5 @@
Check file:///[WILDLINE]/index.ts
error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'.
const result: string = add(1, 2);
~~~~~~
at file:///[WILDLINE]/index.ts:4:7

View file

@ -0,0 +1,3 @@
{
"unstable": ["byonm"]
}

View file

@ -0,0 +1,5 @@
// this package has an empty "main" entry in its package.json for both the package and @types/package
import { add } from "package";
const result: string = add(1, 2);
console.log(result);

View file

@ -0,0 +1 @@
export function add(a: number, b: number): number;

View file

@ -0,0 +1,3 @@
{
"main": ""
}

View file

@ -0,0 +1 @@
module.exports.add = (a, b) => a + b;

View file

@ -0,0 +1,3 @@
{
"main": ""
}

View file

@ -0,0 +1,6 @@
{
"dependencies": {
"package": "*",
"@types/package": "*"
}
}

View file

@ -0,0 +1 @@
3