jspm / generator

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

`deno` as defaultProvider is loading depdendencies from `npm` #367

Open JayaKrishnaNamburu opened 1 month ago

JayaKrishnaNamburu commented 1 month ago

When passing defaultProvider as deno to Generator. The dependencies should be loaded from denoland. But instead the dependencies are being loaded from npm but they are loading from denoland if users pass denoland protocol for the package name.

Here are some examples

const generator = new Generator({
  defaultProvider: "deno",
});
await generator.install("zod");
await generator.install("lodash");

console.log(
  `Installing by passing defaultProvider as deno \n`,
  JSON.stringify(generator.getMap(), null, 2),
);

Will result in an importmap of

{
  "imports": {
    "lodash": "https://ga.jspm.io/npm:lodash@4.17.21/lodash.js",
    "zod": "https://ga.jspm.io/npm:zod@3.23.8/lib/index.mjs"
  }
}

The dependencies are loaded from ga.jspm.io with npm: as prefix for protocol.

When we pass the protocol for the package name. Then it loaded from deno registry.

const generatorNPM = new Generator();
await generatorNPM.install("denoland:zod");
await generatorNPM.install("lodash");
console.log(
  `Installing by passing denoland: protocol for the dependency \n`,
  JSON.stringify(generatorNPM.getMap(), null, 2),
);

Will result in an importmap of

{
  "imports": {
    "lodash": "https://ga.jspm.io/npm:lodash@4.17.21/lodash.js",
    "zod": "https://deno.land/x/zod@v3.23.8/mod.ts"
  }
}

Here the package zod comes from denoland since the protocol is denoland. But we can see the package loadash is coming from npm here as expected.

Let's try the same with skypack

const generatorSkypack = new Generator({
  defaultProvider: "skypack",
});
await generatorSkypack.install("zod");
console.log(
  `Installing by passing defaultProvider as skypack \n`,
  JSON.stringify(generatorSkypack.getMap(), null, 2),
);

This will result in an import of

{
  "imports": {
    "zod": "https://cdn.skypack.dev/zod@3.23.8/mod.ts"
  }
}

Here the package loads from skypack respecting the defaultProtocol that is passed to the generator. Sandbox for the same to test.

JayaKrishnaNamburu commented 1 month ago

Looks like the bug might be from here https://github.com/jspm/generator/blob/main/src/install/installer.ts#L137

if (opts.defaultRegistry) this.defaultRegistry = opts.defaultRegistry;

Before assigning the his.defaultRegistry. We need to do something like

if (opts.defaultRegistry) {
  this.defaultRegistry =
    registryProviders[`${opts.defaultProvider}:`] ?? opts.defaultRegistry;
}

So, when a provider of denoland is selected, it automatically assigns to denoland registry. The only issue i see for the approach is, the standard libs of deno are moved to jsr which in turn need to add support for jsr #366

guybedford commented 1 month ago

We have two separate providers for Deno:

That is, you would want to set defaultProvider: 'denoland' here.

JayaKrishnaNamburu commented 1 month ago

Thanks for the clarification @guybedford. I am not aware of that, but looking like even when we set the defaultProvider to denoland. The import map seems to load from the ga.jspm.io

const generator = new Generator({
  defaultProvider: "denoland",
});
await generator.install("zod");
await generator.install("lodash");

console.log(
  `Installing by passing defaultProvider as deno land\n`,
  JSON.stringify(generator.getMap(), null, 2),
);

is generating

{
  "imports": {
    "lodash": "https://ga.jspm.io/npm:lodash@4.17.21/lodash.js",
    "zod": "https://ga.jspm.io/npm:zod@3.23.8/lib/index.mjs"
  }
}