mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 15:49:44 -05:00
fix(node): handle empty 'main' entry in pkg json (#23155)
This commit is contained in:
parent
b8af46e007
commit
8698492128
12 changed files with 43 additions and 4 deletions
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
10
tests/specs/check/npm_pkg_empty_main_entry/__test__.jsonc
Normal file
10
tests/specs/check/npm_pkg_empty_main_entry/__test__.jsonc
Normal file
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"steps": [{
|
||||
"args": "check index.ts",
|
||||
"output": "check.out",
|
||||
"exitCode": 1
|
||||
}, {
|
||||
"args": "run index.ts",
|
||||
"output": "run.out"
|
||||
}]
|
||||
}
|
5
tests/specs/check/npm_pkg_empty_main_entry/check.out
Normal file
5
tests/specs/check/npm_pkg_empty_main_entry/check.out
Normal 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
|
3
tests/specs/check/npm_pkg_empty_main_entry/deno.json
Normal file
3
tests/specs/check/npm_pkg_empty_main_entry/deno.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"unstable": ["byonm"]
|
||||
}
|
5
tests/specs/check/npm_pkg_empty_main_entry/index.ts
Normal file
5
tests/specs/check/npm_pkg_empty_main_entry/index.ts
Normal 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);
|
1
tests/specs/check/npm_pkg_empty_main_entry/node_modules/@types/package/index.d.ts
generated
vendored
Normal file
1
tests/specs/check/npm_pkg_empty_main_entry/node_modules/@types/package/index.d.ts
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
export function add(a: number, b: number): number;
|
3
tests/specs/check/npm_pkg_empty_main_entry/node_modules/@types/package/package.json
generated
vendored
Normal file
3
tests/specs/check/npm_pkg_empty_main_entry/node_modules/@types/package/package.json
generated
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"main": ""
|
||||
}
|
1
tests/specs/check/npm_pkg_empty_main_entry/node_modules/package/index.js
generated
vendored
Normal file
1
tests/specs/check/npm_pkg_empty_main_entry/node_modules/package/index.js
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
module.exports.add = (a, b) => a + b;
|
3
tests/specs/check/npm_pkg_empty_main_entry/node_modules/package/package.json
generated
vendored
Normal file
3
tests/specs/check/npm_pkg_empty_main_entry/node_modules/package/package.json
generated
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"main": ""
|
||||
}
|
6
tests/specs/check/npm_pkg_empty_main_entry/package.json
Normal file
6
tests/specs/check/npm_pkg_empty_main_entry/package.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"package": "*",
|
||||
"@types/package": "*"
|
||||
}
|
||||
}
|
1
tests/specs/check/npm_pkg_empty_main_entry/run.out
Normal file
1
tests/specs/check/npm_pkg_empty_main_entry/run.out
Normal file
|
@ -0,0 +1 @@
|
|||
3
|
Loading…
Reference in a new issue