summaryrefslogtreecommitdiff
path: root/cli/tools
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tools')
-rw-r--r--cli/tools/vendor/build.rs349
-rw-r--r--cli/tools/vendor/import_map.rs265
-rw-r--r--cli/tools/vendor/mappings.rs42
-rw-r--r--cli/tools/vendor/mod.rs259
-rw-r--r--cli/tools/vendor/test.rs88
5 files changed, 838 insertions, 165 deletions
diff --git a/cli/tools/vendor/build.rs b/cli/tools/vendor/build.rs
index dd362ebfb..ecb7db717 100644
--- a/cli/tools/vendor/build.rs
+++ b/cli/tools/vendor/build.rs
@@ -1,11 +1,17 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use std::path::Path;
+use std::path::PathBuf;
+use deno_ast::ModuleSpecifier;
+use deno_core::anyhow::bail;
+use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_graph::Module;
use deno_graph::ModuleGraph;
use deno_graph::ModuleKind;
+use import_map::ImportMap;
+use import_map::SpecifierMap;
use super::analyze::has_default_export;
use super::import_map::build_import_map;
@@ -15,29 +21,53 @@ use super::specifiers::is_remote_specifier;
/// Allows substituting the environment for testing purposes.
pub trait VendorEnvironment {
+ fn cwd(&self) -> Result<PathBuf, AnyError>;
fn create_dir_all(&self, dir_path: &Path) -> Result<(), AnyError>;
fn write_file(&self, file_path: &Path, text: &str) -> Result<(), AnyError>;
+ fn path_exists(&self, path: &Path) -> bool;
}
pub struct RealVendorEnvironment;
impl VendorEnvironment for RealVendorEnvironment {
+ fn cwd(&self) -> Result<PathBuf, AnyError> {
+ Ok(std::env::current_dir()?)
+ }
+
fn create_dir_all(&self, dir_path: &Path) -> Result<(), AnyError> {
Ok(std::fs::create_dir_all(dir_path)?)
}
fn write_file(&self, file_path: &Path, text: &str) -> Result<(), AnyError> {
- Ok(std::fs::write(file_path, text)?)
+ std::fs::write(file_path, text)
+ .with_context(|| format!("Failed writing {}", file_path.display()))
+ }
+
+ fn path_exists(&self, path: &Path) -> bool {
+ path.exists()
}
}
/// Vendors remote modules and returns how many were vendored.
pub fn build(
- graph: &ModuleGraph,
+ graph: ModuleGraph,
output_dir: &Path,
+ original_import_map: Option<&ImportMap>,
environment: &impl VendorEnvironment,
) -> Result<usize, AnyError> {
assert!(output_dir.is_absolute());
+ let output_dir_specifier =
+ ModuleSpecifier::from_directory_path(output_dir).unwrap();
+
+ if let Some(original_im) = &original_import_map {
+ validate_original_import_map(original_im, &output_dir_specifier)?;
+ }
+
+ // build the graph
+ graph.lock()?;
+ graph.valid()?;
+
+ // figure out how to map remote modules to local
let all_modules = graph.modules();
let remote_modules = all_modules
.iter()
@@ -45,7 +75,7 @@ pub fn build(
.copied()
.collect::<Vec<_>>();
let mappings =
- Mappings::from_remote_modules(graph, &remote_modules, output_dir)?;
+ Mappings::from_remote_modules(&graph, &remote_modules, output_dir)?;
// write out all the files
for module in &remote_modules {
@@ -77,16 +107,59 @@ pub fn build(
environment.write_file(&proxy_path, &text)?;
}
- // create the import map
- if !mappings.base_specifiers().is_empty() {
- let import_map_text = build_import_map(graph, &all_modules, &mappings);
- environment
- .write_file(&output_dir.join("import_map.json"), &import_map_text)?;
+ // create the import map if necessary
+ if !remote_modules.is_empty() {
+ let import_map_path = output_dir.join("import_map.json");
+ let import_map_text = build_import_map(
+ &output_dir_specifier,
+ &graph,
+ &all_modules,
+ &mappings,
+ original_import_map,
+ );
+ environment.write_file(&import_map_path, &import_map_text)?;
}
Ok(remote_modules.len())
}
+fn validate_original_import_map(
+ import_map: &ImportMap,
+ output_dir: &ModuleSpecifier,
+) -> Result<(), AnyError> {
+ fn validate_imports(
+ imports: &SpecifierMap,
+ output_dir: &ModuleSpecifier,
+ ) -> Result<(), AnyError> {
+ for entry in imports.entries() {
+ if let Some(value) = entry.value {
+ if value.as_str().starts_with(output_dir.as_str()) {
+ bail!(
+ "Providing an existing import map with entries for the output directory is not supported (\"{}\": \"{}\").",
+ entry.raw_key,
+ entry.raw_value.unwrap_or("<INVALID>"),
+ );
+ }
+ }
+ }
+ Ok(())
+ }
+
+ validate_imports(import_map.imports(), output_dir)?;
+
+ for scope in import_map.scopes() {
+ if scope.key.starts_with(output_dir.as_str()) {
+ bail!(
+ "Providing an existing import map with a scope for the output directory is not supported (\"{}\").",
+ scope.raw_key,
+ );
+ }
+ validate_imports(scope.imports, output_dir)?;
+ }
+
+ Ok(())
+}
+
fn build_proxy_module_source(
module: &Module,
proxied_module: &ProxiedModule,
@@ -171,9 +244,9 @@ mod test {
output.import_map,
Some(json!({
"imports": {
- "https://localhost/": "./localhost/",
"https://localhost/other.ts?test": "./localhost/other.ts",
"https://localhost/redirect.ts": "./localhost/mod.ts",
+ "https://localhost/": "./localhost/",
}
}))
);
@@ -241,7 +314,7 @@ mod test {
"imports": {
"https://localhost/": "./localhost/",
"https://localhost/redirect.ts": "./localhost/other.ts",
- "https://other/": "./other/"
+ "https://other/": "./other/",
},
"scopes": {
"./localhost/": {
@@ -313,10 +386,10 @@ mod test {
output.import_map,
Some(json!({
"imports": {
- "https://localhost/": "./localhost/",
"https://localhost/mod.TS": "./localhost/mod_2.TS",
"https://localhost/mod.ts": "./localhost/mod_3.ts",
"https://localhost/mod.ts?test": "./localhost/mod_4.ts",
+ "https://localhost/": "./localhost/",
}
}))
);
@@ -543,8 +616,8 @@ mod test {
output.import_map,
Some(json!({
"imports": {
- "http://localhost:4545/": "./localhost_4545/",
"http://localhost:4545/sub/logger/mod.ts?testing": "./localhost_4545/sub/logger/mod.ts",
+ "http://localhost:4545/": "./localhost_4545/",
},
"scopes": {
"./localhost_4545/": {
@@ -599,9 +672,9 @@ mod test {
output.import_map,
Some(json!({
"imports": {
+ "https://localhost/std/hash/mod.ts": "./localhost/std@0.1.0/hash/mod.ts",
"https://localhost/": "./localhost/",
- "https://localhost/std/hash/mod.ts": "./localhost/std@0.1.0/hash/mod.ts"
- }
+ },
}))
);
assert_eq!(
@@ -675,6 +748,254 @@ mod test {
);
}
+ #[tokio::test]
+ async fn existing_import_map_basic() {
+ let mut builder = VendorTestBuilder::with_default_setup();
+ let mut original_import_map = builder.new_import_map("/import_map2.json");
+ original_import_map
+ .imports_mut()
+ .append(
+ "https://localhost/mod.ts".to_string(),
+ "./local_vendor/mod.ts".to_string(),
+ )
+ .unwrap();
+ let local_vendor_scope = original_import_map
+ .get_or_append_scope_mut("./local_vendor/")
+ .unwrap();
+ local_vendor_scope
+ .append(
+ "https://localhost/logger.ts".to_string(),
+ "./local_vendor/logger.ts".to_string(),
+ )
+ .unwrap();
+ local_vendor_scope
+ .append(
+ "/console_logger.ts".to_string(),
+ "./local_vendor/console_logger.ts".to_string(),
+ )
+ .unwrap();
+
+ let output = builder
+ .with_loader(|loader| {
+ loader.add("/mod.ts", "import 'https://localhost/mod.ts'; import 'https://localhost/other.ts';");
+ loader.add("/local_vendor/mod.ts", "import 'https://localhost/logger.ts'; import '/console_logger.ts'; console.log(5);");
+ loader.add("/local_vendor/logger.ts", "export class Logger {}");
+ loader.add("/local_vendor/console_logger.ts", "export class ConsoleLogger {}");
+ loader.add("https://localhost/mod.ts", "console.log(6);");
+ loader.add("https://localhost/other.ts", "import './mod.ts';");
+ })
+ .set_original_import_map(original_import_map.clone())
+ .build()
+ .await
+ .unwrap();
+
+ assert_eq!(
+ output.import_map,
+ Some(json!({
+ "imports": {
+ "https://localhost/mod.ts": "../local_vendor/mod.ts",
+ "https://localhost/": "./localhost/"
+ },
+ "scopes": {
+ "../local_vendor/": {
+ "https://localhost/logger.ts": "../local_vendor/logger.ts",
+ "/console_logger.ts": "../local_vendor/console_logger.ts",
+ },
+ "./localhost/": {
+ "./localhost/mod.ts": "../local_vendor/mod.ts",
+ },
+ }
+ }))
+ );
+ assert_eq!(
+ output.files,
+ to_file_vec(&[("/vendor/localhost/other.ts", "import './mod.ts';")]),
+ );
+ }
+
+ #[tokio::test]
+ async fn existing_import_map_mapped_bare_specifier() {
+ let mut builder = VendorTestBuilder::with_default_setup();
+ let mut original_import_map = builder.new_import_map("/import_map.json");
+ let imports = original_import_map.imports_mut();
+ imports
+ .append("$fresh".to_string(), "https://localhost/fresh".to_string())
+ .unwrap();
+ imports
+ .append("std/".to_string(), "https://deno.land/std/".to_string())
+ .unwrap();
+ let output = builder
+ .with_loader(|loader| {
+ loader.add("/mod.ts", "import 'std/mod.ts'; import '$fresh';");
+ loader.add("https://deno.land/std/mod.ts", "export function test() {}");
+ loader.add_with_headers(
+ "https://localhost/fresh",
+ "export function fresh() {}",
+ &[("content-type", "application/typescript")],
+ );
+ })
+ .set_original_import_map(original_import_map.clone())
+ .build()
+ .await
+ .unwrap();
+
+ assert_eq!(
+ output.import_map,
+ Some(json!({
+ "imports": {
+ "https://deno.land/": "./deno.land/",
+ "https://localhost/": "./localhost/",
+ "$fresh": "./localhost/fresh.ts",
+ "std/mod.ts": "./deno.land/std/mod.ts",
+ },
+ }))
+ );
+ assert_eq!(
+ output.files,
+ to_file_vec(&[
+ ("/vendor/deno.land/std/mod.ts", "export function test() {}"),
+ ("/vendor/localhost/fresh.ts", "export function fresh() {}")
+ ]),
+ );
+ }
+
+ #[tokio::test]
+ async fn existing_import_map_remote_absolute_specifier_local() {
+ let mut builder = VendorTestBuilder::with_default_setup();
+ let mut original_import_map = builder.new_import_map("/import_map.json");
+ original_import_map
+ .imports_mut()
+ .append(
+ "https://localhost/logger.ts?test".to_string(),
+ "./local/logger.ts".to_string(),
+ )
+ .unwrap();
+
+ let output = builder
+ .with_loader(|loader| {
+ loader.add("/mod.ts", "import 'https://localhost/mod.ts'; import 'https://localhost/logger.ts?test';");
+ loader.add("/local/logger.ts", "export class Logger {}");
+ // absolute specifier in a remote module that will point at ./local/logger.ts
+ loader.add("https://localhost/mod.ts", "import '/logger.ts?test';");
+ loader.add("https://localhost/logger.ts?test", "export class Logger {}");
+ })
+ .set_original_import_map(original_import_map.clone())
+ .build()
+ .await
+ .unwrap();
+
+ assert_eq!(
+ output.import_map,
+ Some(json!({
+ "imports": {
+ "https://localhost/logger.ts?test": "../local/logger.ts",
+ "https://localhost/": "./localhost/",
+ },
+ "scopes": {
+ "./localhost/": {
+ "/logger.ts?test": "../local/logger.ts",
+ },
+ }
+ }))
+ );
+ assert_eq!(
+ output.files,
+ to_file_vec(&[("/vendor/localhost/mod.ts", "import '/logger.ts?test';")]),
+ );
+ }
+
+ #[tokio::test]
+ async fn existing_import_map_imports_output_dir() {
+ let mut builder = VendorTestBuilder::with_default_setup();
+ let mut original_import_map = builder.new_import_map("/import_map.json");
+ original_import_map
+ .imports_mut()
+ .append(
+ "std/mod.ts".to_string(),
+ "./vendor/deno.land/std/mod.ts".to_string(),
+ )
+ .unwrap();
+ let err = builder
+ .with_loader(|loader| {
+ loader.add("/mod.ts", "import 'std/mod.ts';");
+ loader.add("/vendor/deno.land/std/mod.ts", "export function f() {}");
+ loader.add("https://deno.land/std/mod.ts", "export function f() {}");
+ })
+ .set_original_import_map(original_import_map.clone())
+ .build()
+ .await
+ .err()
+ .unwrap();
+
+ assert_eq!(
+ err.to_string(),
+ concat!(
+ "Providing an existing import map with entries for the output ",
+ "directory is not supported ",
+ "(\"std/mod.ts\": \"./vendor/deno.land/std/mod.ts\").",
+ )
+ );
+ }
+
+ #[tokio::test]
+ async fn existing_import_map_scopes_entry_output_dir() {
+ let mut builder = VendorTestBuilder::with_default_setup();
+ let mut original_import_map = builder.new_import_map("/import_map.json");
+ let scopes = original_import_map
+ .get_or_append_scope_mut("./other/")
+ .unwrap();
+ scopes
+ .append("/mod.ts".to_string(), "./vendor/mod.ts".to_string())
+ .unwrap();
+ let err = builder
+ .with_loader(|loader| {
+ loader.add("/mod.ts", "console.log(5);");
+ })
+ .set_original_import_map(original_import_map.clone())
+ .build()
+ .await
+ .err()
+ .unwrap();
+
+ assert_eq!(
+ err.to_string(),
+ concat!(
+ "Providing an existing import map with entries for the output ",
+ "directory is not supported ",
+ "(\"/mod.ts\": \"./vendor/mod.ts\").",
+ )
+ );
+ }
+
+ #[tokio::test]
+ async fn existing_import_map_scopes_key_output_dir() {
+ let mut builder = VendorTestBuilder::with_default_setup();
+ let mut original_import_map = builder.new_import_map("/import_map.json");
+ let scopes = original_import_map
+ .get_or_append_scope_mut("./vendor/")
+ .unwrap();
+ scopes
+ .append("/mod.ts".to_string(), "./vendor/mod.ts".to_string())
+ .unwrap();
+ let err = builder
+ .with_loader(|loader| {
+ loader.add("/mod.ts", "console.log(5);");
+ })
+ .set_original_import_map(original_import_map.clone())
+ .build()
+ .await
+ .err()
+ .unwrap();
+
+ assert_eq!(
+ err.to_string(),
+ concat!(
+ "Providing an existing import map with a scope for the output ",
+ "directory is not supported (\"./vendor/\").",
+ )
+ );
+ }
+
fn to_file_vec(items: &[(&str, &str)]) -> Vec<(String, String)> {
items
.iter()
diff --git a/cli/tools/vendor/import_map.rs b/cli/tools/vendor/import_map.rs
index 1b2a2e263..e03260e3e 100644
--- a/cli/tools/vendor/import_map.rs
+++ b/cli/tools/vendor/import_map.rs
@@ -1,44 +1,43 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
-use std::collections::BTreeMap;
-
use deno_ast::LineAndColumnIndex;
use deno_ast::ModuleSpecifier;
use deno_ast::SourceTextInfo;
-use deno_core::serde_json;
use deno_graph::Module;
use deno_graph::ModuleGraph;
use deno_graph::Position;
use deno_graph::Range;
use deno_graph::Resolved;
-use serde::Serialize;
+use import_map::ImportMap;
+use import_map::SpecifierMap;
+use indexmap::IndexMap;
+use log::warn;
use super::mappings::Mappings;
use super::specifiers::is_remote_specifier;
use super::specifiers::is_remote_specifier_text;
-#[derive(Serialize)]
-struct SerializableImportMap {
- imports: BTreeMap<String, String>,
- #[serde(skip_serializing_if = "BTreeMap::is_empty")]
- scopes: BTreeMap<String, BTreeMap<String, String>>,
-}
-
struct ImportMapBuilder<'a> {
+ base_dir: &'a ModuleSpecifier,
mappings: &'a Mappings,
imports: ImportsBuilder<'a>,
- scopes: BTreeMap<String, ImportsBuilder<'a>>,
+ scopes: IndexMap<String, ImportsBuilder<'a>>,
}
impl<'a> ImportMapBuilder<'a> {
- pub fn new(mappings: &'a Mappings) -> Self {
+ pub fn new(base_dir: &'a ModuleSpecifier, mappings: &'a Mappings) -> Self {
ImportMapBuilder {
+ base_dir,
mappings,
- imports: ImportsBuilder::new(mappings),
+ imports: ImportsBuilder::new(base_dir, mappings),
scopes: Default::default(),
}
}
+ pub fn base_dir(&self) -> &ModuleSpecifier {
+ self.base_dir
+ }
+
pub fn scope(
&mut self,
base_specifier: &ModuleSpecifier,
@@ -48,38 +47,115 @@ impl<'a> ImportMapBuilder<'a> {
.entry(
self
.mappings
- .relative_specifier_text(self.mappings.output_dir(), base_specifier),
+ .relative_specifier_text(self.base_dir, base_specifier),
)
- .or_insert_with(|| ImportsBuilder::new(self.mappings))
+ .or_insert_with(|| ImportsBuilder::new(self.base_dir, self.mappings))
}
- pub fn into_serializable(self) -> SerializableImportMap {
- SerializableImportMap {
- imports: self.imports.imports,
- scopes: self
- .scopes
- .into_iter()
- .map(|(key, value)| (key, value.imports))
- .collect(),
+ pub fn into_import_map(
+ self,
+ original_import_map: Option<&ImportMap>,
+ ) -> ImportMap {
+ fn get_local_imports(
+ new_relative_path: &str,
+ original_imports: &SpecifierMap,
+ ) -> Vec<(String, String)> {
+ let mut result = Vec::new();
+ for entry in original_imports.entries() {
+ if let Some(raw_value) = entry.raw_value {
+ if raw_value.starts_with("./") || raw_value.starts_with("../") {
+ let sub_index = raw_value.find('/').unwrap() + 1;
+ result.push((
+ entry.raw_key.to_string(),
+ format!("{}{}", new_relative_path, &raw_value[sub_index..]),
+ ));
+ }
+ }
+ }
+ result
+ }
+
+ fn add_local_imports<'a>(
+ new_relative_path: &str,
+ original_imports: &SpecifierMap,
+ get_new_imports: impl FnOnce() -> &'a mut SpecifierMap,
+ ) {
+ let local_imports =
+ get_local_imports(new_relative_path, original_imports);
+ if !local_imports.is_empty() {
+ let new_imports = get_new_imports();
+ for (key, value) in local_imports {
+ if let Err(warning) = new_imports.append(key, value) {
+ warn!("{}", warning);
+ }
+ }
+ }
+ }
+
+ let mut import_map = ImportMap::new(self.base_dir.clone());
+
+ if let Some(original_im) = original_import_map {
+ let original_base_dir = ModuleSpecifier::from_directory_path(
+ original_im
+ .base_url()
+ .to_file_path()
+ .unwrap()
+ .parent()
+ .unwrap(),
+ )
+ .unwrap();
+ let new_relative_path = self
+ .mappings
+ .relative_specifier_text(self.base_dir, &original_base_dir);
+ // add the imports
+ add_local_imports(&new_relative_path, original_im.imports(), || {
+ import_map.imports_mut()
+ });
+
+ for scope in original_im.scopes() {
+ if scope.raw_key.starts_with("./") || scope.raw_key.starts_with("../") {
+ let sub_index = scope.raw_key.find('/').unwrap() + 1;
+ let new_key =
+ format!("{}{}", new_relative_path, &scope.raw_key[sub_index..]);
+ add_local_imports(&new_relative_path, scope.imports, || {
+ import_map.get_or_append_scope_mut(&new_key).unwrap()
+ });
+ }
+ }
}
- }
- pub fn into_file_text(self) -> String {
- let mut text =
- serde_json::to_string_pretty(&self.into_serializable()).unwrap();
- text.push('\n');
- text
+ let imports = import_map.imports_mut();
+ for (key, value) in self.imports.imports {
+ if !imports.contains(&key) {
+ imports.append(key, value).unwrap();
+ }
+ }
+
+ for (scope_key, scope_value) in self.scopes {
+ if !scope_value.imports.is_empty() {
+ let imports = import_map.get_or_append_scope_mut(&scope_key).unwrap();
+ for (key, value) in scope_value.imports {
+ if !imports.contains(&key) {
+ imports.append(key, value).unwrap();
+ }
+ }
+ }
+ }
+
+ import_map
}
}
struct ImportsBuilder<'a> {
+ base_dir: &'a ModuleSpecifier,
mappings: &'a Mappings,
- imports: BTreeMap<String, String>,
+ imports: IndexMap<String, String>,
}
impl<'a> ImportsBuilder<'a> {
- pub fn new(mappings: &'a Mappings) -> Self {
+ pub fn new(base_dir: &'a ModuleSpecifier, mappings: &'a Mappings) -> Self {
Self {
+ base_dir,
mappings,
imports: Default::default(),
}
@@ -88,7 +164,7 @@ impl<'a> ImportsBuilder<'a> {
pub fn add(&mut self, key: String, specifier: &ModuleSpecifier) {
let value = self
.mappings
- .relative_specifier_text(self.mappings.output_dir(), specifier);
+ .relative_specifier_text(self.base_dir, specifier);
// skip creating identity entries
if key != value {
@@ -98,20 +174,22 @@ impl<'a> ImportsBuilder<'a> {
}
pub fn build_import_map(
+ base_dir: &ModuleSpecifier,
graph: &ModuleGraph,
modules: &[&Module],
mappings: &Mappings,
+ original_import_map: Option<&ImportMap>,
) -> String {
- let mut import_map = ImportMapBuilder::new(mappings);
- visit_modules(graph, modules, mappings, &mut import_map);
+ let mut builder = ImportMapBuilder::new(base_dir, mappings);
+ visit_modules(graph, modules, mappings, &mut builder);
for base_specifier in mappings.base_specifiers() {
- import_map
+ builder
.imports
.add(base_specifier.to_string(), base_specifier);
}
- import_map.into_file_text()
+ builder.into_import_map(original_import_map).to_json()
}
fn visit_modules(
@@ -197,37 +275,70 @@ fn handle_dep_specifier(
mappings: &Mappings,
) {
let specifier = graph.resolve(unresolved_specifier);
- // do not handle specifiers pointing at local modules
- if !is_remote_specifier(&specifier) {
- return;
+ // check if it's referencing a remote module
+ if is_remote_specifier(&specifier) {
+ handle_remote_dep_specifier(
+ text,
+ unresolved_specifier,
+ &specifier,
+ import_map,
+ referrer,
+ mappings,
+ )
+ } else {
+ handle_local_dep_specifier(
+ text,
+ unresolved_specifier,
+ &specifier,
+ import_map,
+ referrer,
+ mappings,
+ );
}
+}
- let base_specifier = mappings.base_specifier(&specifier);
+fn handle_remote_dep_specifier(
+ text: &str,
+ unresolved_specifier: &ModuleSpecifier,
+ specifier: &ModuleSpecifier,
+ import_map: &mut ImportMapBuilder,
+ referrer: &ModuleSpecifier,
+ mappings: &Mappings,
+) {
if is_remote_specifier_text(text) {
+ let base_specifier = mappings.base_specifier(specifier);
if !text.starts_with(base_specifier.as_str()) {
panic!("Expected {} to start with {}", text, base_specifier);
}
let sub_path = &text[base_specifier.as_str().len()..];
- let expected_relative_specifier_text =
- mappings.relative_path(base_specifier, &specifier);
- if expected_relative_specifier_text == sub_path {
- return;
+ let relative_text =
+ mappings.relative_specifier_text(base_specifier, specifier);
+ let expected_sub_path = relative_text.trim_start_matches("./");
+ if expected_sub_path != sub_path {
+ import_map.imports.add(text.to_string(), specifier);
}
-
- import_map.imports.add(text.to_string(), &specifier);
} else {
let expected_relative_specifier_text =
- mappings.relative_specifier_text(referrer, &specifier);
+ mappings.relative_specifier_text(referrer, specifier);
if expected_relative_specifier_text == text {
return;
}
+ if !is_remote_specifier(referrer) {
+ // local module referencing a remote module using
+ // non-remote specifier text means it was something in
+ // the original import map, so add a mapping to it
+ import_map.imports.add(text.to_string(), specifier);
+ return;
+ }
+
+ let base_specifier = mappings.base_specifier(specifier);
+ let base_dir = import_map.base_dir().clone();
let imports = import_map.scope(base_specifier);
if text.starts_with("./") || text.starts_with("../") {
// resolve relative specifier key
let mut local_base_specifier = mappings.local_uri(base_specifier);
- local_base_specifier.set_query(unresolved_specifier.query());
local_base_specifier = local_base_specifier
// path includes "/" so make it relative
.join(&format!(".{}", unresolved_specifier.path()))
@@ -241,18 +352,15 @@ fn handle_dep_specifier(
local_base_specifier.set_query(unresolved_specifier.query());
imports.add(
- mappings.relative_specifier_text(
- mappings.output_dir(),
- &local_base_specifier,
- ),
- &specifier,
+ mappings.relative_specifier_text(&base_dir, &local_base_specifier),
+ specifier,
);
// add a mapping that uses the local directory name and the remote
// filename in order to support files importing this relatively
imports.add(
{
- let local_path = mappings.local_path(&specifier);
+ let local_path = mappings.local_path(specifier);
let mut value =
ModuleSpecifier::from_directory_path(local_path.parent().unwrap())
.unwrap();
@@ -262,17 +370,58 @@ fn handle_dep_specifier(
value.path(),
specifier.path_segments().unwrap().last().unwrap(),
));
- mappings.relative_specifier_text(mappings.output_dir(), &value)
+ mappings.relative_specifier_text(&base_dir, &value)
},
- &specifier,
+ specifier,
);
} else {
// absolute (`/`) or bare specifier should be left as-is
- imports.add(text.to_string(), &specifier);
+ imports.add(text.to_string(), specifier);
}
}
}
+fn handle_local_dep_specifier(
+ text: &str,
+ unresolved_specifier: &ModuleSpecifier,
+ specifier: &ModuleSpecifier,
+ import_map: &mut ImportMapBuilder,
+ referrer: &ModuleSpecifier,
+ mappings: &Mappings,
+) {
+ if !is_remote_specifier(referrer) {
+ // do not handle local modules referencing local modules
+ return;
+ }
+
+ // The remote module is referencing a local file. This could occur via an
+ // existing import map. In this case, we'll have to add an import map
+ // entry in order to map the path back to the local path once vendored.
+ let base_dir = import_map.base_dir().clone();
+ let base_specifier = mappings.base_specifier(referrer);
+ let imports = import_map.scope(base_specifier);
+
+ if text.starts_with("./") || text.starts_with("../") {
+ let referrer_local_uri = mappings.local_uri(referrer);
+ let mut specifier_local_uri =
+ referrer_local_uri.join(text).unwrap_or_else(|_| {
+ panic!(
+ "Error joining {} to {}",
+ unresolved_specifier.path(),
+ referrer_local_uri
+ )
+ });
+ specifier_local_uri.set_query(unresolved_specifier.query());
+
+ imports.add(
+ mappings.relative_specifier_text(&base_dir, &specifier_local_uri),
+ specifier,
+ );
+ } else {
+ imports.add(text.to_string(), specifier);
+ }
+}
+
fn text_from_range<'a>(
text_info: &SourceTextInfo,
text: &'a str,
diff --git a/cli/tools/vendor/mappings.rs b/cli/tools/vendor/mappings.rs
index 2e85445dc..543536128 100644
--- a/cli/tools/vendor/mappings.rs
+++ b/cli/tools/vendor/mappings.rs
@@ -14,6 +14,7 @@ use deno_graph::Position;
use deno_graph::Resolved;
use crate::fs_util::path_with_stem_suffix;
+use crate::fs_util::relative_specifier;
use super::specifiers::dir_name_for_root;
use super::specifiers::get_unique_path;
@@ -28,7 +29,6 @@ pub struct ProxiedModule {
/// Constructs and holds the remote specifier to local path mappings.
pub struct Mappings {
- output_dir: ModuleSpecifier,
mappings: HashMap<ModuleSpecifier, PathBuf>,
base_specifiers: Vec<ModuleSpecifier>,
proxies: HashMap<ModuleSpecifier, ProxiedModule>,
@@ -104,17 +104,12 @@ impl Mappings {
}
Ok(Self {
- output_dir: ModuleSpecifier::from_directory_path(output_dir).unwrap(),
mappings,
base_specifiers,
proxies,
})
}
- pub fn output_dir(&self) -> &ModuleSpecifier {
- &self.output_dir
- }
-
pub fn local_uri(&self, specifier: &ModuleSpecifier) -> ModuleSpecifier {
if specifier.scheme() == "file" {
specifier.clone()
@@ -146,43 +141,14 @@ impl Mappings {
}
}
- pub fn relative_path(
- &self,
- from: &ModuleSpecifier,
- to: &ModuleSpecifier,
- ) -> String {
- let mut from = self.local_uri(from);
- let to = self.local_uri(to);
-
- // workaround using parent directory until https://github.com/servo/rust-url/pull/754 is merged
- if !from.path().ends_with('/') {
- let local_path = self.local_path(&from);
- from = ModuleSpecifier::from_directory_path(local_path.parent().unwrap())
- .unwrap();
- }
-
- // workaround for url crate not adding a trailing slash for a directory
- // it seems to be fixed once a version greater than 2.2.2 is released
- let is_dir = to.path().ends_with('/');
- let mut text = from.make_relative(&to).unwrap();
- if is_dir && !text.ends_with('/') && to.query().is_none() {
- text.push('/');
- }
- text
- }
-
pub fn relative_specifier_text(
&self,
from: &ModuleSpecifier,
to: &ModuleSpecifier,
) -> String {
- let relative_path = self.relative_path(from, to);
-
- if relative_path.starts_with("../") || relative_path.starts_with("./") {
- relative_path
- } else {
- format!("./{}", relative_path)
- }
+ let from = self.local_uri(from);
+ let to = self.local_uri(to);
+ relative_specifier(&from, &to).unwrap()
}
pub fn base_specifiers(&self) -> &Vec<ModuleSpecifier> {
diff --git a/cli/tools/vendor/mod.rs b/cli/tools/vendor/mod.rs
index 3a5455aae..69c759154 100644
--- a/cli/tools/vendor/mod.rs
+++ b/cli/tools/vendor/mod.rs
@@ -3,19 +3,24 @@
use std::path::Path;
use std::path::PathBuf;
+use deno_ast::ModuleSpecifier;
use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::resolve_url_or_path;
use deno_runtime::permissions::Permissions;
+use log::warn;
+use crate::config_file::FmtOptionsConfig;
use crate::flags::VendorFlags;
use crate::fs_util;
+use crate::fs_util::relative_specifier;
+use crate::fs_util::specifier_to_file_path;
use crate::lockfile;
use crate::proc_state::ProcState;
use crate::resolver::ImportMapResolver;
use crate::resolver::JsxResolver;
-use crate::tools::vendor::specifiers::is_remote_specifier_text;
+use crate::tools::fmt::format_json;
mod analyze;
mod build;
@@ -33,14 +38,15 @@ pub async fn vendor(ps: ProcState, flags: VendorFlags) -> Result<(), AnyError> {
let output_dir = fs_util::resolve_from_cwd(&raw_output_dir)?;
validate_output_dir(&output_dir, &flags, &ps)?;
let graph = create_graph(&ps, &flags).await?;
- let vendored_count =
- build::build(&graph, &output_dir, &build::RealVendorEnvironment)?;
+ let vendored_count = build::build(
+ graph,
+ &output_dir,
+ ps.maybe_import_map.as_deref(),
+ &build::RealVendorEnvironment,
+ )?;
eprintln!(
- r#"Vendored {} {} into {} directory.
-
-To use vendored modules, specify the `--import-map` flag when invoking deno subcommands:
- deno run -A --import-map {} {}"#,
+ concat!("Vendored {} {} into {} directory.",),
vendored_count,
if vendored_count == 1 {
"module"
@@ -48,14 +54,31 @@ To use vendored modules, specify the `--import-map` flag when invoking deno subc
"modules"
},
raw_output_dir.display(),
- raw_output_dir.join("import_map.json").display(),
- flags
- .specifiers
- .iter()
- .map(|s| s.as_str())
- .find(|s| !is_remote_specifier_text(s))
- .unwrap_or("main.ts"),
);
+ if vendored_count > 0 {
+ let import_map_path = raw_output_dir.join("import_map.json");
+ if maybe_update_config_file(&output_dir, &ps) {
+ eprintln!(
+ concat!(
+ "\nUpdated your local Deno configuration file with a reference to the ",
+ "new vendored import map at {}. Invoking Deno subcommands will now ",
+ "automatically resolve using the vendored modules. You may override ",
+ "this by providing the `--import-map <other-import-map>` flag or by ",
+ "manually editing your Deno configuration file.",
+ ),
+ import_map_path.display(),
+ );
+ } else {
+ eprintln!(
+ concat!(
+ "\nTo use vendored modules, specify the `--import-map {}` flag when ",
+ r#"invoking Deno subcommands or add an `"importMap": "<path_to_vendored_import_map>"` "#,
+ "entry to a deno.json file.",
+ ),
+ import_map_path.display(),
+ );
+ }
+ }
Ok(())
}
@@ -76,7 +99,7 @@ fn validate_output_dir(
if let Some(import_map_path) = ps
.maybe_import_map
.as_ref()
- .and_then(|m| m.base_url().to_file_path().ok())
+ .and_then(|m| specifier_to_file_path(m.base_url()).ok())
.and_then(|p| fs_util::canonicalize_path(&p).ok())
{
// make the output directory in order to canonicalize it for the check below
@@ -87,10 +110,21 @@ fn validate_output_dir(
})?;
if import_map_path.starts_with(&output_dir) {
- // We don't allow using the output directory to help generate the new state
- // of itself because supporting this scenario adds a lot of complexity.
+ // canonicalize to make the test for this pass on the CI
+ let cwd = fs_util::canonicalize_path(&std::env::current_dir()?)?;
+ // We don't allow using the output directory to help generate the
+ // new state because this may lead to cryptic error messages.
bail!(
- "Using an import map found in the output directory is not supported."
+ concat!(
+ "Specifying an import map file ({}) in the deno vendor output ",
+ "directory is not supported. Please specify no import map or one ",
+ "located outside this directory."
+ ),
+ import_map_path
+ .strip_prefix(&cwd)
+ .unwrap_or(&import_map_path)
+ .display()
+ .to_string(),
);
}
}
@@ -98,6 +132,104 @@ fn validate_output_dir(
Ok(())
}
+fn maybe_update_config_file(output_dir: &Path, ps: &ProcState) -> bool {
+ assert!(output_dir.is_absolute());
+ let config_file = match &ps.maybe_config_file {
+ Some(f) => f,
+ None => return false,
+ };
+ let fmt_config = config_file
+ .to_fmt_config()
+ .unwrap_or_default()
+ .unwrap_or_default();
+ let result = update_config_file(
+ &config_file.specifier,
+ &ModuleSpecifier::from_file_path(output_dir.join("import_map.json"))
+ .unwrap(),
+ &fmt_config.options,
+ );
+ match result {
+ Ok(()) => true,
+ Err(err) => {
+ warn!("Error updating config file. {:#}", err);
+ false
+ }
+ }
+}
+
+fn update_config_file(
+ config_specifier: &ModuleSpecifier,
+ import_map_specifier: &ModuleSpecifier,
+ fmt_options: &FmtOptionsConfig,
+) -> Result<(), AnyError> {
+ if config_specifier.scheme() != "file" {
+ return Ok(());
+ }
+
+ let config_path = specifier_to_file_path(config_specifier)?;
+ let config_text = std::fs::read_to_string(&config_path)?;
+ let relative_text =
+ match relative_specifier(config_specifier, import_map_specifier) {
+ Some(text) => text,
+ None => return Ok(()), // ignore
+ };
+ if let Some(new_text) =
+ update_config_text(&config_text, &relative_text, fmt_options)
+ {
+ std::fs::write(config_path, new_text)?;
+ }
+
+ Ok(())
+}
+
+fn update_config_text(
+ text: &str,
+ import_map_specifier: &str,
+ fmt_options: &FmtOptionsConfig,
+) -> Option<String> {
+ use jsonc_parser::ast::ObjectProp;
+ use jsonc_parser::ast::Value;
+ let ast = jsonc_parser::parse_to_ast(text, &Default::default()).ok()?;
+ let obj = match ast.value {
+ Some(Value::Object(obj)) => obj,
+ _ => return None, // shouldn't happen, so ignore
+ };
+ let import_map_specifier = import_map_specifier.replace('\"', "\\\"");
+
+ match obj.get("importMap") {
+ Some(ObjectProp {
+ value: Value::StringLit(lit),
+ ..
+ }) => Some(format!(
+ "{}{}{}",
+ &text[..lit.range.start + 1],
+ import_map_specifier,
+ &text[lit.range.end - 1..],
+ )),
+ None => {
+ // insert it crudely at a position that won't cause any issues
+ // with comments and format after to make it look nice
+ let insert_position = obj.range.end - 1;
+ let insert_text = format!(
+ r#"{}"importMap": "{}""#,
+ if obj.properties.is_empty() { "" } else { "," },
+ import_map_specifier
+ );
+ let new_text = format!(
+ "{}{}{}",
+ &text[..insert_position],
+ insert_text,
+ &text[insert_position..],
+ );
+ format_json(&new_text, fmt_options)
+ .ok()
+ .map(|formatted_text| formatted_text.unwrap_or(new_text))
+ }
+ // shouldn't happen, so ignore
+ Some(_) => None,
+ }
+}
+
fn is_dir_empty(dir_path: &Path) -> Result<bool, AnyError> {
match std::fs::read_dir(&dir_path) {
Ok(mut dir) => Ok(dir.next().is_none()),
@@ -149,20 +281,85 @@ async fn create_graph(
.map(|im| im.as_resolver())
};
- let graph = deno_graph::create_graph(
- entry_points,
- false,
- maybe_imports,
- &mut cache,
- maybe_resolver,
- maybe_locker,
- None,
- None,
+ Ok(
+ deno_graph::create_graph(
+ entry_points,
+ false,
+ maybe_imports,
+ &mut cache,
+ maybe_resolver,
+ maybe_locker,
+ None,
+ None,
+ )
+ .await,
)
- .await;
+}
- graph.lock()?;
- graph.valid()?;
+#[cfg(test)]
+mod internal_test {
+ use super::*;
+ use pretty_assertions::assert_eq;
- Ok(graph)
+ #[test]
+ fn update_config_text_no_existing_props_add_prop() {
+ let text = update_config_text(
+ "{\n}",
+ "./vendor/import_map.json",
+ &Default::default(),
+ )
+ .unwrap();
+ assert_eq!(
+ text,
+ r#"{
+ "importMap": "./vendor/import_map.json"
+}
+"#
+ );
+ }
+
+ #[test]
+ fn update_config_text_existing_props_add_prop() {
+ let text = update_config_text(
+ r#"{
+ "tasks": {
+ "task1": "other"
+ }
+}
+"#,
+ "./vendor/import_map.json",
+ &Default::default(),
+ )
+ .unwrap();
+ assert_eq!(
+ text,
+ r#"{
+ "tasks": {
+ "task1": "other"
+ },
+ "importMap": "./vendor/import_map.json"
+}
+"#
+ );
+ }
+
+ #[test]
+ fn update_config_text_update_prop() {
+ let text = update_config_text(
+ r#"{
+ "importMap": "./local.json"
+}
+"#,
+ "./vendor/import_map.json",
+ &Default::default(),
+ )
+ .unwrap();
+ assert_eq!(
+ text,
+ r#"{
+ "importMap": "./vendor/import_map.json"
+}
+"#
+ );
+ }
}
diff --git a/cli/tools/vendor/test.rs b/cli/tools/vendor/test.rs
index 5060c493a..7a8feb94b 100644
--- a/cli/tools/vendor/test.rs
+++ b/cli/tools/vendor/test.rs
@@ -5,6 +5,7 @@ use std::collections::HashMap;
use std::collections::HashSet;
use std::path::Path;
use std::path::PathBuf;
+use std::sync::Arc;
use deno_ast::ModuleSpecifier;
use deno_core::anyhow::anyhow;
@@ -16,6 +17,10 @@ use deno_graph::source::LoadFuture;
use deno_graph::source::LoadResponse;
use deno_graph::source::Loader;
use deno_graph::ModuleGraph;
+use deno_graph::ModuleKind;
+use import_map::ImportMap;
+
+use crate::resolver::ImportMapResolver;
use super::build::VendorEnvironment;
@@ -120,6 +125,10 @@ struct TestVendorEnvironment {
}
impl VendorEnvironment for TestVendorEnvironment {
+ fn cwd(&self) -> Result<PathBuf, AnyError> {
+ Ok(make_path("/"))
+ }
+
fn create_dir_all(&self, dir_path: &Path) -> Result<(), AnyError> {
let mut directories = self.directories.borrow_mut();
for path in dir_path.ancestors() {
@@ -141,6 +150,10 @@ impl VendorEnvironment for TestVendorEnvironment {
.insert(file_path.to_path_buf(), text.to_string());
Ok(())
}
+
+ fn path_exists(&self, path: &Path) -> bool {
+ self.files.borrow().contains_key(&path.to_path_buf())
+ }
}
pub struct VendorOutput {
@@ -152,6 +165,8 @@ pub struct VendorOutput {
pub struct VendorTestBuilder {
entry_points: Vec<ModuleSpecifier>,
loader: TestLoader,
+ original_import_map: Option<ImportMap>,
+ environment: TestVendorEnvironment,
}
impl VendorTestBuilder {
@@ -161,6 +176,19 @@ impl VendorTestBuilder {
builder
}
+ pub fn new_import_map(&self, base_path: &str) -> ImportMap {
+ let base = ModuleSpecifier::from_file_path(&make_path(base_path)).unwrap();
+ ImportMap::new(base)
+ }
+
+ pub fn set_original_import_map(
+ &mut self,
+ import_map: ImportMap,
+ ) -> &mut Self {
+ self.original_import_map = Some(import_map);
+ self
+ }
+
pub fn add_entry_point(&mut self, entry_point: impl AsRef<str>) -> &mut Self {
let entry_point = make_path(entry_point.as_ref());
self
@@ -170,11 +198,24 @@ impl VendorTestBuilder {
}
pub async fn build(&mut self) -> Result<VendorOutput, AnyError> {
- let graph = self.build_graph().await;
let output_dir = make_path("/vendor");
- let environment = TestVendorEnvironment::default();
- super::build::build(&graph, &output_dir, &environment)?;
- let mut files = environment.files.borrow_mut();
+ let roots = self
+ .entry_points
+ .iter()
+ .map(|s| (s.to_owned(), deno_graph::ModuleKind::Esm))
+ .collect();
+ let loader = self.loader.clone();
+ let graph =
+ build_test_graph(roots, self.original_import_map.clone(), loader.clone())
+ .await;
+ super::build::build(
+ graph,
+ &output_dir,
+ self.original_import_map.as_ref(),
+ &self.environment,
+ )?;
+
+ let mut files = self.environment.files.borrow_mut();
let import_map = files.remove(&output_dir.join("import_map.json"));
let mut files = files
.iter()
@@ -193,27 +234,26 @@ impl VendorTestBuilder {
action(&mut self.loader);
self
}
+}
- async fn build_graph(&mut self) -> ModuleGraph {
- let graph = deno_graph::create_graph(
- self
- .entry_points
- .iter()
- .map(|s| (s.to_owned(), deno_graph::ModuleKind::Esm))
- .collect(),
- false,
- None,
- &mut self.loader,
- None,
- None,
- None,
- None,
- )
- .await;
- graph.lock().unwrap();
- graph.valid().unwrap();
- graph
- }
+async fn build_test_graph(
+ roots: Vec<(ModuleSpecifier, ModuleKind)>,
+ original_import_map: Option<ImportMap>,
+ mut loader: TestLoader,
+) -> ModuleGraph {
+ let resolver =
+ original_import_map.map(|m| ImportMapResolver::new(Arc::new(m)));
+ deno_graph::create_graph(
+ roots,
+ false,
+ None,
+ &mut loader,
+ resolver.as_ref().map(|im| im.as_resolver()),
+ None,
+ None,
+ None,
+ )
+ .await
}
fn make_path(text: &str) -> PathBuf {