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

Add mermaid.tiny.min.js. 69.7% size reduction. #4734

Open
wants to merge 26 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5f3c4fc
chore: Add tiny bundle
sidharthv96 Aug 14, 2023
a1541aa
chore: Split chunks into folders
sidharthv96 Aug 14, 2023
63d5808
Merge branch 'sidv/splitChunks' into sidv/tinyMermaid
sidharthv96 Aug 14, 2023
58a7447
Merge branch 'sidv/splitChunks' into sidv/tinyMermaid
sidharthv96 Aug 14, 2023
13a6f04
chore: Add vite support for define
sidharthv96 Aug 14, 2023
9632049
v11.0.0-alpha.3
sidharthv96 Aug 14, 2023
7afe87b
Merge branch 'sidv/splitChunks' into sidv/tinyMermaid
sidharthv96 Aug 14, 2023
0280316
Remove sourcemaps
sidharthv96 Aug 14, 2023
91b7d42
chore: Highlight if tiny version is used in error and info.
sidharthv96 Aug 14, 2023
5f95a26
fix: Center version text in SVG
sidharthv96 Aug 14, 2023
55266c4
chore: Simplify diagram exclusion
sidharthv96 Aug 16, 2023
905cc36
chore: Fix ordering issue
sidharthv96 Aug 16, 2023
841619a
Merge branch 'next' into sidv/tinyMermaid
sidharthv96 Aug 17, 2023
6936f6c
Merge branch 'next' into sidv/tinyMermaid
sidharthv96 Aug 17, 2023
e32747d
Merge branch 'next' into sidv/tinyMermaid
sidharthv96 Aug 19, 2023
87b0025
Merge branch 'develop' into sidv/tinyMermaid
sidharthv96 Sep 8, 2023
5feecec
Merge branch 'next' into sidv/tinyMermaid
sidharthv96 Sep 8, 2023
bd1789c
Merge branch 'next' into sidv/tinyMermaid
sidharthv96 Jan 24, 2024
b23b97f
Fix artifact upload
sidharthv96 Jan 24, 2024
c1d3147
Ignore node_modules
sidharthv96 Jan 24, 2024
3a12a63
Merge branch 'next' into sidv/tinyMermaid
sidharthv96 Mar 1, 2024
882e497
Remove katex from mermaid.tiny
sidharthv96 Mar 1, 2024
cedc1d4
Merge branch 'develop' into sidv/tinyMermaid
sidharthv96 Mar 8, 2024
80eaafa
Update docs
sidharthv96 Mar 8, 2024
49f2de9
Merge branch 'develop' into sidv/tinyMermaid
sidharthv96 Mar 23, 2024
70038e9
Merge branch 'develop' into sidv/tinyMermaid
sidharthv96 Apr 16, 2024
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
9 changes: 8 additions & 1 deletion .esbuild/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ const buildPackage = async (entryName: keyof typeof packageOptions) => {
// mermaid.js
{ ...iifeOptions },
// mermaid.min.js
{ ...iifeOptions, minify: true, metafile: shouldVisualize }
{ ...iifeOptions, minify: true, metafile: shouldVisualize },
// mermaid.tiny.min.js
{
...iifeOptions,
minify: true,
includeLargeFeatures: false,
metafile: shouldVisualize,
}
);
}

Expand Down
16 changes: 13 additions & 3 deletions .esbuild/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export interface MermaidBuildOptions {
metafile: boolean;
format: 'esm' | 'iife';
entryName: keyof typeof packageOptions;
includeLargeFeatures: boolean;
}

export const defaultOptions: Omit<MermaidBuildOptions, 'entryName'> = {
minify: false,
metafile: false,
core: false,
format: 'esm',
includeLargeFeatures: true,
} as const;

const buildOptions = (override: BuildOptions): BuildOptions => {
Expand All @@ -34,25 +36,31 @@ const buildOptions = (override: BuildOptions): BuildOptions => {
external: ['require', 'fs', 'path'],
outdir: 'dist',
plugins: [jisonPlugin, jsonSchemaPlugin],
sourcemap: 'external',
// sourcemap: 'linked',
...override,
};
};

