fix(vendor): handle relative imports when mapped local folder name differs from remote's (#14465)

This commit is contained in:
David Sherret 2022-05-23 12:49:28 -04:00 committed by GitHub
parent b65d5024ef
commit d93b7627f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 17 deletions

View file

@ -628,10 +628,14 @@ mod test {
.add("/mod.ts", "import 'https://localhost/mod.ts';")
.add(
"https://localhost/mod.ts",
"import './npm:test@1.0.0/test/test!cjs';",
"import './npm:test@1.0.0/test/test!cjs?test';import './npm:test@1.0.0/mod.ts';",
)
.add(
"https://localhost/npm:test@1.0.0/mod.ts",
"console.log(4);",
)
.add_with_headers(
"https://localhost/npm:test@1.0.0/test/test!cjs",
"https://localhost/npm:test@1.0.0/test/test!cjs?test",
"console.log(5);",
&[("content-type", "application/javascript")],
);
@ -648,7 +652,9 @@ mod test {
},
"scopes": {
"./localhost/": {
"./localhost/npm:test@1.0.0/test/test!cjs": "./localhost/npm_test@1.0.0/test/test!cjs.js"
"./localhost/npm:test@1.0.0/mod.ts": "./localhost/npm_test@1.0.0/mod.ts",
"./localhost/npm:test@1.0.0/test/test!cjs?test": "./localhost/npm_test@1.0.0/test/test!cjs.js",
"./localhost/npm_test@1.0.0/test/test!cjs?test": "./localhost/npm_test@1.0.0/test/test!cjs.js"
}
}
}))
@ -658,8 +664,9 @@ mod test {
to_file_vec(&[
(
"/vendor/localhost/mod.ts",
"import './npm:test@1.0.0/test/test!cjs';"
"import './npm:test@1.0.0/test/test!cjs?test';import './npm:test@1.0.0/mod.ts';"
),
("/vendor/localhost/npm_test@1.0.0/mod.ts", "console.log(4);"),
(
"/vendor/localhost/npm_test@1.0.0/test/test!cjs.js",
"console.log(5);"

View file

@ -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);
}
}
}
@ -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());
@ -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);
}
}
}