-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Import maps #2360
feat: Import maps #2360
Conversation
f10b9bf
to
cbf35ad
Compare
abb0eb6
to
cdc15a1
Compare
@ry please take a look. Got parsing working (there is still some cases with impl Loader for Worker {
type Error = DenoError;
fn resolve(specifier: &str, referrer: &str) -> Result<String, Self::Error> {
if import_map {
let result = import_map.resolve(specifier, referrer);
...
}
resolve_module_spec(specifier, referrer).map_err(DenoError::from)
}
...
} But it's not straight forward as |
5c8ba86
to
ffc298d
Compare
Okay this is more-or-less ready for some review.
Now it's time to integrate this remapping to module loading flow. @ry I will definitely need some help with that EDIT: Regarding use of |
I tried again to integrate import map resolving inside
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good start - nice work.
I'm most concerned about Value
types propagating beyond from_json
and the use of IndexMap
.
@ry I've updated the PR according to your feedback, please take a look at not-resolved comments to settle on the rest of them. Also if you could suggest something with respect to #2360 (comment) I'd be very happy |
I'm ok with adding the |
Yes, I'm having trouble making borrow checker work in |
This patch makes it so that RecursiveLoad doesn't own the Isolate, so Worker::execute_mod_async does not consume itself. Previously Worker implemented Loader, but now ThreadSafeState does. This is necessary preparation work for dynamic import (denoland#1789) and import maps (denoland#1921)
238812f
to
051ceaf
Compare
fe80eb2
to
145cb26
Compare
Working example: // importmap.json
{
"imports": {
"moment": "./moment.ts",
"lodash": ["./lodash.ts"]
}
}
// moment.ts
console.log("I'm remapped moment");
// lodash.ts
console.log("I'm remapped lodash");
// script.ts
import "moment";
import "lodash";
console.log("Import maps in action!");
|
A better example would be to map "http" to "https://deno.land/std/http/" , so that it looks like import { serve } from "http/server.ts";
async function main() {
const body = new TextEncoder().encode("Hello World\n");
for await (const req of serve(":8000")) {
req.respond({ body });
}
}
main(); Would be a nice one for the manual. |
3defdf9
to
f19d9c9
Compare
Arg::with_name("importmap") | ||
.long("importmap") | ||
.value_name("FILE") | ||
.help("Load import map file") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you link to the spec, gist, or manual so people can learn more about the file format?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--importmap <FILE>
Load import map file
Specification: https://wicg.github.io/import-maps/
Examples: https://github.com/WICG/import-maps#the-import-map
Caveat - visible only with deno --help
} | ||
} | ||
|
||
pub fn from_json( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it would be easier to use serde_derive to automatically generate a parser for this.
Might be a lot less code and reduce risk of bugs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went that way, but after several passes I decided that it's actually easier and less code to do it manually (coercing different types to array turned out to be a lot of code in serde_derive
). Test suite is pretty big, so this shouldn't be a problem
@@ -498,10 +499,30 @@ fn op_fetch_module_meta_data( | |||
let use_cache = !state.flags.reload; | |||
let no_fetch = state.flags.no_fetch; | |||
|
|||
// TODO(bartlomieju): I feel this is wrong - specifier is only resolved if there's an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the spec say about modules that don't appear in the import map?
Is there some default behavior to fall back to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's fall-through - in our case ImportMap::resolve
returns None and specifier is resolved as if there was no import map
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But my comment concerns whole op_fetch_module_meta_data
- I believe that there should be resolving of specifier even if there is no import map - to see that, please tak a look at tests/error_004_missing_module.ts
. It not returning proper error. More for reference in this comment
@ry @piscisaureus feedback addressed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - awesome work!
I think we’ll still need to iterate on this, but let’s do it in master and not have you keep rebasing.
🎉 Thanks! Great to have it landed! |
Great work, but I have a question: Why import_map.json with underscore? People usually use camelCase or kebab-case on JavaScript/TypeScript projects |
This PR aims to add support for import maps as described in #1921
For now code is rather poor quality, I intend to correct that in the next days.
It's still very early stage, things to be done:
reading import map from JSON (waiting Add serde_derive crate #2359)