build: convert core build to unbundled ESM

The `mermaid.core.js` build was previously a UMD build that did not
have `node_modules` bundled.

This was designed for users to add `mermaid` to their own apps,
then bundle with Webpack/ESBuild.
Hence the bundle test in `cypress/platform/bundle-test.js`.

As ESBuild does not support UMD, I've switched the `mermaid.core.js`
to instead use ESM, as Mermaid now requires ESM
(due to d3 requiring ESM). All modern bundlers also support ESM.
This commit is contained in:
Alois Klink 2022-09-11 20:55:03 +01:00
parent 48a899f7a9
commit 37aaca0090
No known key found for this signature in database
GPG Key ID: 4482AA96E51ADF71
2 changed files with 37 additions and 15 deletions

View File

@ -1,4 +1,4 @@
const { esmBuild, iifeBuild } = require('./util.cjs');
const { esmBuild, esmCoreBuild, iifeBuild } = require('./util.cjs');
const { build } = require('esbuild');
const handler = (e) => {
@ -12,10 +12,9 @@ build(iifeBuild({ minify: false, watch })).catch(handler);
// mermaid.esm.mjs
build(esmBuild({ minify: false, watch })).catch(handler);
// mermaid.core.js
build(iifeBuild({ minify: false, core: true })).catch(handler);
// mermaid.min.js
build(esmBuild()).catch(handler);
// mermaid.esm.min.mjs
build(iifeBuild()).catch(handler);
// mermaid.core.mjs (node_modules unbundled)
build(esmCoreBuild()).catch(handler);

View File

@ -1,5 +1,6 @@
const { Generator } = require('jison');
const fs = require('fs');
const { dependencies } = require('../package.json');
/** @typedef {import('esbuild').BuildOptions} Options */
@ -27,8 +28,12 @@ const buildOptions = (override = {}) => {
};
/**
* @param {Options} override
* @returns {Options}
* 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({
@ -39,17 +44,35 @@ exports.esmBuild = (override = { minify: true }) => {
};
/**
* @param {Options & { core?: boolean }} override
* @returns {Options}
* Build options for mermaid.core.* build.
*
* This build does not bundle `./node_modules/`, as it is designed to be used
* with Webpack/ESBuild to merge webpack into a website.
*
* @param {Options} override - Override options.
* @returns {Options} ESBuild build 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;
exports.esmCoreBuild = (override) => {
return buildOptions({
outfile: `dist/mermaid${override.minify ? '.min' : core ? '.core' : ''}.js`,
format: 'esm',
outfile: `dist/mermaid.core.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({
outfile: `dist/mermaid${override.minify ? '.min' : ''}.js`,
format: 'iife',
...override,
});