jspm / generator

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

Extract partial import map required by a specific module(s) #132

Closed vovacodes closed 2 years ago

vovacodes commented 2 years ago

Another use-case we have at Framer arises from the modules copy/paste functionality. In Framer every project has its own import map, so when we copy/paste a component from one project to another we need to bring the "source" project's import map along with it and merge it with the "destination" one.

The reasons we won't simply use generator.traceInstall(pastedModuleURL) are:

Unfortunately this solution results in bloating the destination import map with a bunch of unnecessary resolutions that are not even used by the pasted module. A better alternative would be to merge only the part of the source import map that we know is required by the pasted module.

The API can be:

const generator = new Generator({
        mapUrl: "about:blank",
        inputMap,
})

// Extracts the part of the `inputMap` required by module1 and module2.
const submap = await generator.extractSubmap([module1URL, module2URL])
guybedford commented 2 years ago

What if we had a generator.installAll() or generator.reinstall() operation which would reinstall all the top-level installs, and apply pruning? The inputMap would imply the top-level imports so that module1URL and module2URL shouldn't be necessary. This seems to have a nice compatibility with a generator.uninstall().

vovacodes commented 2 years ago

Thing is, we don't feed the "entrypoint" modules to the generator because it's impractical in our case (generator is running in a separate worker and we have hundreds of equally important entrypoint modules). module1URL in my example is one of such "entrypoint" modules though, and what I'd like to get is a dependencies subgraph required for that specific module.

guybedford commented 2 years ago

@vovacodes would it make sense to treat all "imports" keys as entry points?

guybedford commented 2 years ago

This is now implemented in https://github.com/jspm/generator/pull/134 as generator.extractMap(['react', '...']).