const getFileName = (fileName: string, { core, format, minify }: MermaidBuildOptions) => {
const getFileName = (
fileName: string,
{ core, format, minify, includeLargeFeatures }: MermaidBuildOptions
) => {
if (core) {
fileName += '.core';
} else if (format === 'esm') {
fileName += '.esm';
}
if (!includeLargeFeatures) {
fileName += '.tiny';
}
if (minify) {
fileName += '.min';
}
return fileName;
};

export const getBuildConfig = (options: MermaidBuildOptions): BuildOptions => {
const { core, entryName, metafile, format, minify } = options;
const { core, entryName, metafile, format, includeLargeFeatures, minify } = options;
const external: string[] = ['require', 'fs', 'path'];
const { name, file, packageName } = packageOptions[entryName];
const outFileName = getFileName(name, options);
Expand All @@ -66,6 +74,8 @@ export const getBuildConfig = (options: MermaidBuildOptions): BuildOptions => {
logLevel: 'info',
chunkNames: `chunks/${outFileName}/[name]-[hash]`,
define: {
// This needs to be stringified for esbuild
includeLargeFeatures: `${includeLargeFeatures}`,
'import.meta.vitest': 'undefined',
},
});
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ jobs:
- name: Upload Mermaid Build as Artifact
uses: actions/upload-artifact@v4
with:
name: mermaid-build
name: mermaid
path: packages/mermaid/dist

- name: Upload Mermaid Mindmap Build as Artifact
- name: Upload all packages as Artifact
uses: actions/upload-artifact@v4
with:
name: mermaid-mindmap-build
path: packages/mermaid-mindmap/dist
name: mermaid-all
path: |
packages/**/dist
!**/node_modules
4 changes: 4 additions & 0 deletions .vite/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export const getBuildConfig = ({ minify, core, watch, entryName }: BuildOptions)
}),
...visualizerOptions(packageName, core),
],
define: {
// Needs to be string
includeLargeFeatures: 'true',
},
};

