diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2024-01-24 23:44:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-24 23:44:06 +0100 |
commit | 44f8b05f5bf45453496e80c69dc7850aa98c9af8 (patch) | |
tree | ba31d15374e1b658a444e5727abc7b157a2efc73 /cli/tests/integration/run_tests.rs | |
parent | 801ed74118baabef49842bbfb2164f971d2cdb03 (diff) |
feat: Expand 'imports' section of deno.json (#22087)
This commit adds automatic expansion of "imports" field in "deno.json"
file.
If "npm:" or "jsr:" imports are encountered we automatically try to add
a "directory" remapping.
Previously users had to specify entries for both `foo` and `foo/` to be
able to import like
`import { symbol1 } from "foo";` and `import { symbol2 } from
"foo/some_file.js"`:
```
{
"imports": {
"foo": "npm:@foo/bar",
"foo/": "npm:/@foo/bar/",
}
```
With this change users can only add entry for `foo`:
```
{
"imports": {
"foo": "npm:@foo/bar",
}
```
The entry for `foo/` will be provided automatically.
Similarly if user provides "directory" remapping explicitly, we will not
overwrite it.
Diffstat (limited to 'cli/tests/integration/run_tests.rs')
-rw-r--r-- | cli/tests/integration/run_tests.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index 6c82cfeec..b8acce94d 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -5113,3 +5113,64 @@ itest!(warn_on_deprecated_api_with_env_var { http_server: true, exit_code: 0, }); + +#[test] +fn deno_json_imports_expand() { + let test_context = TestContextBuilder::for_npm().use_temp_cwd().build(); + let dir = test_context.temp_dir(); + dir.write( + "deno.json", + r#"{ + "imports": { + "basic": "npm:@denotest/esm-basic" + } +}"#, + ); + + dir.write( + "main.ts", + r#" +// import map should resolve +import { setValue, getValue } from "basic"; +// this entry should have been added automatically +import { hello } from "basic/other.mjs"; + +setValue(5); +console.log(getValue()); +console.log(hello()); +"#, + ); + let output = test_context.new_command().args("run main.ts").run(); + output.assert_matches_text("[WILDCARD]5\nhello, world!\n"); +} + +#[test] +fn deno_json_imports_expand_doesnt_overwrite_existing_entries() { + let test_context = TestContextBuilder::for_npm().use_temp_cwd().build(); + let dir = test_context.temp_dir(); + dir.write( + "deno.json", + r#"{ + "imports": { + "basic": "npm:@denotest/esm-basic", + "basic/": "npm:/@denotest/sub-folders/folder_index_js/" + } +}"#, + ); + + dir.write( + "main.ts", + r#" +// import map should resolve +import { setValue, getValue } from "basic"; +// this entry should map to explicitly specified "basic/" mapping +import { add } from "basic/index.js"; + +setValue(5); +console.log(getValue()); +console.log(add(3, 4)); +"#, + ); + let output = test_context.new_command().args("run main.ts").run(); + output.assert_matches_text("[WILDCARD]5\n7\n"); +} |