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

Better way to use require fallback? #2026

Merged
merged 3 commits into from
Apr 4, 2018
Merged
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
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