mermaid/.vite/build.ts

133 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-11-20 07:43:00 +01:00
import { build, InlineConfig, type PluginOption } from 'vite';
2022-09-22 12:05:22 +02:00
import { resolve } from 'path';
import { fileURLToPath } from 'url';
import jisonPlugin from './jisonPlugin.js';
import jsonSchemaPlugin from './jsonSchemaPlugin.js';
import typescript from '@rollup/plugin-typescript';
2022-11-20 07:43:00 +01:00
import { visualizer } from 'rollup-plugin-visualizer';
2022-11-20 09:46:22 +01:00
import type { TemplateType } from 'rollup-plugin-visualizer/dist/plugin/template-types.js';
2023-06-16 16:55:40 +02:00
import istanbul from 'vite-plugin-istanbul';
import { packageOptions } from '../.build/common.js';
import { generateLangium } from '../.build/generateLangium.js';
2022-09-23 09:01:24 +02:00
2022-11-20 09:46:22 +01:00
const visualize = process.argv.includes('--visualize');
2022-09-22 12:05:22 +02:00
const watch = process.argv.includes('--watch');
2022-11-10 09:21:53 +01:00
const mermaidOnly = process.argv.includes('--mermaid');
2023-06-16 16:55:40 +02:00
const coverage = process.env.VITE_COVERAGE === 'true';
2022-09-22 12:05:22 +02:00
const __dirname = fileURLToPath(new URL('.', import.meta.url));
2023-04-23 12:29:27 +02:00
const sourcemap = false;
2022-09-22 12:05:22 +02:00
2022-09-23 09:01:24 +02:00
type OutputOptions = Exclude<
Exclude<InlineConfig['build'], undefined>['rollupOptions'],
undefined
>['output'];
2022-11-20 12:46:09 +01:00
const visualizerOptions = (packageName: string, core = false): PluginOption[] => {
2022-11-20 09:46:22 +01:00
if (packageName !== 'mermaid' || !visualize) {
return [];
}
2023-02-09 06:58:27 +01:00
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 09:46:22 +01:00
);
};
2022-09-22 12:05:22 +02:00
interface BuildOptions {
minify: boolean | 'esbuild';
core?: boolean;
watch?: boolean;
entryName: keyof typeof packageOptions;
2022-09-22 12:05:22 +02:00
}
export const getBuildConfig = ({ minify, core, watch, entryName }: BuildOptions): InlineConfig => {
2022-11-20 21:36:44 +01:00
const external: (string | RegExp)[] = ['require', 'fs', 'path'];
console.log(entryName, packageOptions[entryName]);
const { name, file, packageName } = packageOptions[entryName];
2022-09-23 09:01:24 +02:00
let output: OutputOptions = [
2022-09-22 12:05:22 +02:00
{
2022-09-23 09:01:24 +02:00
name,
2022-09-22 12:05:22 +02:00
format: 'esm',
2023-04-23 12:29:27 +02:00
sourcemap,
2022-09-23 14:09:08 +02:00
entryFileNames: `${name}.esm${minify ? '.min' : ''}.mjs`,
2022-09-22 12:05:22 +02:00
},
];
const config: InlineConfig = {
configFile: false,
build: {
emptyOutDir: false,
2022-09-23 09:01:24 +02:00
outDir: resolve(__dirname, `../packages/${packageName}/dist`),
2022-09-22 12:05:22 +02:00
lib: {
2022-09-23 09:01:24 +02:00
entry: resolve(__dirname, `../packages/${packageName}/src/${file}`),
name,
2022-09-22 12:05:22 +02:00
// the proper extensions will be added
2022-09-23 09:01:24 +02:00
fileName: name,
2022-09-22 12:05:22 +02:00
},
minify,
rollupOptions: {
external,
output,
},
},
resolve: {
extensions: [],
2022-09-22 12:05:22 +02:00
},
plugins: [
jisonPlugin(),
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
typescript({ compilerOptions: { declaration: false } }),
2023-06-16 16:55:40 +02:00
istanbul({
exclude: ['node_modules', 'test/', '__mocks__', 'generated'],
2023-06-16 16:55:40 +02:00
extension: ['.js', '.ts'],
requireEnv: true,
forceBuildInstrument: coverage,
}),
...visualizerOptions(packageName, core),
],
2022-09-22 12:05:22 +02:00
};
if (watch && config.build) {
config.build.watch = {
include: ['packages/mermaid-example-diagram/src/**', 'packages/mermaid/src/**'],
2022-09-22 12:05:22 +02:00
};
}
return config;
};
const buildPackage = async (entryName: keyof typeof packageOptions) => {
2023-02-16 10:39:22 +01:00
await build(getBuildConfig({ minify: false, entryName }));
2022-09-23 09:01:24 +02:00
};
const main = async () => {
const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[];
for (const pkg of packageNames.filter(
(pkg) => !mermaidOnly || pkg === 'mermaid' || pkg === 'parser'
)) {
2022-09-23 09:01:24 +02:00
await buildPackage(pkg);
}
};
await generateLangium();
2022-09-22 12:05:22 +02:00
if (watch) {
await build(getBuildConfig({ minify: false, watch, core: false, entryName: 'parser' }));
2022-12-11 23:04:41 +01:00
build(getBuildConfig({ minify: false, watch, core: false, entryName: 'mermaid' }));
2022-11-10 09:21:53 +01:00
if (!mermaidOnly) {
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-example-diagram' }));
2023-04-22 21:04:59 +02:00
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-zenuml' }));
2022-11-10 09:21:53 +01:00
}
2022-11-20 12:46:09 +01:00
} else if (visualize) {
await build(getBuildConfig({ minify: false, watch, core: false, entryName: 'parser' }));
2022-11-20 12:46:09 +01:00
await build(getBuildConfig({ minify: false, core: true, entryName: 'mermaid' }));
await build(getBuildConfig({ minify: false, core: false, entryName: 'mermaid' }));
2022-09-22 12:05:22 +02:00
} else {
2022-09-23 09:01:24 +02:00
void main();
2022-09-22 12:05:22 +02:00
}