2022-11-20 12:13:00 +05:30
|
|
|
import { build, InlineConfig, type PluginOption } from 'vite';
|
2022-09-22 15:35:22 +05:30
|
|
|
import { resolve } from 'path';
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import jisonPlugin from './jisonPlugin.js';
|
2022-12-22 01:19:02 +00:00
|
|
|
import jsonSchemaPlugin from './jsonSchemaPlugin.js';
|
2023-03-11 14:54:21 +01:00
|
|
|
import typescript from '@rollup/plugin-typescript';
|
2022-11-20 12:13:00 +05:30
|
|
|
import { visualizer } from 'rollup-plugin-visualizer';
|
2022-11-20 14:16:22 +05:30
|
|
|
import type { TemplateType } from 'rollup-plugin-visualizer/dist/plugin/template-types.js';
|
2023-06-16 20:25:40 +05:30
|
|
|
import istanbul from 'vite-plugin-istanbul';
|
2023-08-13 18:33:41 +05:30
|
|
|
import { packageOptions } from '../.build/common.js';
|
2023-08-20 17:00:45 +03:00
|
|
|
import { generateLangium } from '../.build/generateLangium.js';
|
2022-09-23 12:31:24 +05:30
|
|
|
|
2022-11-20 14:16:22 +05:30
|
|
|
const visualize = process.argv.includes('--visualize');
|
2022-09-22 15:35:22 +05:30
|
|
|
const watch = process.argv.includes('--watch');
|
2022-11-10 13:51:53 +05:30
|
|
|
const mermaidOnly = process.argv.includes('--mermaid');
|
2023-06-16 20:25:40 +05:30
|
|
|
const coverage = process.env.VITE_COVERAGE === 'true';
|
2022-09-22 15:35:22 +05:30
|
|
|
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
2023-04-23 15:59:27 +05:30
|
|
|
const sourcemap = false;
|
2022-09-22 15:35:22 +05:30
|
|
|
|
2022-09-23 12:31:24 +05:30
|
|
|
type OutputOptions = Exclude<
|
|
|
|
Exclude<InlineConfig['build'], undefined>['rollupOptions'],
|
|
|
|
undefined
|
|
|
|
>['output'];
|
|
|
|
|
2022-11-20 17:16:09 +05:30
|
|
|
const visualizerOptions = (packageName: string, core = false): PluginOption[] => {
|
2022-11-20 14:16:22 +05:30
|
|
|
if (packageName !== 'mermaid' || !visualize) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-02-09 11:28:27 +05:30
|
|
|
return ['network', 'treemap', 'sunburst'].map(
|
|
|
|
(chartType) =>
|
|
|
|
visualizer({
|
|
|
|
filename: `./stats/${chartType}${core ? '.core' : ''}.html`,
|
|
|
|
template: chartType as TemplateType,
|
|
|
|
gzipSize: true,
|
|
|
|
brotliSize: true,
|
|
|
|
}) as PluginOption
|
2022-11-20 14:16:22 +05:30
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-09-22 15:35:22 +05:30
|
|
|
interface BuildOptions {
|
|
|
|
minify: boolean | 'esbuild';
|
|
|
|
core?: boolean;
|
|
|
|
watch?: boolean;
|
2022-09-26 08:01:23 +02:00
|
|
|
entryName: keyof typeof packageOptions;
|
2022-09-22 15:35:22 +05:30
|
|
|
}
|
|
|
|
|
2022-09-26 08:01:23 +02:00
|
|
|
export const getBuildConfig = ({ minify, core, watch, entryName }: BuildOptions): InlineConfig => {
|
2022-11-21 02:06:44 +05:30
|
|
|
const external: (string | RegExp)[] = ['require', 'fs', 'path'];
|
2022-09-26 08:01:23 +02:00
|
|
|
console.log(entryName, packageOptions[entryName]);
|
|
|
|
const { name, file, packageName } = packageOptions[entryName];
|
2022-09-23 12:31:24 +05:30
|
|
|
let output: OutputOptions = [
|
2022-09-22 15:35:22 +05:30
|
|
|
{
|
2022-09-23 12:31:24 +05:30
|
|
|
name,
|
2022-09-22 15:35:22 +05:30
|
|
|
format: 'esm',
|
2023-04-23 15:59:27 +05:30
|
|
|
sourcemap,
|
2022-09-23 17:39:08 +05:30
|
|
|
entryFileNames: `${name}.esm${minify ? '.min' : ''}.mjs`,
|
2022-09-22 15:35:22 +05:30
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const config: InlineConfig = {
|
|
|
|
configFile: false,
|
|
|
|
build: {
|
|
|
|
emptyOutDir: false,
|
2022-09-23 12:31:24 +05:30
|
|
|
outDir: resolve(__dirname, `../packages/${packageName}/dist`),
|
2022-09-22 15:35:22 +05:30
|
|
|
lib: {
|
2022-09-23 12:31:24 +05:30
|
|
|
entry: resolve(__dirname, `../packages/${packageName}/src/${file}`),
|
|
|
|
name,
|
2022-09-22 15:35:22 +05:30
|
|
|
// the proper extensions will be added
|
2022-09-23 12:31:24 +05:30
|
|
|
fileName: name,
|
2022-09-22 15:35:22 +05:30
|
|
|
},
|
|
|
|
minify,
|
|
|
|
rollupOptions: {
|
|
|
|
external,
|
|
|
|
output,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
resolve: {
|
2023-03-11 14:54:21 +01:00
|
|
|
extensions: [],
|
2022-09-22 15:35:22 +05:30
|
|
|
},
|
2023-03-11 14:54:21 +01:00
|
|
|
plugins: [
|
|
|
|
jisonPlugin(),
|
2022-12-22 01:19:02 +00:00
|
|
|
jsonSchemaPlugin(), // handles `.schema.yaml` files
|
2023-03-11 15:27:16 +01:00
|
|
|
// @ts-expect-error According to the type definitions, rollup plugins are incompatible with vite
|
2023-03-11 14:54:21 +01:00
|
|
|
typescript({ compilerOptions: { declaration: false } }),
|
2023-06-16 20:25:40 +05:30
|
|
|
istanbul({
|
2023-08-20 17:00:45 +03:00
|
|
|
exclude: ['node_modules', 'test/', '__mocks__', 'generated'],
|
2023-06-16 20:25:40 +05:30
|
|
|
extension: ['.js', '.ts'],
|
|
|
|
requireEnv: true,
|
|
|
|
forceBuildInstrument: coverage,
|
|
|
|
}),
|
2023-03-11 14:54:21 +01:00
|
|
|
...visualizerOptions(packageName, core),
|
|
|
|
],
|
2022-09-22 15:35:22 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
if (watch && config.build) {
|
|
|
|
config.build.watch = {
|
2023-02-08 17:42:58 +01:00
|
|
|
include: ['packages/mermaid-example-diagram/src/**', 'packages/mermaid/src/**'],
|
2022-09-22 15:35:22 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
};
|
|
|
|
|
2022-09-26 08:01:23 +02:00
|
|
|
const buildPackage = async (entryName: keyof typeof packageOptions) => {
|
2023-02-16 15:09:22 +05:30
|
|
|
await build(getBuildConfig({ minify: false, entryName }));
|
2022-09-23 12:31:24 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
const main = async () => {
|
|
|
|
const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[];
|
2023-08-20 17:00:45 +03:00
|
|
|
for (const pkg of packageNames.filter(
|
|
|
|
(pkg) => !mermaidOnly || pkg === 'mermaid' || pkg === 'parser'
|
|
|
|
)) {
|
2022-09-23 12:31:24 +05:30
|
|
|
await buildPackage(pkg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-22 13:38:23 +03:00
|
|
|
await generateLangium();
|
2023-08-20 17:00:45 +03:00
|
|
|
|
2022-09-22 15:35:22 +05:30
|
|
|
if (watch) {
|
2023-08-20 17:00:45 +03:00
|
|
|
await build(getBuildConfig({ minify: false, watch, core: false, entryName: 'parser' }));
|
2022-12-11 14:04:41 -08:00
|
|
|
build(getBuildConfig({ minify: false, watch, core: false, entryName: 'mermaid' }));
|
2022-11-10 13:51:53 +05:30
|
|
|
if (!mermaidOnly) {
|
2023-02-08 17:42:58 +01:00
|
|
|
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-example-diagram' }));
|
2023-04-23 00:34:59 +05:30
|
|
|
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-zenuml' }));
|
2022-11-10 13:51:53 +05:30
|
|
|
}
|
2022-11-20 17:16:09 +05:30
|
|
|
} else if (visualize) {
|
2023-08-20 17:00:45 +03:00
|
|
|
await build(getBuildConfig({ minify: false, watch, core: false, entryName: 'parser' }));
|
2022-11-20 17:16:09 +05:30
|
|
|
await build(getBuildConfig({ minify: false, core: true, entryName: 'mermaid' }));
|
|
|
|
await build(getBuildConfig({ minify: false, core: false, entryName: 'mermaid' }));
|
2022-09-22 15:35:22 +05:30
|
|
|
} else {
|
2022-09-23 12:31:24 +05:30
|
|
|
void main();
|
2022-09-22 15:35:22 +05:30
|
|
|
}
|