Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESBuild #3386

Merged
merged 48 commits into from
Sep 16, 2022
Merged

ESBuild #3386

Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
4f0e18b
Esbuild
sidharthv96 Aug 31, 2022
965df4f
ESBuild
sidharthv96 Aug 31, 2022
1206ec4
Remove jisonloader
sidharthv96 Aug 31, 2022
d67e272
Serve
sidharthv96 Sep 1, 2022
7aeb60f
Merge branch 'develop' into sidv/esbuild
sidharthv96 Sep 1, 2022
fe3bb0b
Typescript
sidharthv96 Sep 1, 2022
0156252
Typescript
sidharthv96 Sep 1, 2022
84bf79f
Fix export
sidharthv96 Sep 1, 2022
6be05e9
Esbuild with types
sidharthv96 Sep 1, 2022
53fe35e
Add webpack build
sidharthv96 Sep 1, 2022
f1fa91a
fix flowchart jison
sidharthv96 Sep 1, 2022
04f1863
use same esbuild in webpack
sidharthv96 Sep 1, 2022
99923fc
Test - esbuild
sidharthv96 Sep 2, 2022
f9bf535
Merge branch 'develop' into sidv/esbuild
sidharthv96 Sep 5, 2022
a0fa8df
Merge branch 'develop' into sidv/esbuild
sidharthv96 Sep 5, 2022
3a17917
Surface `jison` errors
sidharthv96 Sep 6, 2022
51dbdb9
Fix mermaidAPI mock
sidharthv96 Sep 7, 2022
853b676
Merge branch 'develop' into sidv/esbuild
sidharthv96 Sep 7, 2022
a61c17c
Fix doc formatting
sidharthv96 Sep 7, 2022
86cbf85
Merge branch 'develop' into sidv/esbuild
sidharthv96 Sep 7, 2022
056d520
Fix postbuild
sidharthv96 Sep 7, 2022
8a476f8
Remove native memoize
sidharthv96 Sep 9, 2022
1c6328c
Merge branch 'develop' into sidv/memoize
sidharthv96 Sep 9, 2022
5aae45d
fix: Use lodash memoize
sidharthv96 Sep 9, 2022
8e2287a
Merge branch 'develop' into sidv/esbuild
sidharthv96 Sep 9, 2022
b1770d3
Merge branch 'sidv/esbuild' into sidv/memoize
sidharthv96 Sep 9, 2022
d2e7b1e
fix: Support treeshaking
sidharthv96 Sep 9, 2022
ffcb73a
Merge pull request #3434 from mermaid-js/sidv/memoize
knsv Sep 9, 2022
1029ce4
fix: Add `.core` build.
sidharthv96 Sep 9, 2022
c8d3c3a
Correct name `umd` -> `iife`
sidharthv96 Sep 9, 2022
5148acb
Specify `iife` format
sidharthv96 Sep 9, 2022
48a899f
build: remove main function from `.jison` files
aloisklink Sep 10, 2022
37aaca0
build: convert core build to unbundled ESM
aloisklink Sep 11, 2022
5554725
build: change package export to mermaid.core.mjs
aloisklink Sep 11, 2022
0e504f3
Merge branch 'develop' into sidv/esbuild
sidharthv96 Sep 12, 2022
3f76eb0
docs: update `mermaid.core.*` JSDoc
aloisklink Sep 12, 2022
a3bda3c
Merge pull request #3437 from aloisklink/esbuild-backwards-compatible…
sidharthv96 Sep 12, 2022
e740325
Unify webpack build
sidharthv96 Sep 12, 2022
681f4bb
Keep webpack as default build option
sidharthv96 Sep 12, 2022
d873506
Disable minimization on non `min` files.
sidharthv96 Sep 12, 2022
a87abc0
Add diagramAPI to outfile
sidharthv96 Sep 13, 2022
a7fa40e
Remove extension
sidharthv96 Sep 14, 2022
73d02b2
Merge branch 'develop' into sidv/esbuild
sidharthv96 Sep 15, 2022
2f41013
Merge branch 'develop' into sidv/esbuild
sidharthv96 Sep 16, 2022
2693c9b
Fix coverage
sidharthv96 Sep 16, 2022
6452ccc
Merge branch 'develop' into sidv/esbuild
sidharthv96 Sep 16, 2022
2579bf1
Merge branch 'develop' into sidv/esbuild
sidharthv96 Sep 16, 2022
bb413d5
cleanup
sidharthv96 Sep 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .esbuild/esbuild.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { esmBuild, umdBuild } = require('./util.cjs');
const { build } = require('esbuild');

const handler = (e) => {
console.error(e);
process.exit(1);
};
const watch = process.argv.includes('--watch');

