jspm / generator

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

question: export custom provider implemention "resolveLatestTarget" #314

Open Bisstocuz opened 11 months ago

Bisstocuz commented 11 months ago

I wanna write a custom provider like unpkg.ts to use vite-plugin-jspm.

But these code below cannot work:

import { ExactPackage } from '@jspm/generator/src/install/package'

const cdnUrl = 'https://npm.<proxy-url>.com/'

export async function pkgToUrl(pkg: ExactPackage): Promise<`${string}/`> {
    return `${cdnUrl}${pkg.name}@${pkg.version}/`
}

const exactPkgRegEx = /^((?:@[^/\\%@]+\/)?[^./\\%@][^/\\%@]*)@([^/]+)(\/.*)?$/

export function parseUrlPkg(url: string) {
    if (!url.startsWith(cdnUrl)) return null
    const [, name, version] = url.slice(cdnUrl.length).match(exactPkgRegEx) || []
    if (name && version) {
        return { registry: 'npm', name, version }
    }
    return null
}

export { resolveLatestTarget } from '@jspm/generator/src/providers/jspm'

Vite reported:

X [ERROR] No known conditions for "./src/providers/jspm" specifier in "@jspm/generator" package [plugin externalize-deps]

How to call resolveLatestTarget outside of @jspm/generator?

guybedford commented 11 months ago

Good point, it's not currently exposed. The best I can suggest right now would just be to copy and paste the implementation exactly.

Bisstocuz commented 11 months ago

Good point, it's not currently exposed. The best I can suggest right now would just be to copy and paste the implementation exactly.

Another question is, if I tried to implement own resolveLatestTarget, the TypeScript will give a warning "cannot assign this: Provider to this: Resolver". I'm really getting numb from TypeScript.

guybedford commented 11 months ago

This should definitely be easier... you're welcome to make any suggestions to the public interface to improve the user experience here.