mermaid/.esbuild/util.cjs

103 lines
2.7 KiB
JavaScript
Raw Normal View History

const { Generator } = require('jison');
const fs = require('fs');
const { dependencies } = require('../package.json');
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-13 07:55:14 +02:00
outdir: 'dist',
2022-09-01 10:08:02 +02:00
plugins: [jisonPlugin],
sourcemap: 'external',
...override,
};
};
2022-09-13 07:22:51 +02:00
const getEntryPoints = (extension) => {
2022-09-13 07:55:14 +02:00
return {
[`mermaid${extension}`]: 'src/mermaid.ts',
[`diagramAPI${extension}`]: 'src/diagram-api/diagramAPI.ts',
};
};
2022-09-13 07:22:51 +02:00
exports.getEntryPoints = getEntryPoints;
2022-09-01 21:04:03 +02:00
/**
* Build options for mermaid.esm.* build.
*
* For ESM browser use.
*
* @param {Options} override - Override options.
* @returns {Options} ESBuild build options.
2022-09-01 21:04:03 +02:00
*/
exports.esmBuild = (override = { minify: true }) => {
2022-09-01 10:08:02 +02:00
return buildOptions({
format: 'esm',
2022-09-13 07:22:51 +02:00
entryPoints: getEntryPoints(`.esm${override.minify ? '.min' : ''}`),
2022-09-13 07:55:14 +02:00
outExtension: { '.js': '.mjs' },
2022-09-01 21:04:03 +02:00
...override,
2022-09-01 10:08:02 +02:00
});
};
2022-09-01 21:04:03 +02:00
/**
* Build options for mermaid.core.* build.
*
2022-09-13 07:55:14 +02:00
* This build does not bundle `./node_modules/`, as it is designed to be used with
* Webpack/ESBuild/Vite to use mermaid inside an app/website.
*
* @param {Options} override - Override options.
* @returns {Options} ESBuild build options.
*/
exports.esmCoreBuild = (override) => {
return buildOptions({
format: 'esm',
2022-09-13 07:22:51 +02:00
entryPoints: getEntryPoints(`.core`),
2022-09-13 07:55:14 +02:00
outExtension: { '.js': '.mjs' },
external: ['require', 'fs', 'path', ...Object.keys(dependencies)],
platform: 'neutral',
...override,
});
};
/**
* Build options for mermaid.js build.
*
* For IIFE browser use (where ESM is not yet supported).
*
* @param {Options} override - Override options.
* @returns {Options} ESBuild build options.
2022-09-01 21:04:03 +02:00
*/
exports.iifeBuild = (override = { minify: true }) => {
2022-09-01 21:04:03 +02:00
return buildOptions({
2022-09-13 07:22:51 +02:00
entryPoints: getEntryPoints(override.minify ? '.min' : ''),
2022-09-09 14:19:11 +02:00
format: 'iife',
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({
moduleMain: '() => {}', // disable moduleMain (default one requires Node.JS modules)
});
return { contents, warnings: [] };
2022-09-01 10:08:02 +02:00
});
},
};