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

feat: add fetchOptions to provide custom fetch behaivor control #315

Merged
merged 4 commits into from
Aug 9, 2023
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
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

126 changes: 63 additions & 63 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ export interface GeneratorOptions {
*/
cache?: "offline" | boolean;

/**
* User-provided fetch options for fetching modules, check https://github.com/npm/make-fetch-happen#extra-options
*/
fetchOptions?: Record<string, any>;

/**
* Custom provider definitions.
*
Expand Down Expand Up @@ -406,6 +411,7 @@ export class Generator {
providers,
resolutions = {},
cache = true,
fetchOptions = {},
ignore = [],
freeze,
latest,
Expand All @@ -425,18 +431,17 @@ export class Generator {
}

// Initialise the resource fetcher:
let fetchOpts;
let fetchOpts: Record<string, any> = {
retry: 1,
timeout: 10000,
...fetchOptions,
headers: { "Accept-Encoding": "gzip, br" },
}
if (cache === "offline")
fetchOpts = {
cache: "force-cache",
headers: { "Accept-Encoding": "gzip, br" },
};
fetchOpts.cache = "force-cache"
else if (!cache)
fetchOpts = {
cache: "no-store",
headers: { "Accept-Encoding": "gzip, br" },
};
else fetchOpts = { headers: { "Accept-Encoding": "gzip, br" } };
fetchOpts.cache = "no-store";

if (ipfsAPI) fetchOpts.ipfsAPI = ipfsAPI;

// Default logic for the mapUrl, baseUrl and rootUrl:
Expand Down Expand Up @@ -824,8 +829,8 @@ export class Generator {
const newlineTab = !whitespace
? analysis.newlineTab
: analysis.newlineTab.includes("\n")
? analysis.newlineTab
: "\n" + analysis.newlineTab;
? analysis.newlineTab
: "\n" + analysis.newlineTab;

const replacer = new Replacer(html);

Expand Down Expand Up @@ -868,14 +873,13 @@ export class Generator {
!!rootUrl
);

esms = `<script async src="${esmsUrl}" crossorigin="anonymous"${
integrity
? ` integrity="${await getIntegrity(
esmsUrl,
this.traceMap.resolver.fetchOpts
)}"`
: ""
}></script>${newlineTab}`;
esms = `<script async src="${esmsUrl}" crossorigin="anonymous"${integrity
? ` integrity="${await getIntegrity(
esmsUrl,
this.traceMap.resolver.fetchOpts
)}"`
: ""
}></script>${newlineTab}`;

if (analysis.esModuleShims)
replacer.remove(
Expand All @@ -896,28 +900,26 @@ export class Generator {
if (first || whitespace) preloads += newlineTab;
if (first) first = false;
if (integrity) {
preloads += `<link rel="modulepreload" href="${
rootUrl || htmlUrl
? relativeUrl(
new URL(dep),
new URL(rootUrl || htmlUrl),
!!rootUrl
)
: dep
}" integrity="${await getIntegrity(
dep,
this.traceMap.resolver.fetchOpts
)}" />`;
preloads += `<link rel="modulepreload" href="${rootUrl || htmlUrl
? relativeUrl(
new URL(dep),
new URL(rootUrl || htmlUrl),
!!rootUrl
)
: dep
}" integrity="${await getIntegrity(
dep,
this.traceMap.resolver.fetchOpts
)}" />`;
} else {
preloads += `<link rel="modulepreload" href="${
rootUrl || htmlUrl
? relativeUrl(
new URL(dep),
new URL(rootUrl || htmlUrl),
!!rootUrl
)
: dep
}" />`;
preloads += `<link rel="modulepreload" href="${rootUrl || htmlUrl
? relativeUrl(
new URL(dep),
new URL(rootUrl || htmlUrl),
!!rootUrl
)
: dep
}" />`;
}
}
}
Expand All @@ -929,11 +931,11 @@ export class Generator {
if (module.attrs.integrity) {
replacer.remove(
module.attrs.integrity.start -
(replacer.source[
replacer.idx(module.attrs.integrity.start - 1)
] === " "
? 1
: 0),
(replacer.source[
replacer.idx(module.attrs.integrity.start - 1)
] === " "
? 1
: 0),
module.attrs.integrity.end + 1
);
}
Expand Down Expand Up @@ -968,17 +970,17 @@ export class Generator {
analysis.map.start,
analysis.map.end,
(comment ? "<!--" + comment + "-->" + newlineTab : "") +
esms +
'<script type="importmap">' +
(whitespace ? newlineTab : "") +
JSON.stringify(map, null, whitespace ? 2 : 0).replace(
/\n/g,
newlineTab
) +
(whitespace ? newlineTab : "") +
"</script>" +
preloads +
(analysis.map.newScript ? newlineTab : "")
esms +
'<script type="importmap">' +
(whitespace ? newlineTab : "") +
JSON.stringify(map, null, whitespace ? 2 : 0).replace(
/\n/g,
newlineTab
) +
(whitespace ? newlineTab : "") +
"</script>" +
preloads +
(analysis.map.newScript ? newlineTab : "")
);

return replacer.source;
Expand Down Expand Up @@ -1543,12 +1545,10 @@ async function installToTarget(
(install.subpath !== "." && !install.subpath.startsWith("./")))
)
throw new Error(
`Install subpath "${
install.subpath
}" must be a string equal to "." or starting with "./".${
typeof install.subpath === "string"
? `\nTry setting the subpath to "./${install.subpath}"`
: ""
`Install subpath "${install.subpath
}" must be a string equal to "." or starting with "./".${typeof install.subpath === "string"
? `\nTry setting the subpath to "./${install.subpath}"`
: ""
}`
);

Expand Down