-
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
Improve ease of use for import maps #3585
Comments
I think Deno's current CLI philosophy is to be explicit about files like this. Needing to write out the whole command is never really a problem because you could wrap it in a script, otherwise the permission flags or any other would be a huge annoyance. |
Hmm, I get your point about being explicit with files, especially since this has to do with module resolution. A alias for the flag would still be nice.
What do you mean by this? Could you point to an example? I don't think I've seen or read about this pattern in Deno. |
IMO introducing such command is similar to introducing You can leverage stuff like shell script or plainly anything that could spawn a new Deno process (it is possible for third party to develop tools that does such simplification) However generally I believe we are avoiding to let these things slip into the Deno CLI itself. |
Sounds good, what about the remote modules question? Can library authors that expects their modules to be |
Not being able to use import maps in my reusable library modules has been annoying for me, too. One idea I came up with is to create a tool that looks at the dependency tree of the main entry script and tries to find import maps based on the URLs of those files. So if you import It feels a little hacky and there could be problems with this approach. Import maps could conflict, among other things. However, I think that could be solved by using scopes in the generated import map. At the very least, it would be a start towards something usable. |
Maybe @domenic has some suggestions about how to use import maps in a reusable module? |
@sholladay The only way to use import maps in libraries that I see (that wouldn't involve automatically trying to download any special files with magic names) would be either to substitute them in a build step and distribute files with fully qualified import URLs and no source maps (so your users and Deno doesn't have to know that you use import maps in development but then you need a build step) or to use something like what @kitsonk is proposing for the type definitions in this comment, e.g.:
But Deno (currently) can use only a single import map for everything, so merging multiple import maps into one could cause conflicts if multiple libraries used the same prefix. I don't think it can be solved unless Deno either uses multiple import maps (without merging them) for individual files, or maybe Deno resolves the imports and substitutes them in the source code before passing the files to TSC. My concern in that case would be about browser compatibility because I don't see anything like that in https://wicg.github.io/import-maps/ and it would mean having TS files with unresolvable imports that make sense only with some external meta data (which would be pretty much like package.json). This is not an issue for Deno-specific things like importing Maybe it has already been considered, I wonder what Domenic thinks about it. |
The vision for import maps themselves is that they apply at an application level. An application's import map will generally be composed by tooling that takes into account a view of the entire application, including any reusable modules that the application contains. If reusable modules want to use bare specifiers, e.g In systems like npm, they can provide additional out-of-band signals (viz. package.json) to give more details on this request. I.e., a npm-published module that references On the web, so far what we've seen is people using the npm registry and package.json system as input into import-map creation. Deno may want to follow that system, or may want to create its own. One advantage of creating its own system, is that import maps allow A sketch of such a system would be to reuse the import map file format as a sort of Deno-package.json, and then have a command like |
@domenic thanks for your input, and it is understandable. While your final solution is logical, there is this point:
Where we have long had a goal of trying to avoid any sort of "project meta data" as that has been a monkey on the back of Node.js for a long long time and to take an approach where code is code. The paradigm being a web application, which doesn't have this sort of meta data, and the complete programme exists as code. Admittedly we have things now like "manifest.json" and the like which enable things like PWAs, but I argue that we should try to ensure we can fully describe the programme from code. Personally I would rather us steer towards some solution where any sort of meta data resolution, like "merging" of import-maps be described in code, where |
Yeah, that makes sense. I guess the logical thing there would be to add the version ranges (and registry URLs?) in the import specifiers, i.e. instead of |
Currently in Deno, we support arbitrary information in the import specifier, in the sense it is up to whomever is serving the resource to determine whatever meaning. It is just URLs. So in Deno you would just: import * as foo from "https://myregistry.org/foo@^1.2.3/index.ts"; Or whatever semantics that is meaningful to the server providing the module. The same applies to import-maps as well. The challenge at the moment is that unlike a browser which can specify an arbitrary amount of import-maps as part of the HTML via the |
I was just thinking about this. This problem is a lot like the type definitions problem, where we have two scenarios:
In a browser, it is always the latter, and the hoster delivers that information in the form of The type definitions was the same thing, where we only solved the first problem, but I have a PR open to solve the second problem, and a similar solution could work for this too. We could do the following:
|
single-file scriptsI think on this level, this can be easily made convenient via hierarchical online modules needing source mapsIf we go with the “hoster provides a map” we’re in pioneering/extending territory again. I like deno’s philosphy of not barreling down its own path while leaving the rest of the JS ecosystem behind, just to find that it didn’t follow all the way and took a few different turns along the way. import securitySomething of an aside, but it ties into everything we’re talking here: We need integrity checks for imports, to make sure our application is safe when a server is compromised. This could maybe be fixed by extending the import map proposal? It would also mean that you would want a local import map or when downloading it, we’d want to download the import map without its own hash to verify it hasn’t been tampered with. |
Just running through some testing, it appears there's currently no support for import maps in several of the CLI's sub commands: We have issues for a couple of these which I'll link here for reference...
...but I can't find any issue for using import maps in |
@bartlomieju @kitsonk should we include the |
@wongjiahau import maps are supported for |
Actually it's also not working for "deno info", can I open a PR to allow importmap options for "deno install" and "deno info"? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
It would be nice if import maps could:
|
The first point is addressed in #9519. The second point is very unlikely to be implemented; though there is discussion about similar API for worker scope in the import map spec repository. We're aligning import maps to spec as per #9461 and plan to follow it closely, so I'm going to close this issue as it suggest adding APIs that are not part of the spec. |
If Deno is adding compatibility with Node, this feels pretty incompatible. Imagine having multiple, if not dozens, of flags for import maps that you have to adjust whenever you are updating your modules. That's a lot of noise. For Deno-native projects, fine. But for existing Node libraries trying to create an easy interop with Deno, this is a pretty discouraging decision. |
Import maps are an awesome feature, but having to type
--importmap=import_map.json
every time you run a script that depends on them is painful. This type of problem is less of a concern in Node, since I can add as much complexity as I want to an npm script and give it a nice alias.An example of a nicer experience would be to have an alias (maybe
-i
?) and a default (maybeimport_map.json
?) so that as a user I could just typedeno -i my-script.ts
Also, if I'm
import
ing a module that uses an import map to import other dependencies, how will it be able to handle the module resolution? Obviously I can't pass the import map flag to the remote module, and even if I could, I'd have to know the location of each of my dependancies import map. Are import maps only meant for use in non-consumable applications?The text was updated successfully, but these errors were encountered: