import_map 0.20.0

An implementation of WICG Import Maps specification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use serde_json::json;
use serde_json::Value;
use url::Url;

/// This function can be used to modify the `imports` and "scopes" mappings
/// to expand bare specifier imports to provide "directory" imports, eg.:
/// - `"express": "npm:express@4` -> `"express/": "npm:/express@4/`
/// - `"@std": "jsr:@std` -> `"std@/": "jsr:/@std/`
///
/// Only `npm:` and `jsr:` scheme are expanded and if there's already a
/// "directory" import, it is not overwritten.
pub fn expand_import_map_value(import_map: Value) -> Value {
  let Value::Object(mut import_map) = import_map else {
    return import_map;
  };

  if let Some(imports) = import_map.get("imports").and_then(|i| i.as_object()) {
    import_map.insert(
      "imports".to_string(),
      Value::Object(expand_imports(imports)),
    );
  }
  if let Some(scopes) = import_map.remove("scopes") {
    match scopes {
      Value::Object(scopes) => {
        let mut expanded_scopes = serde_json::Map::with_capacity(scopes.len());
        for (key, imports) in scopes {
          let imports = match imports {
            Value::Object(imports) => Value::Object(expand_imports(&imports)),
            _ => imports,
          };
          expanded_scopes.insert(key, imports);
        }
        import_map.insert("scopes".to_string(), Value::Object(expanded_scopes));
      }
      _ => {
        import_map.insert("scopes".to_string(), scopes);
      }
    }
  }

  Value::Object(import_map)
}

fn expand_imports(
  imports_map: &serde_json::Map<String, Value>,
) -> serde_json::Map<String, Value> {
  let mut expanded_imports = serde_json::Map::new();
  for (key, value) in imports_map {
    if !key.ends_with('/') {
      expanded_imports.insert(key.to_string(), value.clone());
      let key_with_trailing_slash = format!("{}/", key);

      // Don't overwrite existing keys
      if imports_map.contains_key(&key_with_trailing_slash) {
        continue;
      }

      let Some(value_str) = value.as_str() else {
        continue;
      };

      if !value_str.ends_with('/') {
        let value_with_trailing_slash =
          if let Some(value_str) = value_str.strip_prefix("jsr:") {
            let value_str = value_str.strip_prefix('/').unwrap_or(value_str);
            Some(format!("jsr:/{}/", value_str))
          } else if let Some(value_str) = value_str.strip_prefix("npm:") {
            let value_str = value_str.strip_prefix('/').unwrap_or(value_str);
            Some(format!("npm:/{}/", value_str))
          } else {
            None
          };

        if let Some(value_with_trailing_slash) = value_with_trailing_slash {
          expanded_imports.insert(
            key_with_trailing_slash,
            Value::String(value_with_trailing_slash),
          );
          continue;
        }
      }
    }

    expanded_imports.insert(key.to_string(), value.clone());
  }
  expanded_imports
}

pub struct ImportMapConfig {
  pub base_url: Url,
  pub import_map_value: Value,
}

fn pop_last_segment(url: &Url) -> Url {
  let mut url = url.clone();
  if let Ok(mut segments) = url.path_segments_mut() {
    segments.pop();
  }
  // seems like a bug in the url crate that poping a path segment
  // does not maintain the slash separator
  // https://github.com/servo/rust-url/issues/912
  if !url.path().ends_with('/') {
    url.set_path(&format!("{}/", url.path()));
  }
  url
}

