mermaid/.esbuild/util.cjs

68 lines
1.7 KiB
JavaScript
Raw Normal View History

const { Generator } = require('jison');
const fs = require('fs');
2022-09-01 10:08:02 +02:00
/** @typedef {import('esbuild').BuildOptions} Options */
/**
* @param {Options} override
* @returns {Options}
*/
const buildOptions = (override = {}) => {
return {
bundle: true,
minify: true,
keepNames: true,
2022-09-02 07:38:14 +02:00
banner: { js: '"use strict";' },
2022-09-01 10:08:02 +02:00
globalName: 'mermaid',
platform: 'browser',
2022-09-01 17:11:31 +02:00
tsconfig: 'tsconfig.json',
resolveExtensions: ['.ts', '.js', '.json', '.jison'],
2022-09-01 10:08:02 +02:00
external: ['require', 'fs', 'path'],
2022-09-01 17:11:31 +02:00
entryPoints: ['src/mermaid.ts'],
2022-09-01 10:08:02 +02:00
outfile: 'dist/mermaid.min.js',
plugins: [jisonPlugin],
sourcemap: 'external',
...override,
};
};
2022-09-01 21:04:03 +02:00
/**
* @param {Options} override
* @returns {Options}
*/
exports.esmBuild = (override = { minify: true }) => {
2022-09-01 10:08:02 +02:00
return buildOptions({
format: 'esm',
2022-09-01 21:04:03 +02:00
outfile: `dist/mermaid.esm${override.minify ? '.min' : ''}.mjs`,
...override,
2022-09-01 10:08:02 +02:00
});
};
2022-09-01 21:04:03 +02:00
/**
2022-09-09 14:02:13 +02:00
* @param {Options & { core?: boolean }} override
2022-09-01 21:04:03 +02:00
* @returns {Options}
*/
2022-09-09 14:18:09 +02:00
exports.iifeBuild = (override = { minify: true, core: false }) => {
2022-09-09 14:02:13 +02:00
const core = override.core;
if (core && override.minify) {
throw new Error('Cannot minify core build');
}
delete override.core;
2022-09-01 21:04:03 +02:00
return buildOptions({
2022-09-09 14:02:13 +02:00
outfile: `dist/mermaid${override.minify ? '.min' : core ? '.core' : ''}.js`,
2022-09-01 21:04:03 +02:00
...override,
});
2022-09-01 10:08:02 +02:00
};
const jisonPlugin = {
name: 'jison',
setup(build) {
build.onLoad({ filter: /\.jison$/ }, async (args) => {
// Load the file from the file system
const source = await fs.promises.readFile(args.path, 'utf8');
const contents = new Generator(source, { 'token-stack': true }).generate();
return { contents, warnings: [] };
2022-09-01 10:08:02 +02:00
});
},
};