mermaid/.esbuild/util.cjs

67 lines
1.5 KiB
JavaScript
Raw Normal View History

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,
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
/**
* @param {Options} override
* @returns {Options}
*/
exports.umdBuild = (override = { minify: true }) => {
return buildOptions({
outfile: `dist/mermaid${override.minify ? '.min' : ''}.js`,
...override,
});
2022-09-01 10:08:02 +02:00
};
const jisonPlugin = {
name: 'jison',
setup(build) {
const { Generator } = require('jison');
let fs = require('fs');
build.onLoad({ filter: /\.jison$/ }, async (args) => {
// Load the file from the file system
let source = await fs.promises.readFile(args.path, 'utf8');
try {
let contents = new Generator(source, {}).generate();
return { contents, warnings: [] };
} catch (e) {
return { errors: [] };
}
});
},
};