mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-28 07:03:17 +08:00
48a899f7a9
When converting a `.jison` file into a CommonJS module, jison by default adds a main() function that calls `require("fs");` Even though the main function is never used in the browser, because `fs` is a Node.JS only module, this causes some esbuild issues. To disable this, we can just set an empty main to the jison generator.
71 lines
1.8 KiB
JavaScript
71 lines
1.8 KiB
JavaScript
const { Generator } = require('jison');
|
|
const fs = require('fs');
|
|
|
|
/** @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'],
|
|
entryPoints: ['src/mermaid.ts'],
|
|
outfile: 'dist/mermaid.min.js',
|
|
plugins: [jisonPlugin],
|
|
sourcemap: 'external',
|
|
...override,
|
|
};
|
|
};
|
|
|
|
/**
|
|
* @param {Options} override
|
|
* @returns {Options}
|
|
*/
|
|
exports.esmBuild = (override = { minify: true }) => {
|
|
return buildOptions({
|
|
format: 'esm',
|
|
outfile: `dist/mermaid.esm${override.minify ? '.min' : ''}.mjs`,
|
|
...override,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* @param {Options & { core?: boolean }} override
|
|
* @returns {Options}
|
|
*/
|
|
exports.iifeBuild = (override = { minify: true, core: false }) => {
|
|
const core = override.core;
|
|
if (core && override.minify) {
|
|
throw new Error('Cannot minify core build');
|
|
}
|
|
delete override.core;
|
|
return buildOptions({
|
|
outfile: `dist/mermaid${override.minify ? '.min' : core ? '.core' : ''}.js`,
|
|
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 = new Generator(source, { 'token-stack': true }).generate({
|
|
moduleMain: '() => {}', // disable moduleMain (default one requires Node.JS modules)
|
|
});
|
|
return { contents, warnings: [] };
|
|
});
|
|
},
|
|
};
|