mermaid/.vite/build.ts

155 lines
4.4 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';
2022-11-14 10:21:23 +01:00
import { readFileSync } from 'fs';
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';
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');
2022-09-22 12:05:22 +02:00
const __dirname = fileURLToPath(new URL('.', import.meta.url));
2022-09-23 09:01:24 +02:00
type OutputOptions = Exclude<
Exclude<InlineConfig['build'], undefined>['rollupOptions'],
undefined
>['output'];
2022-11-20 09:46:22 +01:00
const visualizerOptions = (packageName: string): PluginOption[] => {
if (packageName !== 'mermaid' || !visualize) {
return [];
}
return ['network', 'treemap', 'sunburst'].map((chartType) =>
visualizer({
filename: `./stats/${chartType}.html`,
template: chartType as TemplateType,
gzipSize: true,
brotliSize: true,
})
);
};
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-mindmap': {
name: 'mermaid-mindmap',
packageName: 'mermaid-mindmap',
2022-10-07 10:40:01 +02:00
file: 'detector.ts',
},
2022-11-10 09:21:53 +01:00
// 'mermaid-example-diagram-detector': {
// name: 'mermaid-example-diagram-detector',
// packageName: 'mermaid-example-diagram',
// 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-09-22 12:05:22 +02:00
const external = ['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',
sourcemap: true,
2022-09-23 14:09:08 +02:00
entryFileNames: `${name}.esm${minify ? '.min' : ''}.mjs`,
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: 'umd',
sourcemap: true,
2022-09-23 14:09:08 +02: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.
2022-09-22 12:05:22 +02:00
external.push(...Object.keys(dependencies));
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',
sourcemap: true,
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: ['.jison', '.js', '.ts', '.json'],
},
2022-11-20 09:46:22 +01:00
plugins: [jisonPlugin(), ...visualizerOptions(packageName)],
2022-09-22 12:05:22 +02:00
};
if (watch && config.build) {
config.build.watch = {
include: [
'packages/mermaid-mindmap/src/**',
'packages/mermaid/src/**',
2022-11-10 09:21:53 +01:00
// 'packages/mermaid-example-diagram/src/**',
],
2022-09-22 12:05:22 +02:00
};
}
return config;
};
const buildPackage = async (entryName: keyof typeof packageOptions) => {
2022-09-23 09:01:24 +02:00
return Promise.allSettled([
build(getBuildConfig({ minify: false, entryName })),
build(getBuildConfig({ minify: 'esbuild', entryName })),
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-11-10 09:21:53 +01:00
build(getBuildConfig({ minify: false, watch, core: true, entryName: 'mermaid' }));
if (!mermaidOnly) {
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-mindmap' }));
// build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-example-diagram' }));
}
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
}