mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-14 06:43:25 +08:00
99 lines
2.5 KiB
JavaScript
99 lines
2.5 KiB
JavaScript
const { transformJison } = require('./jisonTransformer');
|
|
const fs = require('fs');
|
|
const { dependencies } = require('../package.json');
|
|
|
|
/** @typedef {import('esbuild').BuildOptions} Options */
|
|
|
|
/**
|
|
* @param {Options} override
|
|
* @returns {Options}
|
|
*/
|
|
const buildOptions = (override = {}) => {
|
|
return {
|
|
bundle: true,
|
|
minify: true,
|
|
keepNames: true,
|
|
banner: { js: '"use strict";' },
|
|
globalName: 'mermaid',
|
|
platform: 'browser',
|
|
tsconfig: 'tsconfig.json',
|
|
resolveExtensions: ['.ts', '.js', '.json', '.jison'],
|
|
external: ['require', 'fs', 'path'],
|
|
outdir: 'dist',
|
|
plugins: [jisonPlugin],
|
|
sourcemap: 'external',
|
|
...override,
|
|
};
|
|
};
|
|
|
|
const getOutFiles = (extension) => {
|
|
return {
|
|
[`mermaid${extension}`]: 'src/mermaid.ts',
|
|
[`diagramAPI${extension}`]: 'src/diagram-api/diagramAPI.ts',
|
|
};
|
|
};
|
|
/**
|
|
* Build options for mermaid.esm.* build.
|
|
*
|
|
* For ESM browser use.
|
|
*
|
|
* @param {Options} override - Override options.
|
|
* @returns {Options} ESBuild build options.
|
|
*/
|
|
exports.esmBuild = (override = { minify: true }) => {
|
|
return buildOptions({
|
|
format: 'esm',
|
|
entryPoints: getOutFiles(`.esm${override.minify ? '.min' : ''}`),
|
|
outExtension: { '.js': '.mjs' },
|
|
...override,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Build options for mermaid.core.* build.
|
|
*
|
|
* 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',
|
|
entryPoints: getOutFiles(`.core`),
|
|
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.
|
|
*/
|
|
exports.iifeBuild = (override = { minify: true }) => {
|
|
return buildOptions({
|
|
entryPoints: getOutFiles(override.minify ? '.min' : ''),
|
|
format: 'iife',
|
|
...override,
|
|
});
|
|
};
|
|
|
|
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 = transformJison(source);
|
|
return { contents, warnings: [] };
|
|
});
|
|
},
|
|
};
|