Skip to content

Commit a115340

Browse files
bartlomiejury
authored andcommitted
feat: Import maps (#2360)
1 parent 8ec5276 commit a115340

24 files changed

+2406
-36
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build_extra/rust/BUILD.gn

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1301,11 +1301,15 @@ rust_proc_macro("serde_derive") {
13011301
rust_rlib("serde_json") {
13021302
edition = "2015"
13031303
source_root = "$cargo_home/registry/src/g.yxqyang.asia-1ecc6299db9ec823/serde_json-1.0.39/src/lib.rs"
1304-
features = [ "default" ]
1304+
features = [
1305+
"default",
1306+
"preserve_order",
1307+
]
13051308
extern_rlib = [
13061309
"itoa",
13071310
"ryu",
13081311
"serde",
1312+
"indexmap",
13091313
]
13101314
args = [
13111315
"--cap-lints",

cli/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ main_extern_rlib = [
2929
"http",
3030
"hyper",
3131
"hyper_rustls",
32+
"indexmap",
3233
"lazy_static",
3334
"libc",
3435
"log",

cli/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ futures = "0.1.27"
2727
http = "0.1.17"
2828
hyper = "0.12.29"
2929
hyper-rustls = "0.16.1"
30+
indexmap = "1.0.2"
3031
integer-atomics = "1.0.2"
3132
lazy_static = "1.3.0"
3233
libc = "0.2.55"
@@ -38,7 +39,7 @@ ring = "0.14.6"
3839
rustyline = "4.1.0"
3940
serde = "1.0.91"
4041
serde_derive = "1.0.91"
41-
serde_json = "1.0.39"
42+
serde_json = { version = "1.0.39", features = [ "preserve_order" ] }
4243
source-map-mappings = "0.5.0"
4344
tempfile = "3.0.8"
4445
tokio = "0.1.20"

cli/errors.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
2+
use crate::import_map::ImportMapError;
23
use crate::js_errors::JSErrorColor;
34
pub use crate::msg::ErrorKind;
45
use crate::resolve_addr::ResolveAddrError;
@@ -24,6 +25,7 @@ enum Repr {
2425
IoErr(io::Error),
2526
UrlErr(url::ParseError),
2627
HyperErr(hyper::Error),
28+
ImportMapErr(ImportMapError),
2729
}
2830

2931
pub fn new(kind: ErrorKind, msg: String) -> DenoError {
@@ -92,6 +94,7 @@ impl DenoError {
9294
ErrorKind::HttpOther
9395
}
9496
}
97+
Repr::ImportMapErr(ref _err) => ErrorKind::ImportMapError,
9598
}
9699
}
97100
}
@@ -103,6 +106,7 @@ impl fmt::Display for DenoError {
103106
Repr::IoErr(ref err) => err.fmt(f),
104107
Repr::UrlErr(ref err) => err.fmt(f),
105108
Repr::HyperErr(ref err) => err.fmt(f),
109+
Repr::ImportMapErr(ref err) => f.pad(&err.msg),
106110
}
107111
}
108112
}
@@ -114,6 +118,7 @@ impl std::error::Error for DenoError {
114118
Repr::IoErr(ref err) => err.description(),
115119
Repr::UrlErr(ref err) => err.description(),
116120
Repr::HyperErr(ref err) => err.description(),
121+
Repr::ImportMapErr(ref err) => &err.msg,
117122
}
118123
}
119124

@@ -123,6 +128,7 @@ impl std::error::Error for DenoError {
123128
Repr::IoErr(ref err) => Some(err),
124129
Repr::UrlErr(ref err) => Some(err),
125130
Repr::HyperErr(ref err) => Some(err),
131+
Repr::ImportMapErr(ref _err) => None,
126132
}
127133
}
128134
}
@@ -202,6 +208,14 @@ impl From<UnixError> for DenoError {
202208
}
203209
}
204210

211+
impl From<ImportMapError> for DenoError {
212+
fn from(err: ImportMapError) -> Self {
213+
Self {
214+
repr: Repr::ImportMapErr(err),
215+
}
216+
}
217+
}
218+
205219
pub fn bad_resource() -> DenoError {
206220
new(ErrorKind::BadResource, String::from("bad resource id"))
207221
}

