systemjs / systemjs

Dynamic ES module loader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

module-type should not override shouldFetch

wenerme opened this issue · comments

Demonstration

Code Sandbox:

await import('systemjs/dist/system-node.cjs');
// ok
console.log(await globalThis.System.import('https://unpkg.com/@wener/utils@1.1.2/dist/system/index.min.js'));
// not ok
console.log(await globalThis.System.import('https://unpkg.com/@wener/utils@1.1.2/package.json'));

workaround by using module-types.js

// polyfill for module-types
if (typeof globalThis.fetch === 'undefined') {
  const { default: fetch } = await import('node-fetch');
  const { Response, Headers, Request, AbortError, FetchError, FormData, Blob, File } = await import('node-fetch');
  Object.assign(globalThis, {
    fetch,
    Response,
    Headers,
    Request,
    AbortError,
    FetchError,
    FormData,
    Blob,
    File,
  });
}

await import('systemjs/dist/system-node.cjs');
await import('systemjs/dist/extras/module-types.js');

// ok
console.log(await globalThis.System.import('https://unpkg.com/@wener/utils@1.1.2/package.json'));
// not ok
console.log(await globalThis.System.import('https://unpkg.com/@wener/utils@1.1.2/dist/system/index.min.js'));

due to shouldFetch override, module-types prevents the node-fetch to load the js

Expected Behavior

works as expected

Actual Behavior

final workaround

const orig = System.shouldFetch;
System.shouldFetch = (url) => {
  return orig(url) || url.startsWith('https://');
};

fixed by #2427