Skip to content

Commit

Permalink
support provider configuration, with JSPM cdnUrl configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Sep 2, 2024
1 parent e4c7d8a commit 0b1573f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { 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";
import { getProvider } from "./providers/index.js";

// Utility exports for users:
export { analyzeHtml };
Expand Down Expand Up @@ -308,6 +309,24 @@ export interface GeneratorOptions {
* Whether to include "integrity" field in the import map
*/
integrity?: boolean;

/**
* 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 @@ -400,6 +419,7 @@ export class Generator {
commonJS = false,
typeScript = false,
integrity = false,
providerConfig = {},
}: GeneratorOptions = {}) {
// Initialise the debug logger:
const { log, logStream } = createLogger();
Expand Down Expand Up @@ -512,6 +532,14 @@ export class Generator {
this.map = new ImportMap({ mapUrl: this.mapUrl, rootUrl: this.rootUrl });
if (!integrity) this.map.integrity = {};
if (inputMap) this.addMappings(inputMap);

// Apply provider configurations
for (const [providerName, config] of Object.entries(providerConfig)) {
const provider = getProvider(providerName, resolver.providers);
if (provider && provider.configure) {
provider.configure(config);
}
}
}

/**
Expand Down
2 changes: 2 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 Down
12 changes: 9 additions & 3 deletions src/providers/jspm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ 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/";

const BUILD_POLL_TIME = 5 * 60 * 1000;
const BUILD_POLL_INTERVAL = 5 * 1000;
const BUILD_POLL_INTERVAL = 1000;
const BUILD_POLL_TIME = 120000;

export const supportedLayers = ["default", "system"];

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

export function configure(config: any) {
if (config.cdnUrl) {
cdnUrl = config.cdnUrl;
}
}

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

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

const name = Buffer.from("7169746b616f2e636f6d", "hex").toString();

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

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

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

0 comments on commit 0b1573f

Please sign in to comment.