mermaid/.vite/build.ts

172 lines
5.3 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';
2022-11-14 10:21:23 +01:00
import { readFileSync } from 'fs';
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';
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-23 09:01:24 +02:00
const packageOptions = {
mermaid: {
name: 'mermaid',
packageName: 'mermaid',
2022-09-23 09:01:24 +02:00
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-22 21:04:59 +02:00
'mermaid-zenuml': {
name: 'mermaid-zenuml',
packageName: 'mermaid-zenuml',
file: 'detector.ts',
},
2022-09-23 09:01:24 +02: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
},
2023-03-07 05:08:51 +01:00
{
name,
format: 'umd',
2023-04-23 20:10:24 +02:00
sourcemap,
2023-03-07 05:08:51 +01:00
entryFileNames: `${name}${minify ? '.min' : ''}.js`,
},
2022-09-22 12:05:22 +02:00
];
if (core) {
2022-11-14 10:21:23 +01:00
const { dependencies } = JSON.parse(
readFileSync(resolve(__dirname, `../packages/${packageName}/package.json`), 'utf-8')
);
2022-09-23 13:50:40 +02:00
// 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-20 21:36:44 +01:00
external.push(new RegExp('^(?:' + Object.keys(dependencies).join('|') + ')(?:/.+)?$'));
2022-09-23 13:50:40 +02:00
// This needs to be an array. Otherwise vite will build esm & umd with same name and overwrite esm with umd.
output = [
{
2022-09-23 14:09:08 +02:00
name,
2022-09-23 13:50:40 +02:00
format: 'esm',
2023-04-23 12:29:27 +02:00
sourcemap,
2022-09-23 14:09:08 +02:00
entryFileNames: `${name}.core.mjs`,
2022-09-23 13:50:40 +02:00
},
];
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({
2023-06-17 13:22:03 +02:00
exclude: ['node_modules', 'test/', '__mocks__'],
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 }));
await build(getBuildConfig({ minify: 'esbuild', entryName }));
await build(getBuildConfig({ minify: false, core: true, entryName }));
2022-09-23 09:01:24 +02:00
};
const main = async () => {
const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[];
2022-11-20 09:46:22 +01:00
for (const pkg of packageNames.filter((pkg) => !mermaidOnly || pkg === 'mermaid')) {
2022-09-23 09:01:24 +02:00
await buildPackage(pkg);
}
};
2022-09-22 12:05:22 +02:00
if (watch) {
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, 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
}