pub fn create_synthetic_import_map(
  mut base_import_map: ImportMapConfig,
  children_import_maps: Vec<ImportMapConfig>,
) -> (Url, Value) {
  let mut synth_import_map_imports = serde_json::Map::new();
  let mut synth_import_map_scopes = serde_json::Map::new();

  let base_import_map_dir = pop_last_segment(&base_import_map.base_url);

  if let Value::Object(base_value) = &mut base_import_map.import_map_value {
    if let Some(Value::Object(base_imports)) = base_value.remove("imports") {
      synth_import_map_imports = base_imports;
    }
    if let Some(Value::Object(base_scopes)) = base_value.remove("scopes") {
      synth_import_map_scopes = base_scopes;
    }
  }

  for child_config in children_import_maps.into_iter() {
    let mut member_scope = serde_json::Map::new();

    let member_dir = pop_last_segment(&child_config.base_url);
    let relative_to_base_dir =
      base_import_map_dir.make_relative(&member_dir).unwrap();
    let member_prefix = format!(
      "./{}/",
      relative_to_base_dir
        .strip_suffix('/')
        .unwrap_or(&relative_to_base_dir)
    );
    let Value::Object(mut import_map_obj) = child_config.import_map_value
    else {
      continue;
    };

    if let Some(Value::Object(imports)) = import_map_obj.remove("imports") {
      for (key, value) in imports {
        let Some(value_str) = value.as_str() else {
          continue;
        };
        let value_url = member_dir.join(value_str).unwrap();
        // `make_relative` can fail here if the provided value is already
        // a fully resolved URL.
        let value_relative_to_base_dir =
          match base_import_map_dir.make_relative(&value_url) {
            Some(v) => format!("./{}", v),
            None => value_str.to_string(),
          };
        member_scope.insert(key, Value::String(value_relative_to_base_dir));
      }
    }
    combine_object(
      &mut synth_import_map_scopes,
      member_prefix,
      Value::Object(member_scope.clone()),
    );

    if let Some(Value::Object(scopes)) = import_map_obj.remove("scopes") {
      for (scope_name, scope_obj) in scopes {
        // Keys for scopes need to be processed - they might look like
        // "/foo/" and coming from "bar" workspace member. So we need to
        // prepend the member name to the scope.
        let Ok(scope_name_dir) = member_dir.join(&scope_name) else {
          combine_object(&mut synth_import_map_scopes, scope_name, scope_obj);
          continue; // not a file specifier
        };
        let Some(relative_to_base_dir) =
          base_import_map_dir.make_relative(&scope_name_dir)
        else {
          combine_object(&mut synth_import_map_scopes, scope_name, scope_obj);
          continue; // not a file specifier
        };
        let new_key = format!(
          "./{}/",
          relative_to_base_dir
            .strip_suffix('/')
            .unwrap_or(&relative_to_base_dir)
        );

        let mut new_scope = serde_json::Map::new();
        if let Value::Object(scope_obj) = scope_obj {
          for (key, value) in scope_obj {
            let Some(value_str) = value.as_str() else {
              continue;
            };
            let value_url = member_dir.join(value_str).unwrap();
            // `make_relative` can fail here if the provided value is already
            // a fully resolved URL.
            let value_relative_to_base_dir =
              match base_import_map_dir.make_relative(&value_url) {
                Some(v) => format!("./{}", v),
                None => value_str.to_string(),
              };
            new_scope.insert(key, Value::String(value_relative_to_base_dir));
          }
        }
        combine_object(
          &mut synth_import_map_scopes,
          new_key,
          Value::Object(new_scope),
        );
      }
    }
  }

  let mut import_map = json!({});

  if !synth_import_map_imports.is_empty() {
    import_map["imports"] = Value::Object(synth_import_map_imports);
  }
  if !synth_import_map_scopes.is_empty() {
    import_map["scopes"] = Value::Object(synth_import_map_scopes);
  }

  (base_import_map.base_url, import_map)
}

fn combine_object(
  base: &mut serde_json::Map<String, Value>,
  property_name: String,
  addition: serde_json::Value,
) {
  if let Some(base_property) = base.get_mut(&property_name) {
    if let Some(base_property_obj) = base_property.as_object_mut() {
      if let Value::Object(addition) = addition {
        for (key, value) in addition.into_iter() {
          combine_object(base_property_obj, key, value);
        }
      }
    } else {
      base.insert(property_name, addition);
    }
  } else {
    base.insert(property_name, addition);
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use pretty_assertions::assert_eq;

  #[test]
  fn test_synthetic_import_map() {
    let base_import_map = ImportMapConfig {
      base_url: Url::parse("file:///import_map.json").unwrap(),
      import_map_value: json!({
        "imports": {
          "@std/assert/": "deno:/@std/assert/",
          "express": "npm:express@4"
        },
        "scopes": {}
      }),
    };
    let children = vec![
      ImportMapConfig {
        base_url: Url::parse("file:///foo/deno.json").unwrap(),
        import_map_value: json!({
          "imports": {
            "moment": "npm:moment@5",
          },
          "scopes": {},
        }),
      },
      ImportMapConfig {
        base_url: Url::parse("file:///bar/deno.json").unwrap(),
        import_map_value: json!({
          "imports": {
            "express": "npm:express@4",
          },
          "scopes": {},
        }),
      },
    ];
    let (url, value) = create_synthetic_import_map(base_import_map, children);
    assert_eq!(url.as_str(), "file:///import_map.json");
    assert_eq!(
      value,
      json!({
        "imports": {
          "@std/assert/": "deno:/@std/assert/",
          "express": "npm:express@4"
        },
        "scopes": {
          "./foo/": {
            "moment": "npm:moment@5"
          },
          "./bar/": {
            "express": "npm:express@4"
          },
        },
      })
    );
  }

  #[test]
  fn test_synthetic_import_map2() {
    let base_import_map = ImportMapConfig {
      base_url: Url::parse("file:///import_map.json").unwrap(),
      import_map_value: json!({
        "imports": {
          "@std/assert/": "deno:/@std/assert/",
          "express": "npm:express@4"
        },
        "scopes": {
          "https://raw.githubusercontent.com/example/": {
            "https://deno.land/std/": "https://deno.land/std@0.177.0/"
          },
          "./foo/": {
            "root": "./other.js",
            // this will be overwritten by the child
            "override": "./overwritten.js"
          }
        }
      }),
    };
    let children = vec![
      ImportMapConfig {
        base_url: Url::parse("file:///foo/deno.json").unwrap(),
        import_map_value: json!({
          "imports": {
            "~/": "./",
            "foo/": "./bar/",
            "override": "./overwritten.js"
          },
          "scopes": {
            "./fizz/": {
              "express": "npm:express@5",
            },
            "https://raw.githubusercontent.com/example/": {
              "https://example.com/": "https://deno.land/std@0.177.0/"
            },
            "https://raw.githubusercontent.com/other/": {
              "https://deno.land/std/": "https://deno.land/std@0.177.0/"
            }
          },
        }),
      },
      ImportMapConfig {
        base_url: Url::parse("file:///bar/deno.json").unwrap(),
        import_map_value: json!({
          "imports": {
            "@/": "./",
            "secret_mod/": "./some_mod/"
          },
          "scopes": {
            "./fizz/": {
              "foo/": "./buzz/",
            }
          },
        }),
      },
    ];
    let (url, value) = create_synthetic_import_map(base_import_map, children);
    assert_eq!(url.as_str(), "file:///import_map.json");
    assert_eq!(
      value,
      json!({
        "imports": {
          "@std/assert/": "deno:/@std/assert/",
          "express": "npm:express@4"
        },
        "scopes": {
          "./foo/": {
            "~/": "./foo/",
            "foo/": "./foo/bar/",
            "override": "./foo/overwritten.js",
            "root": "./other.js",
          },
          "./foo/fizz/": {
            "express": "npm:express@5",
          },
          "./bar/": {
            "@/": "./bar/",
            "secret_mod/": "./bar/some_mod/"
          },
          "./bar/fizz/": {
            "foo/": "./bar/buzz/"
          },
          "https://raw.githubusercontent.com/other/": {
            "https://deno.land/std/": "https://deno.land/std@0.177.0/"
          },
          "https://raw.githubusercontent.com/example/": {
            "https://deno.land/std/": "https://deno.land/std@0.177.0/",
            "https://example.com/": "https://deno.land/std@0.177.0/",
          },
        },
      })
    );
  }

  #[test]
  fn test_expand_imports() {
    let import_map = json!({
      "imports": {
        "@std": "jsr:/@std",
        "@foo": "jsr:@foo",
        "express": "npm:express@4",
        "foo": "https://example.com/foo/bar"
      },
      "scopes": {}
    });
    let value = expand_import_map_value(import_map);
    assert_eq!(
      value,
      json!({
        "imports": {
          "@std": "jsr:/@std",
          "@std/": "jsr:/@std/",
          "@foo": "jsr:@foo",
          "@foo/": "jsr:/@foo/",
          "express": "npm:express@4",
          "express/": "npm:/express@4/",
          "foo": "https://example.com/foo/bar"
        },
        "scopes": {}
      })
    );
  }

  #[test]
  fn test_expand_imports_with_trailing_slash() {
    let import_map = json!({
      "imports": {
        "express": "npm:express@4",
        "express/": "npm:/express@4/foo/bar/",
      },
      "scopes": {}
    });
    let value = expand_import_map_value(import_map);
    assert_eq!(
      value,
      json!({
        "imports": {
          "express": "npm:express@4",
          "express/": "npm:/express@4/foo/bar/",
        },
        "scopes": {}
      })
    );
  }
}