Skip to content

Commit

Permalink
Merge pull request #2026 from jupyter-widgets/require-fallback
Browse files Browse the repository at this point in the history
Better way to use require fallback?
  • Loading branch information
maartenbreddels authored Apr 4, 2018
2 parents fb317cb + 0f2aa2a commit 05b6bcb
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/html-manager/src/libembed-amd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,24 @@ function moduleNameToCDNUrl(moduleName: string, moduleVersion: string) {
fileName = moduleName.substr(index+1);
packageName = moduleName.substr(0, index);
}
return `https://unpkg.com/${packageName}@${moduleVersion}/dist/${fileName}.js`;
return `https://unpkg.com/${packageName}@${moduleVersion}/dist/${fileName}`;
}

function requireLoader(moduleName: string, moduleVersion: string) {
return requirePromise([`${moduleName}`]).catch((err) => {
let failedId = err.requireModules && err.requireModules[0];
if (failedId) {
console.log(`Falling back to unpkg.com for ${moduleName}@${moduleVersion}`);
return requirePromise([moduleNameToCDNUrl(moduleName, moduleVersion)]);
let require = (window as any).requirejs;
if (require === undefined) {
throw new Error("Requirejs is needed, please ensure it is loaded on the page.");
}
const conf = {paths: {}};
conf.paths[moduleName] = moduleNameToCDNUrl(moduleName, moduleVersion);
require.undef(failedId);
require.config(conf);

return requirePromise([`${moduleName}`]);
}
});
}
Expand Down

0 comments on commit 05b6bcb

Please sign in to comment.