fix: Diagram load issue
This commit is contained in:
parent
16be835c9b
commit
58a53c0fa8
|
@ -6,6 +6,7 @@ import pkg from '../package.json' assert { type: 'json' };
|
||||||
|
|
||||||
const { dependencies } = pkg;
|
const { dependencies } = pkg;
|
||||||
const watch = process.argv.includes('--watch');
|
const watch = process.argv.includes('--watch');
|
||||||
|
const mermaidOnly = process.argv.includes('--mermaid');
|
||||||
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
||||||
|
|
||||||
type OutputOptions = Exclude<
|
type OutputOptions = Exclude<
|
||||||
|
@ -129,14 +130,19 @@ const buildPackage = async (entryName: keyof typeof packageOptions) => {
|
||||||
const main = async () => {
|
const main = async () => {
|
||||||
const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[];
|
const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[];
|
||||||
for (const pkg of packageNames) {
|
for (const pkg of packageNames) {
|
||||||
|
if (mermaidOnly && pkg !== 'mermaid') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
await buildPackage(pkg);
|
await buildPackage(pkg);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (watch) {
|
if (watch) {
|
||||||
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid' }));
|
build(getBuildConfig({ minify: false, watch, core: true, entryName: 'mermaid' }));
|
||||||
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-mindmap' }));
|
if (!mermaidOnly) {
|
||||||
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-example-diagram' }));
|
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-mindmap' }));
|
||||||
|
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-example-diagram' }));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
void main();
|
void main();
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
"git graph"
|
"git graph"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"build:mermaid": "ts-node-esm --transpileOnly --project=.vite/tsconfig.json .vite/build.ts --mermaid",
|
||||||
"build:vite": "ts-node-esm --transpileOnly --project=.vite/tsconfig.json .vite/build.ts",
|
"build:vite": "ts-node-esm --transpileOnly --project=.vite/tsconfig.json .vite/build.ts",
|
||||||
"build:types": "concurrently \"tsc -p ./packages/mermaid/tsconfig.json --emitDeclarationOnly\" \"tsc -p ./packages/mermaid-mindmap/tsconfig.json --emitDeclarationOnly\"",
|
"build:types": "concurrently \"tsc -p ./packages/mermaid/tsconfig.json --emitDeclarationOnly\" \"tsc -p ./packages/mermaid-mindmap/tsconfig.json --emitDeclarationOnly\"",
|
||||||
"build:watch": "pnpm build:vite --watch",
|
"build:watch": "pnpm build:vite --watch",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import * as configApi from './config';
|
import * as configApi from './config';
|
||||||
import { log } from './logger';
|
import { log } from './logger';
|
||||||
import { getDiagram, registerDiagram } from './diagram-api/diagramAPI';
|
import { DiagramNotFoundError, getDiagram, registerDiagram } from './diagram-api/diagramAPI';
|
||||||
import { detectType, getDiagramLoader } from './diagram-api/detectType';
|
import { detectType, getDiagramLoader } from './diagram-api/detectType';
|
||||||
import { isDetailedError } from './utils';
|
import { isDetailedError } from './utils';
|
||||||
export class Diagram {
|
export class Diagram {
|
||||||
|
@ -88,9 +88,13 @@ export const getDiagramFromText = async (txt: string, parseError?: Function): Pr
|
||||||
// Trying to find the diagram
|
// Trying to find the diagram
|
||||||
getDiagram(type);
|
getDiagram(type);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (!(error instanceof DiagramNotFoundError)) {
|
||||||
|
log.error(error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
const loader = getDiagramLoader(type);
|
const loader = getDiagramLoader(type);
|
||||||
if (!loader) {
|
if (!loader) {
|
||||||
throw new Error(`Diagram ${type} not found.`);
|
throw new Error(`Loader for ${type} not found.`);
|
||||||
}
|
}
|
||||||
// Diagram not available, loading it
|
// Diagram not available, loading it
|
||||||
const { diagram } = await loader();
|
const { diagram } = await loader();
|
||||||
|
|
|
@ -34,6 +34,7 @@ export const registerDiagram = (
|
||||||
_setupGraphViewbox: any
|
_setupGraphViewbox: any
|
||||||
) => void
|
) => void
|
||||||
) => {
|
) => {
|
||||||
|
log.debug(`Registering diagram ${id}`);
|
||||||
if (diagrams[id]) {
|
if (diagrams[id]) {
|
||||||
log.warn(`Diagram ${id} already registered.`);
|
log.warn(`Diagram ${id} already registered.`);
|
||||||
// The error throw is commented out to as it breaks pages where you have multiple diagrams,
|
// The error throw is commented out to as it breaks pages where you have multiple diagrams,
|
||||||
|
@ -50,11 +51,19 @@ export const registerDiagram = (
|
||||||
if (typeof callback !== 'undefined') {
|
if (typeof callback !== 'undefined') {
|
||||||
callback(log, setLogLevel, getConfig, sanitizeText, setupGraphViewbox);
|
callback(log, setLogLevel, getConfig, sanitizeText, setupGraphViewbox);
|
||||||
}
|
}
|
||||||
|
log.debug(`Registered diagram ${id}. ${Object.keys(diagrams).join(', ')} diagrams registered.`);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getDiagram = (name: string): DiagramDefinition => {
|
export const getDiagram = (name: string): DiagramDefinition => {
|
||||||
|
log.debug(`Getting diagram ${name}. ${Object.keys(diagrams).join(', ')} diagrams registered.`);
|
||||||
if (name in diagrams) {
|
if (name in diagrams) {
|
||||||
return diagrams[name];
|
return diagrams[name];
|
||||||
}
|
}
|
||||||
throw new Error(`Diagram ${name} not found.`);
|
throw new DiagramNotFoundError(name);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export class DiagramNotFoundError extends Error {
|
||||||
|
constructor(message: string) {
|
||||||
|
super(`Diagram ${message} not found.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -30,6 +30,8 @@ export const log: Record<keyof typeof LEVELS, typeof console.log> = {
|
||||||
* @param {LogLevel} [level="fatal"] The level to set the logging to. Default is `"fatal"`
|
* @param {LogLevel} [level="fatal"] The level to set the logging to. Default is `"fatal"`
|
||||||
*/
|
*/
|
||||||
export const setLogLevel = function (level: keyof typeof LEVELS | number | string = 'fatal') {
|
export const setLogLevel = function (level: keyof typeof LEVELS | number | string = 'fatal') {
|
||||||
|
// TODO: Even if we call initialize with loglevel 0, many initial logs are skipped as LL is set to 5 initially.
|
||||||
|
|
||||||
let numericLevel: number = LEVELS.fatal;
|
let numericLevel: number = LEVELS.fatal;
|
||||||
if (typeof level === 'string') {
|
if (typeof level === 'string') {
|
||||||
level = level.toLowerCase();
|
level = level.toLowerCase();
|
||||||
|
@ -39,6 +41,7 @@ export const setLogLevel = function (level: keyof typeof LEVELS | number | strin
|
||||||
} else if (typeof level === 'number') {
|
} else if (typeof level === 'number') {
|
||||||
numericLevel = level;
|
numericLevel = level;
|
||||||
}
|
}
|
||||||
|
numericLevel = 0;
|
||||||
log.trace = () => {};
|
log.trace = () => {};
|
||||||
log.debug = () => {};
|
log.debug = () => {};
|
||||||
log.info = () => {};
|
log.info = () => {};
|
||||||
|
|
Loading…
Reference in New Issue