Skip to content

Commit

Permalink
Merge pull request #3503 from mermaid-js/sidv/viteVitest
Browse files Browse the repository at this point in the history
Build mermaid using Vite
  • Loading branch information
knsv authored Sep 23, 2022
2 parents dc53b2f + 1e71798 commit 1a46582
Show file tree
Hide file tree
Showing 22 changed files with 1,132 additions and 1,594 deletions.
20 changes: 0 additions & 20 deletions .esbuild/esbuild.cjs

This file was deleted.

79 changes: 0 additions & 79 deletions .esbuild/serve.cjs

This file was deleted.

94 changes: 0 additions & 94 deletions .esbuild/util.cjs

This file was deleted.

89 changes: 89 additions & 0 deletions .vite/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { build, InlineConfig } from 'vite';
import { resolve } from 'path';
import { fileURLToPath } from 'url';
import jisonPlugin from './jisonPlugin.js';
import pkg from '../package.json' assert { type: 'json' };
type OutputOptions = Exclude<
Exclude<InlineConfig['build'], undefined>['rollupOptions'],
undefined
>['output'];

const { dependencies } = pkg;
const watch = process.argv.includes('--watch');
const __dirname = fileURLToPath(new URL('.', import.meta.url));

interface BuildOptions {
minify: boolean | 'esbuild';
core?: boolean;
watch?: boolean;
}

export const getBuildConfig = ({ minify, core, watch }: BuildOptions): InlineConfig => {
const external = ['require', 'fs', 'path'];
let output: OutputOptions = [
{
name: 'mermaid',
format: 'esm',
sourcemap: true,
entryFileNames: `[name].esm${minify ? '.min' : ''}.mjs`,
},
{
name: 'mermaid',
format: 'umd',
sourcemap: true,
entryFileNames: `[name]${minify ? '.min' : ''}.js`,
},
];

if (core) {
// Core build is used to generate file without bundled dependencies.
// This is used by downstream projects to bundle dependencies themselves.
external.push(...Object.keys(dependencies));
// This needs to be an array. Otherwise vite will build esm & umd with same name and overwrite esm with umd.
output = [
{
format: 'esm',
sourcemap: true,
entryFileNames: `[name].core.mjs`,
},
];
}

const config: InlineConfig = {
configFile: false,
build: {
emptyOutDir: false,
lib: {
entry: resolve(__dirname, '../src/mermaid.ts'),
name: 'mermaid',
// the proper extensions will be added
fileName: 'mermaid',
},
minify,
rollupOptions: {
external,
output,
},
},
resolve: {
extensions: ['.jison', '.js', '.ts', '.json'],
},
plugins: [jisonPlugin()],
};

if (watch && config.build) {
config.build.watch = {
include: 'src/**',
};
}

return config;
};

if (watch) {
build(getBuildConfig({ minify: false, watch }));
} else {
build(getBuildConfig({ minify: false }));
build(getBuildConfig({ minify: 'esbuild' }));
build(getBuildConfig({ minify: false, core: true }));
}
17 changes: 17 additions & 0 deletions .vite/jisonPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { transformJison } from './jisonTransformer.js';
const fileRegex = /\.(jison)$/;

export default function jison() {
return {
name: 'jison',

transform(src: string, id: string) {
if (fileRegex.test(id)) {
return {
code: transformJison(src),
map: null, // provide source map if available
};
}
},
};
}
9 changes: 6 additions & 3 deletions .esbuild/jisonTransformer.cjs → .vite/jisonTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { Generator } = require('jison');
exports.transformJison = (src) => {
const parser = new Generator(src, {
// @ts-ignore No typings for jison
import jison from 'jison';

export const transformJison = (src: string): string => {
// @ts-ignore No typings for jison
const parser = new jison.Generator(src, {
moduleType: 'js',
'token-stack': true,
});
Expand Down
26 changes: 26 additions & 0 deletions .vite/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import express from 'express';
import { createServer as createViteServer } from 'vite';
// import { getBuildConfig } from './build';

async function createServer() {
const app = express();

// Create Vite server in middleware mode
const vite = await createViteServer({
configFile: './vite.config.ts',
server: { middlewareMode: true },
appType: 'custom', // don't include Vite's default HTML handling middlewares
});

app.use(vite.middlewares);
app.use(express.static('dist'));
app.use(express.static('demos'));
app.use(express.static('cypress/platform'));

app.listen(9000, () => {
console.log(`Listening on http://localhost:9000`);
});
}

// build(getBuildConfig({ minify: false, watch: true }));
createServer();
6 changes: 6 additions & 0 deletions .vite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "ES2022"
}
}
6 changes: 4 additions & 2 deletions cypress/helpers/util.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Base64 } from 'js-base64';
const utf8ToB64 = (str) => {
return window.btoa(unescape(encodeURIComponent(str)));
};

export const mermaidUrl = (graphStr, options, api) => {
const obj = {
code: graphStr,
mermaid: options,
};
const objStr = JSON.stringify(obj);
let url = 'http://localhost:9000/e2e.html?graph=' + Base64.encodeURI(objStr);
let url = 'http://localhost:9000/e2e.html?graph=' + utf8ToB64(objStr);
if (api) {
url = 'http://localhost:9000/xss.html?graph=' + graphStr;
}
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/other/xss.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('XSS', () => {

it('should not allow tags in the css', () => {
const str =
'eyJjb2RlIjoiJSV7aW5pdDogeyAnZm9udEZhbWlseSc6ICdcXFwiPjwvc3R5bGU-PGltZyBzcmM9eCBvbmVycm9yPXhzc0F0dGFjaygpPid9IH0lJVxuZ3JhcGggTFJcbiAgICAgQSAtLT4gQiIsIm1lcm1haWQiOnsidGhlbWUiOiJkZWZhdWx0IiwiZmxvd2NoYXJ0Ijp7Imh0bWxMYWJlbHMiOmZhbHNlfX0sInVwZGF0ZUVkaXRvciI6ZmFsc2V9';
'eyJjb2RlIjoiJSV7aW5pdDogeyAnZm9udEZhbWlseSc6ICdcXFwiPjwvc3R5bGU+PGltZyBzcmM9eCBvbmVycm9yPXhzc0F0dGFjaygpPid9IH0lJVxuZ3JhcGggTFJcbiAgICAgQSAtLT4gQiIsIm1lcm1haWQiOnsidGhlbWUiOiJkZWZhdWx0IiwiZmxvd2NoYXJ0Ijp7Imh0bWxMYWJlbHMiOmZhbHNlfX0sInVwZGF0ZUVkaXRvciI6ZmFsc2V9';

const url = mermaidUrl(
str,
Expand Down
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 '../../src/mermaid';

let code = `flowchart LR
Power_Supply --> Transmitter_A
Expand Down
2 changes: 1 addition & 1 deletion cypress/platform/e2e.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<head>
<meta charset="utf-8" />
<!-- <meta charset="iso-8859-15"/> -->
<script src="/e2e.esm.mjs" type="module"></script>
<script src="./viewer.js" type="module" />
<!-- <link href="https://fonts.googleapis.com/css?family=Mansalva&display=swap" rel="stylesheet" /> -->
<link
href="https://fonts.googleapis.com/css?family=Noto+Sans+SC&display=swap"
Expand Down
Loading

0 comments on commit 1a46582

Please sign in to comment.