jspm / generator

JSPM Import Map Generator
Apache License 2.0
160 stars 20 forks source link

Arbitrary URL installs #291

Closed guybedford closed 1 year ago

guybedford commented 1 year ago

I think it would be useful to consider installation of arbitrary URLs.

At the moment we install URLs as packages, with an optional subpath which is looked up in exports.

But when users do know the exact internal subpath, it can be useful to think in terms of installing a full module path.

Strictly speaking this isn't an install operation, it's more of an add operation. But I think treating it as an install makes sense from an end-user perspective to consider eg:

jspm install react app=./src/app.js external=https://cdn.com/external/module.js

The steps required would be:

URL install steps

  1. If the install ends in a / treat that as the enforced package boundary to install and stop
  2. If the install does not end in a /, determine the package boundary using the normal package boundary detection algorithm.
  3. Skip exports resolution entirely (i.e. the path within the package boundary is not what we call the subpath), instead we jump straight to the later phase in resolution in treating this as the fully resolved module URL.
  4. Like we do in decomposition, if possible decompose that mapping into exports when forming the saved primary resolution, thereby deducing the subpath only at this stage (exports subpath).

I think it's a nice ease-of-use feature to have for those just wanting basic import map management, and now that decomposition is clever enough to handle these kinds of things I think it could even work quite intuitively.

guybedford commented 1 year ago

A way to cheat in the above could be to make this a CLI-only feature that instead takes the following steps:

  1. Are we installing a target that is URL-like - starts with ./, ../, / or is a URL, AND the target does not end in a trailing /? If so continue with the steps below, otherwise install it like a normal install target.
  2. Write the URL mapping into the "imports" overriding whatever is there
  3. Perform a normal install operation, where the decomposition on initialization should handle the setup correctly.
Bubblyworld commented 1 year ago

Yeah, I started trying to tackle this at the generator level but it's a bit gnarlier than I expected (I mistook installSubpath for "already resolved subpath"). So I think I'm going to take the cheat approach for now.

Bubblyworld commented 1 year ago

...but I think an approach that'll work in the generator is to expand the InstallTarget type to be a union of the current { pkgTarget, installSubpath } type and a URL, and then implementing the logic in the OP in the installer for URL targets.