aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>2024-06-13 08:00:05 +0000
committerJan-Hendrik Willms <tleilax+github@gmail.com>2024-06-13 10:10:52 +0200
commiteb71022a83e6b443c2b318216496b03f3a552ea7 (patch)
tree66ab32ed4b1ab961bf5ac7b58e72bffd399782de
parentd626df1ca2b44cfb900809a71c2389c0d4ad05b0 (diff)
Re-throw error when failing to load a chunk.
Closes #4287 Merge request studip/studip!3103
-rw-r--r--resources/assets/javascripts/chunk-loader.js123
1 files changed, 64 insertions, 59 deletions
diff --git a/resources/assets/javascripts/chunk-loader.js b/resources/assets/javascripts/chunk-loader.js
index 81bb04b..e93f21f 100644
--- a/resources/assets/javascripts/chunk-loader.js
+++ b/resources/assets/javascripts/chunk-loader.js
@@ -8,78 +8,83 @@ STUDIP.loadScript = function (script_name) {
});
};
-STUDIP.loadChunk = (function () {
- var mathjax_promise = null;
+let mathjax_promise = null;
- return function (chunk) {
- var promise = null;
- switch (chunk) {
+/** This function dynamically loads JS features organized in chunks.
+ *
+ * @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':
- promise = import(
- /* webpackChunkName: "code-highlight" */
- './chunks/code-highlight'
- ).then(({default: hljs}) => {
- return hljs;
- });
- break;
+ case 'chartist':
+ promise = import(
+ /* webpackChunkName: "chartist" */
+ './chunks/chartist'
+ ).then(({ default: Chartist }) => Chartist);
+ break;
- case 'chartist':
- promise = import(
- /* webpackChunkName: "chartist" */
- './chunks/chartist'
- ).then(({ default: Chartist }) => Chartist);
- break;
+ case 'fullcalendar':
+ promise = import(
+ /* webpackChunkName: "fullcalendar" */
+ './chunks/fullcalendar'
+ );
+ break;
- case 'fullcalendar':
- promise = import(
- /* webpackChunkName: "fullcalendar" */
- './chunks/fullcalendar'
- );
- break;
+ case 'tablesorter':
+ promise = import(
+ /* webpackChunkName: "tablesorter" */
+ './chunks/tablesorter'
+ );
+ break;
- case 'tablesorter':
- promise = import(
- /* webpackChunkName: "tablesorter" */
- './chunks/tablesorter'
- );
- break;
-
- case 'mathjax':
- if (mathjax_promise === null) {
- mathjax_promise = STUDIP.loadScript(
- 'javascripts/mathjax/MathJax.js?config=TeX-AMS_HTML,default'
- ).then(() => {
+ case 'mathjax':
+ if (mathjax_promise === null) {
+ mathjax_promise = STUDIP.loadScript('javascripts/mathjax/MathJax.js?config=TeX-AMS_HTML,default')
+ .then(() => {
(function (origPrint) {
window.print = function () {
- window.MathJax.Hub.Queue(
- ['Delay', window.MathJax.Callback, 700],
- origPrint
- );
+ window.MathJax.Hub.Queue(['Delay', window.MathJax.Callback, 700], origPrint);
};
})(window.print);
return window.MathJax;
- }).catch(() => {
- console.log('Could not load mathjax')
+ })
+ .catch(() => {
+ throw new Error('Could not load mathjax');
});
- }
- promise = mathjax_promise;
- break;
+ }
+ promise = mathjax_promise;
+ break;
- case 'vue':
- promise = import(
- /* webpackChunkName: "vue.js" */
- './chunks/vue'
- );
- break;
+ case 'vue':
+ promise = import(
+ /* webpackChunkName: "vue.js" */
+ './chunks/vue'
+ );
+ break;
- default:
- promise = Promise.reject(new Error(`Unknown chunk: ${chunk}`));
- }
+ default:
+ 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);
- });
- };
-}());
+ }
+ throw error;
+ });
+};