jspm / generator

JSPM Import Map Generator
Apache License 2.0
165 stars 21 forks source link

feat: flat rescoping with multi-version reconstruction #170

Closed guybedford closed 2 years ago

guybedford commented 2 years ago

This updates the input map decomposition to handle a new feature which is that it's possible for the same package name in an import map to flatten multiple versions into the flattened scope. We maintain this as special "flattened resolution" data which is like normal resolution data but also keyed by subpath, which is extracted during decomposition and then repopulated into the specific scopes as the main install trace runs.

For example. consider the map:

{
  "imports": {
    "pkg": "https://ga.jspm.io/npm:pkg/main.js"
  },
  "scopes": {
    "https://ga.jspm.io/": {
      "dep": "https://ga.jspm.io/npm:dep@1.2.3/main.js",
      "dep/feature": "https://ga.jspm.io/npm:dep@2.3.4/feature.js"
    }
  }
}

One dependency may have had import 'dep' to dep version 1, while another dependency had import 'dep/feature' to dep version 2. Flattening is able to combine these scopes when the import map is output since there isn't any duplication at the specifier level, but we then end up with the same package name having different versions for different subpaths.

This resolution information is now fully reconstructed in the lock data structure and maintained - whereas before one version would override the other.

Note that in "imports" one version will still override the other when this happens, since at the dependency level there is still the expectation that you only have one version of a dependency, or if you have multi versions of a dependency that you at least use aliasing (interestingly, @fusionstrings this exactly comes up at the same time as you brought up this point!).

Resolves https://github.com/jspm/generator/issues/165.