mermaid/.vite/build.ts

128 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-09-22 12:05:22 +02:00
import { build, InlineConfig } from 'vite';
import { resolve } from 'path';
import { fileURLToPath } from 'url';
import jisonPlugin from './jisonPlugin.js';
import pkg from '../package.json' assert { type: 'json' };
2022-09-23 09:01:24 +02:00
2022-09-22 12:05:22 +02:00
const { dependencies } = pkg;
const watch = process.argv.includes('--watch');
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'];
const packageOptions = {
mermaid: {
name: 'mermaid',
file: 'mermaid.ts',
},
'mermaid-mindmap': {
name: 'mermaid-mindmap',
2022-09-23 16:55:30 +02:00
file: 'diagram.ts',
},
'mermaid-mindmap-detector': {
name: 'mermaid-mindmap-detector',
2022-09-23 09:01:24 +02:00
file: 'registry.ts',
},
};
2022-09-22 12:05:22 +02:00
interface BuildOptions {
minify: boolean | 'esbuild';
core?: boolean;
watch?: boolean;
2022-09-23 09:01:24 +02:00
packageName: keyof typeof packageOptions;
2022-09-22 12:05:22 +02:00
}
2022-09-23 09:01:24 +02:00
export const getBuildConfig = ({
minify,
core,
watch,
packageName,
}: BuildOptions): InlineConfig => {
2022-09-22 12:05:22 +02:00
const external = ['require', 'fs', 'path'];
2022-09-23 09:01:24 +02:00
const { name, file } = packageOptions[packageName];
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-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'],
},
plugins: [jisonPlugin()],
};
if (watch && config.build) {
config.build.watch = {
include: 'src/**',
};
}
return config;
};
2022-09-23 09:01:24 +02:00
const buildPackage = async (packageName: keyof typeof packageOptions) => {
return Promise.allSettled([
build(getBuildConfig({ minify: false, packageName })),
build(getBuildConfig({ minify: 'esbuild', packageName })),
build(getBuildConfig({ minify: false, core: true, packageName })),
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) {
await buildPackage(pkg);
}
};
2022-09-22 12:05:22 +02:00
if (watch) {
2022-09-23 09:01:24 +02:00
build(getBuildConfig({ minify: false, watch, packageName: '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
}