mirror of
				https://github.com/mermaid-js/mermaid.git
				synced 2025-10-30 02:14:15 +01:00 
			
		
		
		
	chore: Split chunks into folders
This commit is contained in:
		| @@ -1,40 +1,47 @@ | |||||||
| import { build } from 'esbuild'; | import { build } from 'esbuild'; | ||||||
| import { mkdir, writeFile } from 'node:fs/promises'; | import { mkdir, writeFile } from 'node:fs/promises'; | ||||||
| import { getBuildConfig } from './util.js'; | import { MermaidBuildOptions, defaultConfig, getBuildConfig } from './util.js'; | ||||||
| import { packageOptions } from '../.build/common.js'; | import { packageOptions } from '../.build/common.js'; | ||||||
|  |  | ||||||
| const shouldVisualize = process.argv.includes('--visualize'); | const shouldVisualize = process.argv.includes('--visualize'); | ||||||
|  |  | ||||||
| const buildPackage = async (entryName: keyof typeof packageOptions) => { | const buildPackage = async (entryName: keyof typeof packageOptions) => { | ||||||
|   // package.mjs |   const commonConfig = { ...defaultConfig, entryName }; | ||||||
|   await build(getBuildConfig({ entryName, minify: false })); |   const configs = [ | ||||||
|   // package.min.mjs |     { ...commonConfig }, | ||||||
|   const { metafile } = await build( |     { | ||||||
|     getBuildConfig({ entryName, minify: true, metafile: shouldVisualize }) |       ...commonConfig, | ||||||
|   ); |       minify: true, | ||||||
|   if (metafile) { |       metafile: shouldVisualize, | ||||||
|     // Upload metafile into https://esbuild.github.io/analyze/ |     }, | ||||||
|     await writeFile(`stats/meta-${entryName}.json`, JSON.stringify(metafile)); |     { ...commonConfig, core: true }, | ||||||
|   } |   ]; | ||||||
|   // package.core.mjs |  | ||||||
|   await build(getBuildConfig({ entryName, minify: false, core: true })); |  | ||||||
|   if (entryName === 'mermaid') { |   if (entryName === 'mermaid') { | ||||||
|     // mermaid.js |     const iifeConfig: MermaidBuildOptions = { ...commonConfig, format: 'iife' }; | ||||||
|     await build(getBuildConfig({ entryName, minify: false, format: 'iife' })); |     configs.push( | ||||||
|     // mermaid.min.js |       { ...iifeConfig }, | ||||||
|     await build(getBuildConfig({ entryName, minify: true, format: 'iife' })); |       { ...iifeConfig, minify: true }, | ||||||
|     // mermaid.tiny.min.js |       { | ||||||
|     const { metafile } = await build( |         ...iifeConfig, | ||||||
|       getBuildConfig({ |  | ||||||
|         entryName, |  | ||||||
|         minify: true, |         minify: true, | ||||||
|         includeLargeDiagrams: false, |         includeLargeDiagrams: false, | ||||||
|         metafile: shouldVisualize, |         metafile: shouldVisualize, | ||||||
|         format: 'iife', |       } | ||||||
|       }) |  | ||||||
|     ); |     ); | ||||||
|     if (metafile) { |   } | ||||||
|       await writeFile(`stats/meta-${entryName}-tiny.json`, JSON.stringify(metafile)); |  | ||||||
|  |   const results = await Promise.all(configs.map((config) => build(getBuildConfig(config)))); | ||||||
|  |  | ||||||
|  |   if (shouldVisualize) { | ||||||
|  |     for (const { metafile } of results) { | ||||||
|  |       if (!metafile) { | ||||||
|  |         continue; | ||||||
|  |       } | ||||||
|  |       const fileName = Object.keys(metafile.outputs) | ||||||
|  |         .filter((key) => key.includes('.min') && key.endsWith('js'))[0] | ||||||
|  |         .replace('dist/', ''); | ||||||
|  |       await writeFile(`stats/${fileName}.meta.json`, JSON.stringify(metafile)); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| }; | }; | ||||||
| @@ -47,9 +54,7 @@ const handler = (e) => { | |||||||
| const main = async () => { | const main = async () => { | ||||||
|   await mkdir('stats').catch(() => {}); |   await mkdir('stats').catch(() => {}); | ||||||
|   const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[]; |   const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[]; | ||||||
|   for (const pkg of packageNames) { |   await Promise.allSettled(packageNames.map((pkg) => buildPackage(pkg).catch(handler))); | ||||||
|     await buildPackage(pkg).catch(handler); |  | ||||||
|   } |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
| void main(); | void main(); | ||||||
|   | |||||||
| @@ -8,15 +8,23 @@ import { jisonPlugin } from './jisonPlugin.js'; | |||||||
|  |  | ||||||
| const __dirname = fileURLToPath(new URL('.', import.meta.url)); | const __dirname = fileURLToPath(new URL('.', import.meta.url)); | ||||||
|  |  | ||||||
| interface MermaidBuildOptions { | export interface MermaidBuildOptions { | ||||||
|   minify: boolean; |   minify: boolean; | ||||||
|   core?: boolean; |   core: boolean; | ||||||
|   metafile?: boolean; |   metafile: boolean; | ||||||
|   format?: 'esm' | 'iife'; |   format: 'esm' | 'iife'; | ||||||
|   entryName: keyof typeof packageOptions; |   entryName: keyof typeof packageOptions; | ||||||
|   includeLargeDiagrams?: boolean; |   includeLargeDiagrams: boolean; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | export const defaultConfig: Omit<MermaidBuildOptions, 'entryName'> = { | ||||||
|  |   minify: false, | ||||||
|  |   metafile: false, | ||||||
|  |   core: false, | ||||||
|  |   format: 'esm', | ||||||
|  |   includeLargeDiagrams: true, | ||||||
|  | } as const; | ||||||
|  |  | ||||||
| const buildOptions = (override: BuildOptions): BuildOptions => { | const buildOptions = (override: BuildOptions): BuildOptions => { | ||||||
|   return { |   return { | ||||||
|     bundle: true, |     bundle: true, | ||||||
| @@ -33,25 +41,38 @@ const buildOptions = (override: BuildOptions): BuildOptions => { | |||||||
|   }; |   }; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| export const getBuildConfig = ({ | const getFileName = ( | ||||||
|   minify, |   fileName: string, | ||||||
|   core, |   { core, format, includeLargeDiagrams, minify, entryName }: MermaidBuildOptions | ||||||
|   entryName, | ) => { | ||||||
|   metafile, |   if (core) { | ||||||
|   format = 'esm', |     fileName += '.core'; | ||||||
|   includeLargeDiagrams = true, |   } | ||||||
| }: MermaidBuildOptions): BuildOptions => { |   if (format === 'esm') { | ||||||
|  |     fileName += '.esm'; | ||||||
|  |   } | ||||||
|  |   if (!includeLargeDiagrams) { | ||||||
|  |     fileName += '.tiny'; | ||||||
|  |   } | ||||||
|  |   if (minify) { | ||||||
|  |     fileName += '.min'; | ||||||
|  |   } | ||||||
|  |   return fileName; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | export const getBuildConfig = (options: MermaidBuildOptions): BuildOptions => { | ||||||
|  |   const { core, entryName, metafile, format, includeLargeDiagrams } = options; | ||||||
|   const external: string[] = ['require', 'fs', 'path']; |   const external: string[] = ['require', 'fs', 'path']; | ||||||
|   const { name, file, packageName } = packageOptions[entryName]; |   const { name, file, packageName } = packageOptions[entryName]; | ||||||
|  |   const outFileName = getFileName(name, options); | ||||||
|   let output: BuildOptions = buildOptions({ |   let output: BuildOptions = buildOptions({ | ||||||
|     absWorkingDir: resolve(__dirname, `../packages/${packageName}`), |     absWorkingDir: resolve(__dirname, `../packages/${packageName}`), | ||||||
|     entryPoints: { |     entryPoints: { | ||||||
|       [`${name}${core ? '.core' : format === 'iife' ? '' : '.esm'}${ |       [outFileName]: `src/${file}`, | ||||||
|         includeLargeDiagrams ? '' : '.tiny' |  | ||||||
|       }${minify ? '.min' : ''}`]: `src/${file}`, |  | ||||||
|     }, |     }, | ||||||
|     metafile, |     metafile, | ||||||
|     logLevel: 'info', |     logLevel: 'info', | ||||||
|  |     chunkNames: `chunks/${outFileName}/[name]-[hash]`, | ||||||
|     define: { |     define: { | ||||||
|       // This needs to be stringified for esbuild |       // This needs to be stringified for esbuild | ||||||
|       includeLargeDiagrams: `${includeLargeDiagrams}`, |       includeLargeDiagrams: `${includeLargeDiagrams}`, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Sidharth Vinod
					Sidharth Vinod