4.4 KB139 lines
Blame
1import type { InlineConfig } from 'vite';
2import { build, type PluginOption } from 'vite';
3import { resolve } from 'path';
4import { fileURLToPath } from 'url';
5import jisonPlugin from './jisonPlugin.js';
6import jsonSchemaPlugin from './jsonSchemaPlugin.js';
7import typescript from '@rollup/plugin-typescript';
8import { visualizer } from 'rollup-plugin-visualizer';
9import type { TemplateType } from 'rollup-plugin-visualizer/dist/plugin/template-types.js';
10import istanbul from 'vite-plugin-istanbul';
11import { packageOptions } from '../.build/common.js';
12import { generateLangium } from '../.build/generateLangium.js';
13
14const visualize = process.argv.includes('--visualize');
15const watch = process.argv.includes('--watch');
16const mermaidOnly = process.argv.includes('--mermaid');
17const coverage = process.env.VITE_COVERAGE === 'true';
18const __dirname = fileURLToPath(new URL('.', import.meta.url));
19const sourcemap = false;
20
21type OutputOptions = Exclude<
22 Exclude<InlineConfig['build'], undefined>['rollupOptions'],
23 undefined
24>['output'];
25
26const visualizerOptions = (packageName: string, core = false): PluginOption[] => {
27 if (packageName !== 'mermaid' || !visualize) {
28 return [];
29 }
30 return ['network', 'treemap', 'sunburst'].map(
31 (chartType) =>
32 visualizer({
33 filename: `./stats/${chartType}${core ? '.core' : ''}.html`,
34 template: chartType as TemplateType,
35 gzipSize: true,
36 brotliSize: true,
37 }) as PluginOption
38 );
39};
40
41interface BuildOptions {
42 minify: boolean | 'esbuild';
43 core?: boolean;
44 watch?: boolean;
45 entryName: keyof typeof packageOptions;
46}
47
48export const getBuildConfig = ({ minify, core, watch, entryName }: BuildOptions): InlineConfig => {
49 const external: (string | RegExp)[] = ['require', 'fs', 'path'];
50 // eslint-disable-next-line no-console
51 console.log(entryName, packageOptions[entryName]);
52 const { name, file, packageName } = packageOptions[entryName];
53 const output: OutputOptions = [
54 {
55 name,
56 format: 'esm',
57 sourcemap,
58 entryFileNames: `${name}.esm${minify ? '.min' : ''}.mjs`,
59 },
60 ];
61
62 const config: InlineConfig = {
63 configFile: false,
64 build: {
65 emptyOutDir: false,
66 outDir: resolve(__dirname, `../packages/${packageName}/dist`),
67 lib: {
68 entry: resolve(__dirname, `../packages/${packageName}/src/${file}`),
69 name,
70 // the proper extensions will be added
71 fileName: name,
72 },
73 minify,
74 rollupOptions: {
75 external,
76 output,
77 },
78 },
79 define: {
80 'import.meta.vitest': 'undefined',
81 'injected.includeLargeFeatures': 'true',
82 'injected.version': `'0.0.0'`,
83 },
84 resolve: {
85 extensions: [],
86 },
87 plugins: [
88 jisonPlugin(),
89 jsonSchemaPlugin(), // handles `.schema.yaml` files
90 typescript({ compilerOptions: { declaration: false } }),
91 istanbul({
92 exclude: ['node_modules', 'test/', '__mocks__', 'generated'],
93 extension: ['.js', '.ts'],
94 requireEnv: true,
95 forceBuildInstrument: coverage,
96 }),
97 ...visualizerOptions(packageName, core),
98 ],
99 };
100
101 if (watch && config.build) {
102 config.build.watch = {
103 include: ['packages/mermaid-example-diagram/src/**', 'packages/mermaid/src/**'],
104 };
105 }
106
107 return config;
108};
109
110const buildPackage = async (entryName: keyof typeof packageOptions) => {
111 await build(getBuildConfig({ minify: false, entryName }));
112};
113
114const main = async () => {
115 const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[];
116 for (const pkg of packageNames.filter(
117 (pkg) => !mermaidOnly || pkg === 'mermaid' || pkg === 'parser'
118 )) {
119 await buildPackage(pkg);
120 }
121};
122
123await generateLangium();
124
125if (watch) {
126 await build(getBuildConfig({ minify: false, watch, core: false, entryName: 'parser' }));
127 void build(getBuildConfig({ minify: false, watch, core: false, entryName: 'mermaid' }));
128 if (!mermaidOnly) {
129 void build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-example-diagram' }));
130 void build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-zenuml' }));
131 }
132} else if (visualize) {
133 await build(getBuildConfig({ minify: false, watch, core: false, entryName: 'parser' }));
134 await build(getBuildConfig({ minify: false, core: true, entryName: 'mermaid' }));
135 await build(getBuildConfig({ minify: false, core: false, entryName: 'mermaid' }));
136} else {
137 void main();
138}
139