Skip to content

Commit

Permalink
Support provider configuration, with JSPM cdnUrl configuration (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Sep 2, 2024
1 parent 85b4f54 commit 7e75053
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { Replacer } from "./common/str.js";
import { analyzeHtml } from "./html/analyze.js";
import { InstallTarget, type InstallMode } from "./install/installer.js";
import { LockResolutions } from "./install/lock.js";
import { getDefaultProviderStrings, type Provider } from "./providers/index.js";
import { configureProviders, getDefaultProviderStrings, type Provider } from "./providers/index.js";
import * as nodemodules from "./providers/nodemodules.js";
import { Resolver } from "./trace/resolver.js";
import { getMaybeWrapperUrl } from "./common/wrapper.js";
Expand Down Expand Up @@ -315,6 +315,24 @@ export interface GeneratorOptions {
* Defaults to 3.
*/
fetchRetries?: number;

/**
* Provider configuration options
*
* @example
* ```js
* const generator = new Generator({
* mapUrl: import.meta.url,
* defaultProvider: "jspm.io",
* providerConfig: {
* "jspm.io": {
* cdnUrl: `https://jspm-mirror.com/`
* }
* }
*/
providerConfig?: {
[providerName: string]: any;
};
}

export interface ModuleAnalysis {
Expand Down Expand Up @@ -408,6 +426,7 @@ export class Generator {
typeScript = false,
integrity = false,
fetchRetries,
providerConfig = {},
}: GeneratorOptions = {}) {
// Initialise the debug logger:
const { log, logStream } = createLogger();
Expand Down Expand Up @@ -524,6 +543,8 @@ export class Generator {
// Set the fetch retry count
if (typeof fetchRetries === 'number')
setRetryCount(fetchRetries);

configureProviders(providerConfig, resolver.providers);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export interface Provider {
): Promise<PackageConfig | null>;

supportedLayers?: string[];

configure?(config: any): void;
}

export const defaultProviders: Record<string, Provider> = {
Expand All @@ -68,6 +70,15 @@ export function getProvider(name: string, providers: Record<string, Provider>) {
throw new JspmError(`No provider named "${name}" has been defined.`);
}

// Apply provider configurations
export function configureProviders(providerConfig: Record<string, any>, providers: Record<string, Provider>) {
for (const [providerName, provider] of Object.entries(providers)) {
if (provider.configure) {
provider.configure(providerConfig[providerName] || {});
}
}
}

export function getDefaultProviderStrings() {
let res = [];
for (const [name, provider] of Object.entries(defaultProviders)) {
Expand Down
6 changes: 5 additions & 1 deletion src/providers/jspm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SemverRange } from "sver";
// @ts-ignore
import { fetch } from "#fetch";

const cdnUrl = "https://ga.jspm.io/";
let cdnUrl = "https://ga.jspm.io/";
const systemCdnUrl = "https://ga.system.jspm.io/";
const apiUrl = "https://api.jspm.io/";

Expand All @@ -25,6 +25,10 @@ export async function pkgToUrl(
return `${layer === "system" ? systemCdnUrl : cdnUrl}${pkgToStr(pkg)}/`;
}

export function configure(config: any) {
cdnUrl = config.cdnUrl || "https://ga.jspm.io/";
}

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

Expand Down
26 changes: 26 additions & 0 deletions test/providers/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Generator } from "@jspm/generator";
import assert from "assert";

// this private origin shouldn't really be shared publicly
const name = [111, 97, 107, 116, 105, 113].map(x => String.fromCharCode(x)).reverse().join('');

// Test with custom CDN URL
{
const generator = new Generator({
mapUrl: import.meta.url,
defaultProvider: "jspm.io",
providerConfig: {
"jspm.io": {
cdnUrl: `https://${name}.com/`
}
}
});

await generator.install("react@17.0.1");
const json = generator.getMap();

assert.strictEqual(
json.imports.react,
`https://${name}.com/npm:react@17.0.1/dev.index.js`
);
}

0 comments on commit 7e75053

Please sign in to comment.