Skip to content
Snippets Groups Projects
Commit 577055c3 authored by Marcus Eibrink-Lunzenauer's avatar Marcus Eibrink-Lunzenauer Committed by Jan-Hendrik Willms
Browse files

Re-throw error when failing to load a chunk.

Closes #4287

Merge request studip/studip!3103
parent 00c397a9
No related branches found
No related tags found
No related merge requests found
...@@ -8,95 +8,100 @@ export const loadScript = function (script_name) { ...@@ -8,95 +8,100 @@ export const loadScript = function (script_name) {
}); });
}; };
export const loadChunk = (function () { let mathjax_promise = null;
let mathjax_promise = null;
return function (chunk) { /** This function dynamically loads JS features organized in chunks.
let promise = null; *
switch (chunk) { * @param {string} chunk The name of the chunk to load.
* @param {{ silent: boolean }} options Options for loading the chunk.
* Pass `{ silent: true }` to supress
* error messages.
* @return {Promise}
*/
export const loadChunk = function (chunk, { silent = false } = {}) {
let promise = null;
switch (chunk) {
case 'code-highlight':
promise = import(
/* webpackChunkName: "code-highlight" */
'./chunks/code-highlight'
).then(({ default: hljs }) => {
return hljs;
});
break;
case 'code-highlight': case 'courseware':
promise = import( promise = Promise.all([
/* webpackChunkName: "code-highlight" */ STUDIP.loadChunk('vue'),
'./chunks/code-highlight' import(
).then(({default: hljs}) => { /* webpackChunkName: "courseware" */
return hljs; './chunks/courseware'
}); ),
break; ]).then(([Vue]) => Vue);
break;
case 'courseware': case 'chartist':
promise = Promise.all([ promise = import(
STUDIP.loadChunk('vue'), /* webpackChunkName: "chartist" */
import( './chunks/chartist'
/* webpackChunkName: "courseware" */ ).then(({ default: Chartist }) => Chartist);
'./chunks/courseware' break;
),
]).then(([Vue]) => Vue);
break;
case 'chartist': case 'fullcalendar':
promise = import( promise = import(
/* webpackChunkName: "chartist" */ /* webpackChunkName: "fullcalendar" */
'./chunks/chartist' './chunks/fullcalendar'
).then(({ default: Chartist }) => Chartist); );
break; break;
case 'fullcalendar': case 'tablesorter':
promise = import( promise = import(
/* webpackChunkName: "fullcalendar" */ /* webpackChunkName: "tablesorter" */
'./chunks/fullcalendar' './chunks/tablesorter'
); );
break; break;
case 'tablesorter': case 'mathjax':
promise = import( if (mathjax_promise === null) {
/* webpackChunkName: "tablesorter" */ mathjax_promise = STUDIP.loadScript('javascripts/mathjax/MathJax.js?config=TeX-AMS_HTML,default')
'./chunks/tablesorter' .then(() => {
);
break;
case 'mathjax':
if (mathjax_promise === null) {
mathjax_promise = STUDIP.loadScript(
'javascripts/mathjax/MathJax.js?config=TeX-AMS_HTML,default'
).then(() => {
(function (origPrint) { (function (origPrint) {
window.print = function () { window.print = function () {
window.MathJax.Hub.Queue( window.MathJax.Hub.Queue(['Delay', window.MathJax.Callback, 700], origPrint);
['Delay', window.MathJax.Callback, 700],
origPrint
);
}; };
})(window.print); })(window.print);
return window.MathJax; return window.MathJax;
}).catch(() => { })
console.log('Could not load mathjax') .catch(() => {
throw new Error('Could not load mathjax');
}); });
} }
promise = mathjax_promise; promise = mathjax_promise;
break; break;
case 'vue': case 'vue':
promise = import( promise = import(
/* webpackChunkName: "vue.js" */ /* webpackChunkName: "vue.js" */
'./chunks/vue' './chunks/vue'
); );
break; break;
case 'wysiwyg': case 'wysiwyg':
promise = import( promise = import(
/* webpackChunkName: "wysiwyg.js" */ /* webpackChunkName: "wysiwyg.js" */
'./chunks/wysiwyg' './chunks/wysiwyg'
); );
break; break;
default: default:
promise = Promise.reject(new Error(`Unknown chunk: ${chunk}`)); promise = Promise.reject(new Error(`Unknown chunk: ${chunk}`));
} }
return promise.catch((error) => { return promise.catch((error) => {
if (!silent) {
console.error(`Could not load chunk ${chunk}`, error); console.error(`Could not load chunk ${chunk}`, error);
}); }
}; throw error;
}()); });
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment