-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3386 from mermaid-js/sidv/esbuild
ESBuild
- Loading branch information
Showing
24 changed files
with
986 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const { esmBuild, esmCoreBuild, iifeBuild } = require('./util.cjs'); | ||
const { build } = require('esbuild'); | ||
|
||
const handler = (e) => { | ||
console.error(e); | ||
process.exit(1); | ||
}; | ||
const watch = process.argv.includes('--watch'); | ||
|
||
// mermaid.js | ||
build(iifeBuild({ minify: false, watch })).catch(handler); | ||
// mermaid.esm.mjs | ||
build(esmBuild({ minify: false, watch })).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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const esbuild = require('esbuild'); | ||
const http = require('http'); | ||
const path = require('path'); | ||
const { iifeBuild } = require('./util.cjs'); | ||
|
||
// Start esbuild's server on a random local port | ||
esbuild | ||
.serve( | ||
{ | ||
servedir: path.join(__dirname, '..'), | ||
}, | ||
iifeBuild({ minify: false }) | ||
) | ||
.then((result) => { | ||
// The result tells us where esbuild's local server is | ||
const { host, port } = result; | ||
|
||
// Then start a proxy server on port 3000 | ||
http | ||
.createServer((req, res) => { | ||
if (req.url.includes('mermaid.js')) { | ||
req.url = '/dist/mermaid.js'; | ||
} | ||
const options = { | ||
hostname: host, | ||
port: port, | ||
path: req.url, | ||
method: req.method, | ||
headers: req.headers, | ||
}; | ||
|
||
// Forward each incoming request to esbuild | ||
const proxyReq = http.request(options, (proxyRes) => { | ||
// If esbuild returns "not found", send a custom 404 page | ||
console.error('pp', req.url); | ||
if (proxyRes.statusCode === 404) { | ||
if (!req.url.endsWith('.html')) { | ||
res.writeHead(404, { 'Content-Type': 'text/html' }); | ||
res.end('<h1>A custom 404 page</h1>'); | ||
return; | ||
} | ||
} | ||
|
||
// Otherwise, forward the response from esbuild to the client | ||
res.writeHead(proxyRes.statusCode, proxyRes.headers); | ||
proxyRes.pipe(res, { end: true }); | ||
}); | ||
|
||
// Forward the body of the request to esbuild | ||
req.pipe(proxyReq, { end: true }); | ||
}) | ||
.listen(3000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const { Generator } = require('jison'); | ||
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 = new Generator(source, { 'token-stack': true }).generate({ | ||
moduleMain: '() => {}', // disable moduleMain (default one requires Node.JS modules) | ||
}); | ||
return { contents, warnings: [] }; | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,5 @@ cypress/snapshots/ | |
|
||
# eslint --cache file | ||
.eslintcache | ||
.tsbuildinfo | ||
tsconfig.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.