mirror of
https://github.com/denoland/deno.git
synced 2024-12-21 23:04:45 -05:00
fix(npm): better error message when attempting to use typescript in npm packages (#16813)
This commit is contained in:
parent
8fc62f93bf
commit
d0a4e23ae8
6 changed files with 40 additions and 7 deletions
|
@ -703,20 +703,25 @@ pub fn url_to_node_resolution(
|
|||
npm_resolver: &dyn RequireNpmResolver,
|
||||
) -> Result<NodeResolution, AnyError> {
|
||||
let url_str = url.as_str().to_lowercase();
|
||||
Ok(if url_str.starts_with("http") {
|
||||
NodeResolution::Esm(url)
|
||||
if url_str.starts_with("http") {
|
||||
Ok(NodeResolution::Esm(url))
|
||||
} else if url_str.ends_with(".js") || url_str.ends_with(".d.ts") {
|
||||
let package_config = get_closest_package_json(&url, npm_resolver)?;
|
||||
if package_config.typ == "module" {
|
||||
NodeResolution::Esm(url)
|
||||
Ok(NodeResolution::Esm(url))
|
||||
} else {
|
||||
NodeResolution::CommonJs(url)
|
||||
Ok(NodeResolution::CommonJs(url))
|
||||
}
|
||||
} else if url_str.ends_with(".mjs") || url_str.ends_with(".d.mts") {
|
||||
NodeResolution::Esm(url)
|
||||
Ok(NodeResolution::Esm(url))
|
||||
} else if url_str.ends_with(".ts") {
|
||||
Err(generic_error(format!(
|
||||
"TypeScript files are not supported in npm packages: {}",
|
||||
url
|
||||
)))
|
||||
} else {
|
||||
NodeResolution::CommonJs(url)
|
||||
})
|
||||
Ok(NodeResolution::CommonJs(url))
|
||||
}
|
||||
}
|
||||
|
||||
fn finalize_resolution(
|
||||
|
|
|
@ -314,6 +314,14 @@ itest!(types_no_types_entry {
|
|||
exit_code: 0,
|
||||
});
|
||||
|
||||
itest!(typescript_file_in_package {
|
||||
args: "run npm/typescript_file_in_package/main.ts",
|
||||
output: "npm/typescript_file_in_package/main.out",
|
||||
envs: env_vars(),
|
||||
http_server: true,
|
||||
exit_code: 1,
|
||||
});
|
||||
|
||||
#[test]
|
||||
fn parallel_downloading() {
|
||||
let (out, _err) = util::run_and_collect_output_with_args(
|
||||
|
|
4
cli/tests/testdata/npm/registry/@denotest/typescript-file/1.0.0/index.ts
vendored
Normal file
4
cli/tests/testdata/npm/registry/@denotest/typescript-file/1.0.0/index.ts
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
// this should not work because we don't support typescript files in npm packages
|
||||
export function getValue(): 5 {
|
||||
return 5;
|
||||
}
|
5
cli/tests/testdata/npm/registry/@denotest/typescript-file/1.0.0/package.json
vendored
Normal file
5
cli/tests/testdata/npm/registry/@denotest/typescript-file/1.0.0/package.json
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"name": "@denotest/typescript-file",
|
||||
"version": "1.0.0",
|
||||
"main": "./index.ts"
|
||||
}
|
6
cli/tests/testdata/npm/typescript_file_in_package/main.out
vendored
Normal file
6
cli/tests/testdata/npm/typescript_file_in_package/main.out
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
Download http://localhost:4545/npm/registry/@denotest/typescript-file
|
||||
Download http://localhost:4545/npm/registry/@denotest/typescript-file/1.0.0.tgz
|
||||
error: Could not resolve 'npm:@denotest/typescript-file'.
|
||||
|
||||
Caused by:
|
||||
TypeScript files are not supported in npm packages: file:///[WILDCARD]/@denotest/typescript-file/1.0.0/index.ts
|
5
cli/tests/testdata/npm/typescript_file_in_package/main.ts
vendored
Normal file
5
cli/tests/testdata/npm/typescript_file_in_package/main.ts
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
// We don't support typescript files in npm packages because we don't
|
||||
// want to encourage people distributing npm packages that aren't JavaScript.
|
||||
import { getValue } from "npm:@denotest/typescript-file";
|
||||
|
||||
console.log(getValue());
|
Loading…
Reference in a new issue