-
Notifications
You must be signed in to change notification settings - Fork 43
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
Standard way of resolving import maps #364
Comments
Hey Reed, could you please clarify the exact expected behaviour? Were you hoping for |
Perhaps I'm expecting deno_graph to do too much? Maybe this import map resolution logic lives in deno itself. But to clarify by example: whenever I click on an import in vs code, it takes me to the right spot. I want the same thing here. I have a file and an import map, and I want to get the same experience as in vs code. So yeah, fully resolved urls. |
@deer What you are looking for is |
Thanks, that helps quite a bit. I'll keep playing with it and perhaps this issue can just be closed with a small documentation change. |
The code went from this nightmare: function createCustomResolver(
imports: Record<string, string>,
projectLocation: string,
const isLocal = projectLocation.startsWith("file://");
const projectPath = isLocal
? path.fromFileUrl(projectLocation)
: projectLocation;
return (specifier: string, referrer: string) => {
for (const key of Object.keys(imports)) {
if (specifier.startsWith(key)) {
if (imports[key] === "./") {
const modifiedPath = path.join(
projectPath,
specifier.replace(key, ""),
);
return path.toFileUrl(modifiedPath).href;
} else {
specifier = specifier.replace(key, imports[key]);
}
break;
}
return new URL(specifier, referrer).toString();
};
} to the following: function createCustomResolver(
imports: ImportMap,
baseURL: URL,
) {
return (specifier: string, referrer: string) => {
if (/^(?:\.\.\/)+|^\.\//.test(specifier)) {
return path.join(path.dirname(referrer), specifier);
}
return resolveModuleSpecifier(specifier, imports, baseURL);
};
} I then discovered that https://deno.land/x/import_map is different than https://deno.land/x/importmap and that the first one is the official deno project. (I also discovered that my previous version wasn't correct in all cases, but was fine for the cases I was interested in.) So now my code is: function createCustomResolver(imports: ImportMap) {
return (specifier: string, referrer: string) => {
return imports.resolve(specifier, referrer);
};
} I think I've finally reached the end goal. Thanks again @lucacasonato. |
You might be able to just use |
Is there no standard way of resolving import maps?
I have a simple project:
And then I have a slightly less simple test file:
When I run the above I get the following output:
Is there something I'm missing? I would expect a standard way of passing in my
Record<string, string>
and having the resolution take place in the same way the cli does it.This came up when trying to programmatically chase down dependencies of a fresh plugin. See denoland/fresh#2266 for the real code. Basically I need valid URLs for all dependencies of a plugin, in order to get the content of the files so that tailwind can extract the classes. This is of course only possible if the import map resolution takes place.
The text was updated successfully, but these errors were encountered: