Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support provider configuration, with JSPM cdnUrl configuration #377

Merged
merged 4 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("[email protected]");
const json = generator.getMap();

assert.strictEqual(
json.imports.react,
`https://${name}.com/npm:[email protected]/dev.index.js`
);
}
Loading