From 4601c90904fb57f54ed91e31ea235b24de5add45 Mon Sep 17 00:00:00 2001 From: Alois Klink Date: Thu, 20 Oct 2022 10:20:18 +0100 Subject: [PATCH 1/3] fix: load lazyLoadedDiagrams in initThrowsErrors Previously, calling initThrowsErrors would not load any of the lazyLoadedDiagrams entries. --- packages/mermaid/src/mermaid.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/mermaid/src/mermaid.ts b/packages/mermaid/src/mermaid.ts index ae6c62547..51af1a827 100644 --- a/packages/mermaid/src/mermaid.ts +++ b/packages/mermaid/src/mermaid.ts @@ -45,16 +45,6 @@ const init = async function ( callback?: Function ) { try { - const conf = mermaidAPI.getConfig(); - if (conf?.lazyLoadedDiagrams && conf.lazyLoadedDiagrams.length > 0) { - // Load all lazy loaded diagrams in parallel - await Promise.allSettled( - conf.lazyLoadedDiagrams.map(async (diagram: string) => { - const { id, detector, loadDiagram } = await import(diagram); - addDetector(id, detector, loadDiagram); - }) - ); - } await initThrowsErrors(config, nodes, callback); } catch (e) { log.warn('Syntax Error rendering'); @@ -82,6 +72,16 @@ const initThrowsErrors = async function ( mermaid.sequenceConfig = config; } + if (conf?.lazyLoadedDiagrams && conf.lazyLoadedDiagrams.length > 0) { + // Load all lazy loaded diagrams in parallel + await Promise.allSettled( + conf.lazyLoadedDiagrams.map(async (diagram: string) => { + const { id, detector, loadDiagram } = await import(diagram); + addDetector(id, detector, loadDiagram); + }) + ); + } + // if last argument is a function this is the callback function log.debug(`${!callback ? 'No ' : ''}Callback function found`); let nodesToProcess: ArrayLike; From 895a5eb78a1b57dc49a0c73af129e9e923953170 Mon Sep 17 00:00:00 2001 From: Alois Klink Date: Thu, 20 Oct 2022 10:53:08 +0100 Subject: [PATCH 2/3] docs(mermaid): document initThrowsErrors Add some basic tsdoc for initThrowsErrors --- packages/mermaid/src/mermaid.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/mermaid/src/mermaid.ts b/packages/mermaid/src/mermaid.ts index 51af1a827..e7d9da84d 100644 --- a/packages/mermaid/src/mermaid.ts +++ b/packages/mermaid/src/mermaid.ts @@ -57,6 +57,18 @@ const init = async function ( } }; +/** + * Equivalent to {@link init()}, except an error will be thrown on error. + * + * @param config - **Deprecated** Mermaid sequenceConfig. + * @param nodes - One of: + * - A DOM Node + * - An array of DOM nodes (as would come from a jQuery selector) + * - A W3C selector, a la `.mermaid` (default) + * @param callback - Function that is called with the id of each generated mermaid diagram. + * + * @returns Resolves on success, otherwise the {@link Promise} will be rejected with an Error. + */ const initThrowsErrors = async function ( config?: MermaidConfig, // eslint-disable-next-line no-undef From 3a2669e63403a2e612187529f807a65d63a031aa Mon Sep 17 00:00:00 2001 From: Alois Klink Date: Thu, 20 Oct 2022 11:10:58 +0100 Subject: [PATCH 3/3] fix(mermaid): error if lazyLoadedDiagrams fails Throw an error if lazyLoadedDiagrams fails to load properly. Rendering is still performed, even on a lazyLoadedDiagrams failure. --- packages/mermaid/src/mermaid.spec.ts | 23 ++++++++++++++++++++++- packages/mermaid/src/mermaid.ts | 11 +++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/packages/mermaid/src/mermaid.spec.ts b/packages/mermaid/src/mermaid.spec.ts index 8cf180ae7..e3bc69448 100644 --- a/packages/mermaid/src/mermaid.spec.ts +++ b/packages/mermaid/src/mermaid.spec.ts @@ -48,11 +48,32 @@ describe('when using mermaid and ', function () { const node = document.createElement('div'); node.appendChild(document.createTextNode('graph TD;\na;')); - mermaid.initThrowsErrors(undefined, node); + await mermaid.initThrowsErrors(undefined, node); // mermaidAPI.render function has been mocked, since it doesn't yet work // in Node.JS (only works in browser) expect(mermaidAPI.render).toHaveBeenCalled(); }); + it('should throw error (but still render) if lazyLoadedDiagram fails', async () => { + const node = document.createElement('div'); + node.appendChild(document.createTextNode('graph TD;\na;')); + + mermaidAPI.setConfig({ + lazyLoadedDiagrams: ['this-file-does-not-exist.mjs'], + }); + await expect(mermaid.initThrowsErrors(undefined, node)).rejects.toThrowError( + // this error message is probably different on every platform + // this one is just for vite-note (node/jest/browser may be different) + 'Failed to load this-file-does-not-exist.mjs' + ); + + // should still render, even if lazyLoadedDiagrams fails + expect(mermaidAPI.render).toHaveBeenCalled(); + }); + + afterEach(() => { + // we modify mermaid config in some tests, so we need to make sure to reset them + mermaidAPI.reset(); + }); }); describe('checking validity of input ', function () { diff --git a/packages/mermaid/src/mermaid.ts b/packages/mermaid/src/mermaid.ts index e7d9da84d..9e91ec79d 100644 --- a/packages/mermaid/src/mermaid.ts +++ b/packages/mermaid/src/mermaid.ts @@ -84,14 +84,22 @@ const initThrowsErrors = async function ( mermaid.sequenceConfig = config; } + const errors = []; + if (conf?.lazyLoadedDiagrams && conf.lazyLoadedDiagrams.length > 0) { // Load all lazy loaded diagrams in parallel - await Promise.allSettled( + const results = await Promise.allSettled( conf.lazyLoadedDiagrams.map(async (diagram: string) => { const { id, detector, loadDiagram } = await import(diagram); addDetector(id, detector, loadDiagram); }) ); + for (const result of results) { + if (result.status == 'rejected') { + log.warn(`Failed to lazyLoadedDiagram due to `, result.reason); + errors.push(result.reason); + } + } } // if last argument is a function this is the callback function @@ -119,7 +127,6 @@ const initThrowsErrors = async function ( const idGenerator = new utils.initIdGenerator(conf.deterministicIds, conf.deterministicIDSeed); let txt: string; - const errors = []; // element is the current div with mermaid class for (const element of Array.from(nodesToProcess)) {