Skip to content

Commit

Permalink
fix(vendor): handle relative imports when mapped local folder name di…
Browse files Browse the repository at this point in the history
…ffers from remote's (denoland#14465)
  • Loading branch information
dsherret committed May 23, 2022
1 parent b65d502 commit d93b762
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
15 changes: 11 additions & 4 deletions cli/tools/vendor/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,14 @@ mod test {
.add("/mod.ts", "import 'https://localhost/mod.ts';")
.add(
"https://localhost/mod.ts",
"import './npm:[email protected]/test/test!cjs';",
"import './npm:[email protected]/test/test!cjs?test';import './npm:[email protected]/mod.ts';",
)
.add(
"https://localhost/npm:[email protected]/mod.ts",
"console.log(4);",
)
.add_with_headers(
"https://localhost/npm:[email protected]/test/test!cjs",
"https://localhost/npm:[email protected]/test/test!cjs?test",
"console.log(5);",
&[("content-type", "application/javascript")],
);
Expand All @@ -648,7 +652,9 @@ mod test {
},
"scopes": {
"./localhost/": {
"./localhost/npm:[email protected]/test/test!cjs": "./localhost/[email protected]/test/test!cjs.js"
"./localhost/npm:[email protected]/mod.ts": "./localhost/[email protected]/mod.ts",
"./localhost/npm:[email protected]/test/test!cjs?test": "./localhost/[email protected]/test/test!cjs.js",
"./localhost/[email protected]/test/test!cjs?test": "./localhost/[email protected]/test/test!cjs.js"
}
}
}))
Expand All @@ -658,8 +664,9 @@ mod test {
to_file_vec(&[
(
"/vendor/localhost/mod.ts",
"import './npm:[email protected]/test/test!cjs';"
"import './npm:[email protected]/test/test!cjs?test';import './npm:[email protected]/mod.ts';"
),
("/vendor/localhost/[email protected]/mod.ts", "console.log(4);"),
(
"/vendor/localhost/[email protected]/test/test!cjs.js",
"console.log(5);"
Expand Down
52 changes: 39 additions & 13 deletions cli/tools/vendor/import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ impl<'a> ImportsBuilder<'a> {
}

pub fn add(&mut self, key: String, specifier: &ModuleSpecifier) {
self.imports.insert(
key,
self
.mappings
.relative_specifier_text(self.mappings.output_dir(), specifier),
);
let value = self
.mappings
.relative_specifier_text(self.mappings.output_dir(), specifier);

// skip creating identity entries
if key != value {
self.imports.insert(key, value);
}
}
}

Expand Down Expand Up @@ -221,7 +223,8 @@ fn handle_dep_specifier(
return;
}

let key = if text.starts_with("./") || text.starts_with("../") {
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());
Expand All @@ -236,14 +239,37 @@ fn handle_dep_specifier(
)
});
local_base_specifier.set_query(unresolved_specifier.query());
mappings
.relative_specifier_text(mappings.output_dir(), &local_base_specifier)

imports.add(
mappings.relative_specifier_text(
mappings.output_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 mut value =
ModuleSpecifier::from_directory_path(local_path.parent().unwrap())
.unwrap();
value.set_query(specifier.query());
value.set_path(&format!(
"{}{}",
value.path(),
specifier.path_segments().unwrap().last().unwrap(),
));
mappings.relative_specifier_text(mappings.output_dir(), &value)
},
&specifier,
);
} else {
// absolute (`/`) or bare specifier should be left as-is
text.to_string()
};
let imports = import_map.scope(base_specifier);
imports.add(key, &specifier);
imports.add(text.to_string(), &specifier);
}
}
}

Expand Down

0 comments on commit d93b762

Please sign in to comment.