build(umdBuild({ minify: false, watch })).catch(handler);
build(esmBuild({ minify: false, watch })).catch(handler);

build(esmBuild()).catch(handler);
build(umdBuild()).catch(handler);
53 changes: 53 additions & 0 deletions .esbuild/serve.cjs
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 { umdBuild } = require('./util.cjs');

// Start esbuild's server on a random local port
esbuild
.serve(
{
servedir: path.join(__dirname, '..'),
aloisklink marked this conversation as resolved.
Show resolved Hide resolved
},
umdBuild({ 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);
});
62 changes: 62 additions & 0 deletions .esbuild/util.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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} override
* @returns {Options}
*/
exports.umdBuild = (override = { minify: true }) => {
return buildOptions({
outfile: `dist/mermaid${override.minify ? '.min' : ''}.js`,
...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();
return { contents, warnings: [] };
});
},
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ cypress/snapshots/

# eslint --cache file
.eslintcache
.tsbuildinfo
tsconfig.tsbuildinfo
15 changes: 14 additions & 1 deletion .webpack/webpack.config.base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import path from 'path';
const esbuild = require('esbuild');
const { ESBuildMinifyPlugin } = require('esbuild-loader');
export const resolveRoot = (...relativePath) => path.resolve(__dirname, '..', ...relativePath);

