Skip to content

Commit

Permalink
Stop using "require" to get auth fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
NSeydoux committed Sep 10, 2021
1 parent b6e7562 commit 5f47afc
Showing 1 changed file with 13 additions and 44 deletions.
57 changes: 13 additions & 44 deletions src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,56 +29,25 @@ const defaultFetch: typeof window.fetch = async (resource, init) => {
if (typeof window === "object" && typeof require !== "function") {
return window.fetch(resource, init);
}
/* istanbul ignore if: `require` is always defined in the unit test environment */
if (typeof require !== "function") {
// Note: The following commented-out block may cause Webpack to fail, because it
// tries to resolve the import (even when the import is legitimately unresolved).
// Working around this requires to use the `IgnorePlugin` from Webpack, which
// would be a burden to dependants with a legitimate use case. For this reason,
// automatically detecting if @inrupt/solid-client-authn-browser may be imported
// is disabled for the time being.

// When using Node.js with ES Modules, require is not defined:
// let fetch;
// try {
// // solid-client-authn-browser may be unresolved, we just try to autodetect it.
// const { fetch: defaultSessionFetch } = await import(
// /* eslint-disable import/no-unresolved */
// // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// // @ts-ignore
// "@inrupt/solid-client-authn-browser"
// );
// fetch = defaultSessionFetch;
// } catch (e) {
// const crossFetchModule = await import("cross-fetch");
// fetch = crossFetchModule.default;
// return await fetch(resource, init);
// }
const crossFetchModule = await import("cross-fetch");
const fetch = crossFetchModule.default;
return fetch(resource, init);
}
// Implementation note: it's up to the client application to resolve these module names to their
// respective npm packages. At least one commonly used tool (Webpack) is only able to do that if
// the module names are literal strings.
// Additionally, Webpack throws a warning in a way that halts compilation for at least Next.js
// when using native JavaScript dynamic imports (`import()`), whereas `require()` just logs a
// warning. Since the use of package names instead of file names requires a bundler anyway, this
// should not have any practical consequences. For more background, see:
// https://github.com/webpack/webpack/issues/7713
// eslint-disable-next-line no-shadow
let fetch;

try {
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
const sca = require("@inrupt/solid-client-authn-browser");
/* istanbul ignore next : `solid-client-authn-browser` is not a dependency of this library */
fetch = sca.fetch;
// solid-client-authn-browser may be unresolved, we just try to autodetect it.
const { fetch: defaultSessionFetch } = await import(
/* eslint-disable import/no-unresolved */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
"@inrupt/solid-client-authn-browser"
);
fetch = defaultSessionFetch;
} catch (e) {
// eslint-disable-next-line prefer-const, global-require
fetch = require("cross-fetch");
const crossFetchModule = await import("cross-fetch");
fetch = crossFetchModule.default;
// return await fetch(resource, init);
}

// const crossFetchModule = await import("cross-fetch");
// fetch = crossFetchModule.default;
return fetch(resource, init);
};

Expand Down

0 comments on commit 5f47afc

Please sign in to comment.