| 1 | import { build } from 'esbuild'; |
| 2 | import { cp, mkdir, readFile, rename, writeFile } from 'node:fs/promises'; |
| 3 | import { packageOptions } from '../.build/common.js'; |
| 4 | import { generateLangium } from '../.build/generateLangium.js'; |
| 5 | import type { MermaidBuildOptions } from './util.js'; |
| 6 | import { defaultOptions, getBuildConfig } from './util.js'; |
| 7 | |
| 8 | const shouldVisualize = process.argv.includes('--visualize'); |
| 9 | |
| 10 | const buildPackage = async (entryName: keyof typeof packageOptions) => { |
| 11 | const commonOptions: MermaidBuildOptions = { |
| 12 | ...defaultOptions, |
| 13 | options: packageOptions[entryName], |
| 14 | } as const; |
| 15 | const buildConfigs: MermaidBuildOptions[] = [ |
| 16 | // package.mjs |
| 17 | { ...commonOptions }, |
| 18 | // package.min.mjs |
| 19 | { |
| 20 | ...commonOptions, |
| 21 | minify: true, |
| 22 | metafile: shouldVisualize, |
| 23 | }, |
| 24 | // package.core.mjs |
| 25 | { ...commonOptions, core: true }, |
| 26 | ]; |
| 27 | |
| 28 | if (entryName === 'mermaid') { |
| 29 | const iifeOptions: MermaidBuildOptions = { ...commonOptions, format: 'iife' }; |
| 30 | buildConfigs.push( |
| 31 | // mermaid.js |
| 32 | { ...iifeOptions }, |
| 33 | // mermaid.min.js |
| 34 | { ...iifeOptions, minify: true, metafile: shouldVisualize }, |
| 35 | // mermaid.tiny.min.js |
| 36 | { |
| 37 | ...iifeOptions, |
| 38 | minify: true, |
| 39 | includeLargeFeatures: false, |
| 40 | metafile: shouldVisualize, |
| 41 | sourcemap: false, |
| 42 | } |
| 43 | ); |
| 44 | } |
| 45 | if (entryName === 'mermaid-zenuml') { |
| 46 | const iifeOptions: MermaidBuildOptions = { |
| 47 | ...commonOptions, |
| 48 | format: 'iife', |
| 49 | globalName: 'mermaid-zenuml', |
| 50 | }; |
| 51 | buildConfigs.push( |
| 52 | // mermaid-zenuml.js |
| 53 | { ...iifeOptions }, |
| 54 | // mermaid-zenuml.min.js |
| 55 | { ...iifeOptions, minify: true, metafile: shouldVisualize } |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | const results = await Promise.all(buildConfigs.map((option) => build(getBuildConfig(option)))); |
| 60 | |
| 61 | if (shouldVisualize) { |
| 62 | for (const { metafile } of results) { |
| 63 | if (!metafile?.outputs) { |
| 64 | continue; |
| 65 | } |
| 66 | const fileName = Object.keys(metafile.outputs) |
| 67 | .find((file) => !file.includes('chunks') && file.endsWith('js'))! |
| 68 | .replace('dist/', ''); |
| 69 | // Upload metafile into https://esbuild.github.io/analyze/ |
| 70 | await writeFile(`stats/${fileName}.meta.json`, JSON.stringify(metafile)); |
| 71 | } |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | const handler = (e) => { |
| 76 | // eslint-disable-next-line no-console |
| 77 | console.error(e); |
| 78 | process.exit(1); |
| 79 | }; |
| 80 | |
| 81 | const buildTinyMermaid = async () => { |
| 82 | await mkdir('./packages/tiny/dist', { recursive: true }); |
| 83 | await rename( |
| 84 | './packages/mermaid/dist/mermaid.tiny.min.js', |
| 85 | './packages/tiny/dist/mermaid.tiny.js' |
| 86 | ); |
| 87 | // Copy version from mermaid's package.json to tiny's package.json |
| 88 | const mermaidPkg = JSON.parse(await readFile('./packages/mermaid/package.json', 'utf8')); |
| 89 | const tinyPkg = JSON.parse(await readFile('./packages/tiny/package.json', 'utf8')); |
| 90 | tinyPkg.version = mermaidPkg.version; |
| 91 | |
| 92 | await writeFile('./packages/tiny/package.json', JSON.stringify(tinyPkg, null, 2) + '\n'); |
| 93 | await cp('./packages/mermaid/CHANGELOG.md', './packages/tiny/CHANGELOG.md'); |
| 94 | }; |
| 95 | |
| 96 | const main = async () => { |
| 97 | await generateLangium(); |
| 98 | await mkdir('stats', { recursive: true }); |
| 99 | const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[]; |
| 100 | // it should build `parser` before `mermaid` because it's a dependency |
| 101 | for (const pkg of packageNames) { |
| 102 | await buildPackage(pkg).catch(handler); |
| 103 | } |
| 104 | await buildTinyMermaid(); |
| 105 | }; |
| 106 | |
| 107 | void main(); |
| 108 | |