jspm / generator

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

Expand package entrypoints utility - want to reuse #225

Open jorenbroekema opened 1 year ago

jorenbroekema commented 1 year ago

Hey! I am looking for some advice/guidance with regards to creating import maps from package entrypoints. I have tried @jspm/generator package. It works, generally speaking, but instead of choosing a single subpath, I want to build an import map that installs all possible subpaths of a package. I could loop over my exports entrypoints and do so, however, I have entrypoints with wildstar subpaths, so I'd have to expand those first.

I can write a util that does this, but I prefer using something that already exists and is more battle-tested.

I noticed that on JSPM CDN, my package's package.json "exports" map is expanded --> https://ga.jspm.io/npm:@lion/ui@0.1.4/package.json versus another CDN like unpkg where it only has the original exports with wildstars in them (not expanded) --> https://unpkg.com/browse/@lion/ui@0.1.4/package.json

I was wondering if someone could point me in the right direction, I'd love to reuse the logic that's used for JSPM CDN to expand package entrypoints (resolving those * chars in paths).

Basically going from:

{
  "exports": {
    "./*": {
      "types": "./dist-types/exports/*",
      "default": "./exports/*"
    },
    "./calendar-translations/*": "./components/calendar/translations/*"
  }
}

to:

{
  "exports": {
    "./accordion.d.ts.map": {
      "types": "./dist-types/exports/accordion.d.ts.map"
    },
    "./button.d.ts.map": {
      "types": "./dist-types/exports/button.d.ts.map"
    },
    "./calendar-test-helpers.d.ts.map": {
      "types": "./dist-types/exports/calendar-test-helpers.d.ts.map"
    },
    "./accordion.js": {
      "default": "./exports/accordion.js"
    },
    "./button.js": {
      "default": "./exports/button.js"
    },
    "./calendar-test-helpers.js": {
      "default": "./exports/calendar-test-helpers.js"
    },
    "./calendar-translations/en-GB.js": "./components/calendar/translations/en-GB.js",
    "./calendar-translations/nl-NL.js": "./components/calendar/translations/nl-NL.js"
  }
}
guybedford commented 1 year ago

This expansion is done by the CDN itself not the import map generation tooling. We did have an option to generate the "full map" in earlier versions of the generator but this is not currently exposed functionality since installing all subpaths quickly generated a very large map. Instead we treat the map as a specific traced subset always and encourage tracing the app each time through traceInstall. This is especially important since the import map is blocking on the main critical load path of the page.

We could certainly reenable full map generation though.

jorenbroekema commented 1 year ago

Yeah that would be great if such a feature could be exposed. For my use case these full import maps wouldn't be used on runtime but moreso for analysis reasons.

Even if the full map option is not public, could you share the utility for expanding the export map/subpaths?