mermaid/.vite/build.ts

162 lines
4.9 KiB
TypeScript
Raw Normal View History

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-11-14 14:51:23 +05:30
import { readFileSync } from 'fs';
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';
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');
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-23 12:31:24 +05:30
const packageOptions = {
mermaid: {
name: 'mermaid',
packageName: 'mermaid',
2022-09-23 12:31:24 +05:30
file: 'mermaid.ts',
},
'mermaid-example-diagram': {
name: 'mermaid-example-diagram',
packageName: 'mermaid-example-diagram',
2022-10-07 10:40:01 +02:00
file: 'detector.ts',
},
2023-04-23 00:34:59 +05:30
'mermaid-zenuml': {
name: 'mermaid-zenuml',
packageName: 'mermaid-zenuml',
file: 'detector.ts',
},
2022-09-23 12:31:24 +05:30
};
2022-09-22 15:35:22 +05:30
interface BuildOptions {
minify: boolean | 'esbuild';
core?: boolean;
watch?: boolean;
entryName: keyof typeof packageOptions;
2022-09-22 15:35:22 +05:30
}
export const getBuildConfig = ({ minify, core, watch, entryName }: BuildOptions): InlineConfig => {
2022-11-21 02:06:44 +05:30
const external: (string | RegExp)[] = ['require', 'fs', 'path'];
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
},
2023-03-07 09:38:51 +05:30
{
name,
format: 'umd',
2023-04-23 23:40:24 +05:30
sourcemap,
2023-03-07 09:38:51 +05:30
entryFileNames: `${name}${minify ? '.min' : ''}.js`,
},
2022-09-22 15:35:22 +05:30
];
if (core) {
2022-11-14 14:51:23 +05:30
const { dependencies } = JSON.parse(
readFileSync(resolve(__dirname, `../packages/${packageName}/package.json`), 'utf-8')
);
2022-09-23 17:20:40 +05:30
// Core build is used to generate file without bundled dependencies.
// This is used by downstream projects to bundle dependencies themselves.
// Ignore dependencies and any dependencies of dependencies
// Adapted from the RegEx used by `rollup-plugin-node`
2022-11-21 02:06:44 +05:30
external.push(new RegExp('^(?:' + Object.keys(dependencies).join('|') + ')(?:/.+)?$'));
2022-09-23 17:20:40 +05:30
// This needs to be an array. Otherwise vite will build esm & umd with same name and overwrite esm with umd.
output = [
{
2022-09-23 17:39:08 +05:30
name,
2022-09-23 17:20:40 +05:30
format: 'esm',
2023-04-23 15:59:27 +05:30
sourcemap,
2022-09-23 17:39:08 +05:30
entryFileNames: `${name}.core.mjs`,
2022-09-23 17:20:40 +05:30
},
];
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: {
extensions: [],
2022-09-22 15:35:22 +05:30
},
plugins: [
jisonPlugin(),
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 } }),
...visualizerOptions(packageName, core),
],
2022-09-22 15:35:22 +05:30
};
if (watch && config.build) {
config.build.watch = {
include: ['packages/mermaid-example-diagram/src/**', 'packages/mermaid/src/**'],
2022-09-22 15:35:22 +05:30
};
}
return config;
};
const buildPackage = async (entryName: keyof typeof packageOptions) => {
2023-02-16 15:09:22 +05:30
await build(getBuildConfig({ minify: false, entryName }));
await build(getBuildConfig({ minify: 'esbuild', entryName }));
await build(getBuildConfig({ minify: false, core: true, entryName }));
2022-09-23 12:31:24 +05:30
};
const main = async () => {
const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[];
2022-11-20 14:16:22 +05:30
for (const pkg of packageNames.filter((pkg) => !mermaidOnly || pkg === 'mermaid')) {
2022-09-23 12:31:24 +05:30
await buildPackage(pkg);
}
};
2022-09-22 15:35:22 +05:30
if (watch) {
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) {
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) {
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
}