export default {
Expand Down Expand Up @@ -35,7 +37,11 @@ export default {
test: /\.js$/,
include: [resolveRoot('./src'), resolveRoot('./node_modules/dagre-d3-renderer/lib')],
use: {
loader: 'babel-loader',
loader: 'esbuild-loader',
options: {
implementation: esbuild,
target: 'es2015',
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
{
Expand All @@ -55,4 +61,11 @@ export default {
],
},
devtool: 'source-map',
optimization: {
minimizer: [
new ESBuildMinifyPlugin({
target: 'es2015',
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved
}),
],
},
};
11 changes: 0 additions & 11 deletions .webpack/webpack.config.e2e.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,4 @@ export default merge(baseConfig, {
externals: {
mermaid: 'mermaid',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
],
},
});
2 changes: 1 addition & 1 deletion cypress/platform/bundle-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mermaid from '../../dist/mermaid.core';
import mermaid from '../../dist/mermaid';
knsv marked this conversation as resolved.
Show resolved Hide resolved

let code = `flowchart LR
Power_Supply --> Transmitter_A
Expand Down
31 changes: 21 additions & 10 deletions docs/Setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,15 @@ This sets the auto-wrap padding for the diagram (sides only)

**Notes:** Default value: 0.

## parse

### Parameters

- `text` **[string][5]**
- `parseError` **[Function][6]?**

Returns **[boolean][7]**

## setSiteConfig

## setSiteConfig
Expand All @@ -1424,7 +1433,7 @@ function _Default value: At default, will mirror Global Config_

- `conf` **MermaidConfig** The base currentConfig to use as siteConfig

Returns **[object][5]** The siteConfig
Returns **[object][8]** The siteConfig

## getSiteConfig

Expand All @@ -1436,7 +1445,7 @@ Returns **[object][5]** The siteConfig

**Notes**: Returns **any** values in siteConfig.

Returns **[object][5]** The siteConfig
Returns **[object][8]** The siteConfig

## setConfig

Expand Down Expand Up @@ -1475,10 +1484,10 @@ $(function () {

### Parameters

- `id` **[string][6]** The id of the element to be rendered
- `text` **[string][6]** The graph definition
- `cb` **function (svgCode: [string][6], bindFunctions: function (element: [Element][7]): void): void**
- `container` **[Element][7]** Selector to element in which a div with the graph temporarily will be
- `id` **[string][5]** The id of the element to be rendered
- `text` **[string][5]** The graph definition
- `cb` **function (svgCode: [string][5], bindFunctions: function (element: [Element][9]): void): void**
- `container` **[Element][9]** Selector to element in which a div with the graph temporarily will be
inserted. If one is provided a hidden div will be inserted in the body of the page instead. The
element will be removed when rendering is completed.

Expand Down Expand Up @@ -1517,7 +1526,7 @@ Pushes in a directive to the configuration

### Parameters

- `directive` **[object][5]** The directive to push in
- `directive` **[object][8]** The directive to push in

## reset

Expand Down Expand Up @@ -1615,6 +1624,8 @@ Returns **void**
[2]: Setup.md?id=render
[3]: 8.6.0_docs.md
[4]: #mermaidapi-configuration-defaults
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[7]: https://developer.mozilla.org/docs/Web/API/Element
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[9]: https://developer.mozilla.org/docs/Web/API/Element
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const path = require('path');

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
testEnvironment: 'jsdom',
preset: 'ts-jest',
transform: {
'^.+\\.jsx?$': ['babel-jest', { rootMode: 'upward' }],
'^.+\\.tsx?$': ['jest-esbuild', { banner: '"use strict";' }],
'^.+\\.jsx?$': ['jest-esbuild', { banner: '"use strict";' }],
'^.+\\.jison$': [
path.resolve(__dirname, './src/jison/transformer.js'),
{ 'token-stack': true },
Expand Down
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
"git graph"
],
"scripts": {
"build:dev": "webpack --mode development --progress --color",
"build:prod": "webpack --mode production --progress --color",
"build": "concurrently \"yarn build:dev\" \"yarn build:prod\"",
"clean": "rimraf dist",
"build:code": "node .esbuild/esbuild.cjs",
"build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly",
"build:webpack": "webpack --mode production --progress --color",
"build:watch": "yarn build:code --watch",
"build": "yarn clean; concurrently \"yarn build:code\" \"yarn build:types\"",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@knsv There are changes to the build commands.

  • build:code: Only builds code. Does not typecheck. (The editor will be verifying types anyway. This is the fastest)
  • build:types: Only builds types using tsc. Slower than just esbuild.
  • build:webpack: Uses eslint-loader within webpack to build as we were doing previously.
  • build: Builds both code & types.

"docs:build": "ts-node-esm src/docs.mts",
"docs:verify": "ts-node-esm src/docs.mts --verify",
"postbuild": "documentation build src/mermaidAPI.ts src/config.ts src/defaultConfig.ts --shallow -f md --markdown-toc false > src/docs/Setup.md && prettier --write src/docs/Setup.md && yarn docs:build",
"build:watch": "yarn build:dev --watch",
"release": "yarn build",
"lint": "eslint --cache --ignore-path .gitignore . && prettier --check .",
"lint:fix": "eslint --fix --ignore-path .gitignore . && prettier --write .",
Expand All @@ -43,7 +45,7 @@
"test": "yarn lint && jest src/.*",
"test:watch": "jest --watch src",
"prepublishOnly": "yarn build && yarn test",
"prepare": "concurrently \"husky install\" \"yarn build:prod\" \"yarn postbuild\"",
"prepare": "concurrently \"husky install\" \"yarn build\"",
"pre-commit": "lint-staged"
},
"repository": {
Expand Down Expand Up @@ -97,6 +99,8 @@
"cypress": "9.7.0",
"cypress-image-snapshot": "^4.0.1",
"documentation": "13.2.0",
"esbuild": "^0.15.6",
"esbuild-loader": "^2.19.0",
"eslint": "^8.23.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-cypress": "^2.12.1",
Expand All @@ -110,6 +114,7 @@
"identity-obj-proxy": "^3.0.0",
"jest": "^28.0.3",
"jest-environment-jsdom": "^29.0.2",
"jest-esbuild": "^0.2.9",
"jison": "^0.4.18",
"js-base64": "3.7.2",
"lint-staged": "^13.0.0",
Expand All @@ -118,6 +123,7 @@
"prettier": "^2.7.1",
"prettier-plugin-jsdoc": "^0.4.2",
"remark": "^14.0.2",
"rimraf": "^3.0.2",
"start-server-and-test": "^1.12.6",
"terser-webpack-plugin": "^5.3.6",
"ts-jest": "^28.0.8",
Expand Down
4 changes: 2 additions & 2 deletions src/dagre-wrapper/intersect/intersect-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module.exports = intersectNode;

/**
* @param node
* @param point
Expand All @@ -8,3 +6,5 @@ function intersectNode(node, point) {
// console.info('Intersect Node');
return node.intersect(point);
}

export default intersectNode;
2 changes: 1 addition & 1 deletion src/diagrams/c4/c4Renderer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { select } from 'd3';
import svgDraw, { drawText, fixLifeLineHeights } from './svgDraw';
import svgDraw from './svgDraw';
import { log } from '../../logger';
import { parser } from './parser/c4Diagram';
import common from '../common/common';
Expand Down
2 changes: 1 addition & 1 deletion src/diagrams/flowchart/parser/flow.jison
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ document
{ $$ = [];}
| document line
{
if($2 !== []){
if(!Array.isArray($2) || $2.length > 0){
knsv marked this conversation as resolved.
Show resolved Hide resolved
$1.push($2);
}
$$=$1;}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import { select, selectAll } from 'd3';
import svgDraw, { drawText, fixLifeLineHeights } from './svgDraw';
import { log } from '../../logger';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import { select } from 'd3';
import svgDraw from './svgDraw';
import { getConfig } from '../../config';
Expand Down
Loading