if (watch && config.build) {
Expand Down
2 changes: 1 addition & 1 deletion docs/config/setup/interfaces/mermaidAPI.ParseOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ The `parseError` function will not be called.

#### Defined in

[mermaidAPI.ts:64](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L64)
[mermaidAPI.ts:66](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L66)
2 changes: 1 addition & 1 deletion docs/config/setup/interfaces/mermaidAPI.ParseResult.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ The diagram type, e.g. 'flowchart', 'sequence', etc.

#### Defined in

[mermaidAPI.ts:71](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L71)
[mermaidAPI.ts:73](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L73)
6 changes: 3 additions & 3 deletions docs/config/setup/interfaces/mermaidAPI.RenderResult.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bindFunctions?.(div); // To call bindFunctions only if it's present.

#### Defined in

[mermaidAPI.ts:94](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L94)
[mermaidAPI.ts:96](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L96)

---

Expand All @@ -51,7 +51,7 @@ The diagram type, e.g. 'flowchart', 'sequence', etc.

#### Defined in

[mermaidAPI.ts:84](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L84)
[mermaidAPI.ts:86](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L86)

---

Expand All @@ -63,4 +63,4 @@ The svg code for the rendered graph.

#### Defined in

[mermaidAPI.ts:80](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L80)
[mermaidAPI.ts:82](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L82)
18 changes: 9 additions & 9 deletions docs/config/setup/modules/mermaidAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Renames and re-exports [mermaidAPI](mermaidAPI.md#mermaidapi)

#### Defined in

[mermaidAPI.ts:74](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L74)
[mermaidAPI.ts:76](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L76)

## Variables

Expand Down Expand Up @@ -98,7 +98,7 @@ mermaid.initialize(config);

#### Defined in

[mermaidAPI.ts:635](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L635)
[mermaidAPI.ts:637](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L637)

## Functions

Expand Down Expand Up @@ -129,7 +129,7 @@ Return the last node appended

#### Defined in

[mermaidAPI.ts:277](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L277)
[mermaidAPI.ts:279](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L279)

---

Expand All @@ -155,7 +155,7 @@ the cleaned up svgCode

#### Defined in

[mermaidAPI.ts:223](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L223)
[mermaidAPI.ts:225](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L225)

---

Expand All @@ -180,7 +180,7 @@ the string with all the user styles

#### Defined in

[mermaidAPI.ts:153](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L153)
[mermaidAPI.ts:155](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L155)

---

Expand All @@ -203,7 +203,7 @@ the string with all the user styles

#### Defined in

[mermaidAPI.ts:200](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L200)
[mermaidAPI.ts:202](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L202)

---

Expand All @@ -230,7 +230,7 @@ with an enclosing block that has each of the cssClasses followed by !important;

#### Defined in

[mermaidAPI.ts:138](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L138)
[mermaidAPI.ts:140](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L140)

---

Expand All @@ -256,7 +256,7 @@ Put the svgCode into an iFrame. Return the iFrame code

#### Defined in

[mermaidAPI.ts:254](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L254)
[mermaidAPI.ts:256](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L256)

---

Expand All @@ -281,4 +281,4 @@ Remove any existing elements from the given document

#### Defined in

[mermaidAPI.ts:327](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L327)
[mermaidAPI.ts:329](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L329)
8 changes: 6 additions & 2 deletions packages/mermaid/src/diagram-api/diagram-orchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { packet } from '../diagrams/packet/detector.js';
import block from '../diagrams/block/blockDetector.js';
import { registerLazyLoadedDiagrams } from './detectType.js';
import { registerDiagram } from './diagramAPI.js';
import '../type.d.ts';

let hasLoadedDiagrams = false;
export const addDiagrams = () => {
Expand Down Expand Up @@ -66,6 +67,11 @@ export const addDiagrams = () => {
return text.toLowerCase().trimStart().startsWith('---');
}
);

if (includeLargeFeatures) {
registerLazyLoadedDiagrams(flowchartElk, mindmap);
}

// Ordering of detectors is important. The first one to return true will be used.
registerLazyLoadedDiagrams(
c4,
Expand All @@ -77,10 +83,8 @@ export const addDiagrams = () => {
pie,
requirement,
sequence,
flowchartElk,
flowchartV2,
flowchart,
mindmap,
timeline,
git,
stateV2,
Expand Down
45 changes: 26 additions & 19 deletions packages/mermaid/src/diagrams/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,27 +341,34 @@ export const renderKatex = async (text: string, config: MermaidConfig): Promise<
return text.replace(katexRegex, 'MathML is unsupported in this environment.');
}

const { default: katex } = await import('katex');
return text
.split(lineBreakRegex)
.map((line) =>
hasKatex(line)
? `<div style="display: flex; align-items: center; justify-content: center; white-space: nowrap;">
if (includeLargeFeatures) {
const { default: katex } = await import('katex');
return text
.split(lineBreakRegex)
.map((line) =>
hasKatex(line)
? `<div style="display: flex; align-items: center; justify-content: center; white-space: nowrap;">
${line}
</div>`
: `<div>${line}</div>`
)
.join('')
.replace(katexRegex, (_, c) =>
katex
.renderToString(c, {
throwOnError: true,
displayMode: true,
output: isMathMLSupported() ? 'mathml' : 'htmlAndMathml',
})
.replace(/\n/g, ' ')
.replace(/<annotation.*<\/annotation>/g, '')
);
: `<div>${line}</div>`
)
.join('')
.replace(katexRegex, (_, c) =>
katex
.renderToString(c, {
throwOnError: true,
displayMode: true,
output: isMathMLSupported() ? 'mathml' : 'htmlAndMathml',
})
.replace(/\n/g, ' ')
.replace(/<annotation.*<\/annotation>/g, '')
);
}

return text.replace(
katexRegex,
'Katex is unsupported in mermaid.tiny.js. Please use mermaid.js or mermaid.min.js.'
);
};

export default {
Expand Down
2 changes: 1 addition & 1 deletion packages/mermaid/src/diagrams/error/errorDiagram.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DiagramDefinition } from '../../diagram-api/types.js';
import { renderer } from './errorRenderer.js';

const diagram: DiagramDefinition = {
export const diagram: DiagramDefinition = {
db: {},
renderer,
parser: {
Expand Down
13 changes: 6 additions & 7 deletions packages/mermaid/src/diagrams/info/infoRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { log } from '../../logger.js';
import { configureSvgSize } from '../../setupGraphViewbox.js';
import type { DrawDefinition, Group, SVG } from '../../diagram-api/types.js';
import type { DrawDefinition, SVG } from '../../diagram-api/types.js';
import { selectSvgElement } from '../../rendering-util/selectSvgElement.js';

/**
Expand All @@ -15,15 +15,14 @@ const draw: DrawDefinition = (text, id, version) => {

const svg: SVG = selectSvgElement(id);
configureSvgSize(svg, 100, 400, true);

const group: Group = svg.append('g');
group
svg
.append('text')
.attr('x', 100)
.attr('y', 40)
.attr('x', '50%')
.attr('y', '50%')
.attr('class', 'version')
.attr('font-size', 32)
.style('text-anchor', 'middle')
.attr('dominant-baseline', 'middle')
.attr('text-anchor', 'middle')
.text(`v${version}`);
};

Expand Down
4 changes: 3 additions & 1 deletion packages/mermaid/src/mermaidAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { select } from 'd3';
import { compile, serialize, stringify } from 'stylis';
// @ts-ignore: TODO Fix ts errors
import { version } from '../package.json';
import { version as packageVersion } from '../package.json';
import * as configApi from './config.js';
import { addDiagrams } from './diagram-api/diagram-orchestration.js';
import { Diagram } from './Diagram.js';
Expand All @@ -32,6 +32,8 @@ import type { DiagramMetadata, DiagramStyleClassDef } from './diagram-api/types.
import { preprocessDiagram } from './preprocess.js';
import { decodeEntities } from './utils.js';

const version = packageVersion + (includeLargeFeatures ? '' : '-tiny');

const MAX_TEXTLENGTH = 50_000;
const MAX_TEXTLENGTH_EXCEEDED_MSG =
'graph TB;a[Maximum text size in diagram exceeded];style a fill:#faa';
Expand Down
2 changes: 2 additions & 0 deletions packages/mermaid/src/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line no-var
declare var includeLargeFeatures: boolean;
2 changes: 2 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export default defineConfig({
},
},
define: {
// Needs to be string
includeLargeFeatures: 'true',
'import.meta.vitest': 'undefined',
},
});
Loading