2022-09-19 12:49:24 +05:30
|
|
|
const { transformJison } = require('./jisonTransformer');
|
2022-09-06 20:23:46 +05:30
|
|
|
const fs = require('fs');
|
2022-09-11 20:55:03 +01:00
|
|
|
const { dependencies } = require('../package.json');
|
2022-09-06 20:23:46 +05:30
|
|
|
|
2022-09-01 13:38:02 +05:30
|
|
|
/** @typedef {import('esbuild').BuildOptions} Options */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Options} override
|
|
|
|
* @returns {Options}
|
|
|
|
*/
|
|
|
|
const buildOptions = (override = {}) => {
|
|
|
|
return {
|
|
|
|
bundle: true,
|
|
|
|
minify: true,
|
|
|
|
keepNames: true,
|
2022-09-02 11:08:14 +05:30
|
|
|
banner: { js: '"use strict";' },
|
2022-09-01 13:38:02 +05:30
|
|
|
globalName: 'mermaid',
|
|
|
|
platform: 'browser',
|
2022-09-01 20:41:31 +05:30
|
|
|
tsconfig: 'tsconfig.json',
|
|
|
|
resolveExtensions: ['.ts', '.js', '.json', '.jison'],
|
2022-09-01 13:38:02 +05:30
|
|
|
external: ['require', 'fs', 'path'],
|
2022-09-13 11:25:14 +05:30
|
|
|
outdir: 'dist',
|
2022-09-01 13:38:02 +05:30
|
|
|
plugins: [jisonPlugin],
|
|
|
|
sourcemap: 'external',
|
|
|
|
...override,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-09-13 11:25:14 +05:30
|
|
|
const getOutFiles = (extension) => {
|
|
|
|
return {
|
|
|
|
[`mermaid${extension}`]: 'src/mermaid.ts',
|
|
|
|
[`diagramAPI${extension}`]: 'src/diagram-api/diagramAPI.ts',
|
|
|
|
};
|
|
|
|
};
|
2022-09-02 00:34:03 +05:30
|
|
|
/**
|
2022-09-11 20:55:03 +01:00
|
|
|
* Build options for mermaid.esm.* build.
|
|
|
|
*
|
|
|
|
* For ESM browser use.
|
|
|
|
*
|
|
|
|
* @param {Options} override - Override options.
|
|
|
|
* @returns {Options} ESBuild build options.
|
2022-09-02 00:34:03 +05:30
|
|
|
*/
|
|
|
|
exports.esmBuild = (override = { minify: true }) => {
|
2022-09-01 13:38:02 +05:30
|
|
|
return buildOptions({
|
|
|
|
format: 'esm',
|
2022-09-13 11:25:14 +05:30
|
|
|
entryPoints: getOutFiles(`.esm${override.minify ? '.min' : ''}`),
|
|
|
|
outExtension: { '.js': '.mjs' },
|
2022-09-02 00:34:03 +05:30
|
|
|
...override,
|
2022-09-01 13:38:02 +05:30
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-09-02 00:34:03 +05:30
|
|
|
/**
|
2022-09-11 20:55:03 +01:00
|
|
|
* Build options for mermaid.core.* build.
|
|
|
|
*
|
2022-09-13 11:25:14 +05:30
|
|
|
* 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.
|
2022-09-11 20:55:03 +01:00
|
|
|
*
|
|
|
|
* @param {Options} override - Override options.
|
|
|
|
* @returns {Options} ESBuild build options.
|
|
|
|
*/
|
|
|
|
exports.esmCoreBuild = (override) => {
|
|
|
|
return buildOptions({
|
|
|
|
format: 'esm',
|
2022-09-13 11:25:14 +05:30
|
|
|
entryPoints: getOutFiles(`.core`),
|
|
|
|
outExtension: { '.js': '.mjs' },
|
2022-09-11 20:55:03 +01:00
|
|
|
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-02 00:34:03 +05:30
|
|
|
*/
|
2022-09-11 20:55:03 +01:00
|
|
|
exports.iifeBuild = (override = { minify: true }) => {
|
2022-09-02 00:34:03 +05:30
|
|
|
return buildOptions({
|
2022-09-13 11:25:14 +05:30
|
|
|
entryPoints: getOutFiles(override.minify ? '.min' : ''),
|
2022-09-09 17:49:11 +05:30
|
|
|
format: 'iife',
|
2022-09-02 00:34:03 +05:30
|
|
|
...override,
|
|
|
|
});
|
2022-09-01 13:38:02 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
const jisonPlugin = {
|
|
|
|
name: 'jison',
|
|
|
|
setup(build) {
|
|
|
|
build.onLoad({ filter: /\.jison$/ }, async (args) => {
|
|
|
|
// Load the file from the file system
|
2022-09-06 20:23:46 +05:30
|
|
|
const source = await fs.promises.readFile(args.path, 'utf8');
|
2022-09-19 12:49:24 +05:30
|
|
|
const contents = transformJison(source);
|
2022-09-06 20:23:46 +05:30
|
|
|
return { contents, warnings: [] };
|
2022-09-01 13:38:02 +05:30
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|