cli/flags.rs

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub struct DenoFlags {
1515
/// When the `--config`/`-c` flag is used to pass the name, this will be set
1616
/// the path passed on the command line, otherwise `None`.
1717
pub config_path: Option<String>,
18+
/// When the `--importmap` flag is used to pass the name, this will be set
19+
/// the path passed on the command line, otherwise `None`.
20+
pub import_map_path: Option<String>,
1821
pub allow_read: bool,
1922
pub read_whitelist: Vec<String>,
2023
pub allow_write: bool,
@@ -82,6 +85,16 @@ fn add_run_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
8285
Arg::with_name("no-prompt")
8386
.long("no-prompt")
8487
.help("Do not use prompts"),
88+
).arg(
89+
Arg::with_name("importmap")
90+
.long("importmap")
91+
.value_name("FILE")
92+
.help("Load import map file")
93+
.long_help(
94+
"Load import map file
95+
Specification: https://wicg.github.io/import-maps/
96+
Examples: https://github.com/WICG/import-maps#the-import-map",
97+
).takes_value(true),
8598
)
8699
}
87100

@@ -367,21 +380,18 @@ pub fn parse_flags(matches: &ArgMatches) -> DenoFlags {
367380
flags.v8_flags = Some(v8_flags);
368381
}
369382

370-
flags = parse_permission_args(flags, matches);
383+
flags = parse_run_args(flags, matches);
371384
// flags specific to "run" subcommand
372385
if let Some(run_matches) = matches.subcommand_matches("run") {
373-
flags = parse_permission_args(flags.clone(), run_matches);
386+
flags = parse_run_args(flags.clone(), run_matches);
374387
}
375388

376389
flags
377390
}
378391

379392
/// Parse permission specific matches Args and assign to DenoFlags.
380393
/// This method is required because multiple subcommands use permission args.
381-
fn parse_permission_args(
382-
mut flags: DenoFlags,
383-
matches: &ArgMatches,
384-
) -> DenoFlags {
394+
fn parse_run_args(mut flags: DenoFlags, matches: &ArgMatches) -> DenoFlags {
385395
if matches.is_present("allow-read") {
386396
if matches.value_of("allow-read").is_some() {
387397
let read_wl = matches.values_of("allow-read").unwrap();
@@ -435,6 +445,7 @@ fn parse_permission_args(
435445
if matches.is_present("no-prompt") {
436446
flags.no_prompts = true;
437447
}
448+
flags.import_map_path = matches.value_of("importmap").map(ToOwned::to_owned);
438449

439450
flags
440451
}
@@ -912,6 +923,7 @@ mod tests {
912923
assert_eq!(subcommand, DenoSubcommand::Xeval);
913924
assert_eq!(argv, svec!["deno", "console.log(val)"]);
914925
}
926+
915927
#[test]
916928
fn test_flags_from_vec_19() {
917929
use tempfile::TempDir;
@@ -936,6 +948,7 @@ mod tests {
936948
assert_eq!(subcommand, DenoSubcommand::Run);
937949
assert_eq!(argv, svec!["deno", "script.ts"]);
938950
}
951+
939952
#[test]
940953
fn test_flags_from_vec_20() {
941954
use tempfile::TempDir;
@@ -960,6 +973,7 @@ mod tests {
960973
assert_eq!(subcommand, DenoSubcommand::Run);
961974
assert_eq!(argv, svec!["deno", "script.ts"]);
962975
}
976+
963977
#[test]
964978
fn test_flags_from_vec_21() {
965979
let (flags, subcommand, argv) = flags_from_vec(svec![
@@ -1067,4 +1081,35 @@ mod tests {
10671081
assert_eq!(subcommand, DenoSubcommand::Bundle);
10681082
assert_eq!(argv, svec!["deno", "source.ts", "bundle.js"])
10691083
}
1084+
1085+
#[test]
1086+
fn test_flags_from_vec_27() {
1087+
let (flags, subcommand, argv) = flags_from_vec(svec![
1088+
"deno",
1089+
"run",
1090+
"--importmap=importmap.json",
1091+
"script.ts"
1092+
]);
1093+
assert_eq!(
1094+
flags,
1095+
DenoFlags {
1096+
import_map_path: Some("importmap.json".to_owned()),
1097+
..DenoFlags::default()
1098+
}
1099+
);
1100+
assert_eq!(subcommand, DenoSubcommand::Run);
1101+
assert_eq!(argv, svec!["deno", "script.ts"]);
1102+
1103+
let (flags, subcommand, argv) =
1104+
flags_from_vec(svec!["deno", "--importmap=importmap.json", "script.ts"]);
1105+
assert_eq!(
1106+
flags,
1107+
DenoFlags {
1108+
import_map_path: Some("importmap.json".to_owned()),
1109+
..DenoFlags::default()
1110+
}
1111+
);
1112+
assert_eq!(subcommand, DenoSubcommand::Run);
1113+
assert_eq!(argv, svec!["deno", "script.ts"]);
1114+
}
10701115
}

0 commit comments

Comments
 (0)