Skip to content

Commit

Permalink
feat: add a TypeScript project built by Rsbuild (#601)
Browse files Browse the repository at this point in the history
Fetch the BPMN diagram instead of storing it in the js chunks to reduce
their size.

Rsbuild provides an easy way to obtain the URL of an asset using a
regular import in the TypeScript code. This is how the diagram URL is
retrieved.
  • Loading branch information
tbouffard authored Sep 19, 2024
1 parent a34272b commit a3e4b22
Show file tree
Hide file tree
Showing 14 changed files with 502 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check-projects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
- typescript-lit-element
- typescript-vanilla-with-parcel
- typescript-vanilla-with-rollup
- typescript-vanilla-with-rsbuild
- typescript-vanilla-with-vitejs
- typescript-vue
bv-npm-package:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Show how to integrate `bpmn-visualization` in project, using various kind of fra
- [TypeScript + Lit Element](projects/typescript-lit-element/README.md) - integration in a TypeScript Lit-Element project
- [TypeScript + Parcel](projects/typescript-vanilla-with-parcel/README.md) - integration in a vanilla TypeScript Parcel project
- [TypeScript + Rollup](projects/typescript-vanilla-with-rollup/README.md) - integration in a vanilla TypeScript Rollup project
- [TypeScript + Rsbuild](projects/typescript-vanilla-with-rsbuild/README.md) - integration in a vanilla TypeScript Rsbuild project
- [TypeScript + Vite](projects/typescript-vanilla-with-vitejs/README.md) - integration in a vanilla TypeScript Vite project
- [TypeScript + Vue](projects/typescript-vue/README.md) - integration in a TypeScript Vue project

Expand Down
14 changes: 14 additions & 0 deletions projects/typescript-vanilla-with-rsbuild/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Local
.DS_Store
*.local
*.log*

# Dist
node_modules
dist/
package-lock.json

# IDE
.vscode/*
!.vscode/extensions.json
.idea
1 change: 1 addition & 0 deletions projects/typescript-vanilla-with-rsbuild/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
23 changes: 23 additions & 0 deletions projects/typescript-vanilla-with-rsbuild/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Rsbuild Project

A vanilla TypeScript project built with https://rsbuild.dev

Initialized by following https://rsbuild.dev/guide/start/quick-start:
- `npm create rsbuild@latest`
- this used [email protected]


To run locally:

1. `npm install`
2. `npm start`
3. [localhost app](http://localhost:3000)

You will see the following diagram:

![BPMN diagram in the home page](docs/home.png)

The code calling `bpmn-visualization` to render the BPMN diagram is available in [index.ts](src/index.ts).

If you want to bundle the application, run `npm run build` and then run `npm run preview` to access to a preview of the
bundle application with http://localhost:3000.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions projects/typescript-vanilla-with-rsbuild/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "typescript-vanilla-with-rsbuild",
"private": true,
"version": "0.0.0",
"scripts": {
"start": "rsbuild dev --open",
"build": "rsbuild build",
"preview": "rsbuild preview"
},
"dependencies": {
"bpmn-visualization": "0.44.0"
},
"devDependencies": {
"@rsbuild/core": "1.0.4",
"typescript": "~5.5.4"
}
}
25 changes: 25 additions & 0 deletions projects/typescript-vanilla-with-rsbuild/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
html: {
favicon: './src/assets/favicon.svg',
title: 'bpmn-visualization TypeScript Integration with Rsbuild',
},
output: {
// ensure assets are correctly loaded when the application is not at the root of the server, for examples when it is deployed on GitHub pages.
assetPrefix: 'auto',
},
// https://rsbuild.dev/guide/basic/static-assets#extend-asset-types
tools: {
rspack(config, { addRules }) {
addRules([
{
// for BPMN diagrams
test: /\.xml$/,
// converts asset to a separate file and exports the URL address.
type: 'asset/resource',
},
]);
},
},
});

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions projects/typescript-vanilla-with-rsbuild/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.xml' {
const content: string;
export default content;
}
26 changes: 26 additions & 0 deletions projects/typescript-vanilla-with-rsbuild/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
body {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
}
footer {
position: absolute;
bottom: 1rem;
right: 1rem;
z-index: 1;
}

.title {
text-align: center;
margin-top: 3rem;
}

.bpmn-highlight.bpmn-task > rect {
fill: pink;
fill-opacity: 30%;
stroke: coral;
}
.bpmn-highlight.bpmn-task >rect:hover {
fill-opacity: 70%;
}
83 changes: 83 additions & 0 deletions projects/typescript-vanilla-with-rsbuild/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import './index.css';
// the URL support is provided by rsbuild, see rsbuild.config.ts
// use the xml extension, as surge.sh returns an "HTTP 410 (Gone)" error for the bpmn extension
import diagramUrl from './assets/EC-purchase-orders-collapsed.xml';
import { BpmnVisualization, FitType, getVersion, mxgraph } from 'bpmn-visualization';


document.querySelector('#root')!.innerHTML = `
<h1 class="title">bpmn-visualization TypeScript Integration with Rsbuild</h1>
<div id="bpmn-container"></div>
<footer></footer>
`;

// instantiate BpmnVisualization, pass the container HTMLElement - present in index.html
const bpmnVisualization = new BpmnVisualization({
container: "bpmn-container",
});
// load the BPMN diagram defined above
const response = await fetch(diagramUrl);
const diagram = await response.text();
bpmnVisualization.load(diagram, { fit: { type: FitType.Center, margin: 10}});
const registry = bpmnVisualization.bpmnElementsRegistry;

// Style elements with CSS
registry.addCssClasses(
[
'Activity_1t65hvk', // Create Purchase Order Item
'Activity_00vbm9s', // Record Goods Receipts
],
'bpmn-highlight',
);

// Style elements with the "Update Style" API
registry.updateStyle(['Gateway_1ezcj46', 'Activity_0yabbur', 'Event_07598zy'],
{
stroke: { color: 'blue', width: 6 },
fill: { color: 'orange', opacity: 40 },
});
registry.updateStyle(['Flow_1kkicvr', 'Flow_12q12yb'],
{
stroke: { color: 'blue', width: 4 },
});

// Add overlays
// Record Invoice Receipt
registry.addOverlays('Activity_1u4jwkv',
{
label: '150',
position: 'top-center',
style: {
font: {
size: 18
},
stroke: {
color: 'white'
}
},
});
// Remove Payment Block
registry.addOverlays('Activity_083jf01',
{
label: '72',
position: 'top-center',
style: {
fill: {
color: '#ff0101'
},
font: {
color: 'white',
size: 22,
},
stroke: {
color: '#ff0101',
},
},
});

// display the bpmn-visualization version in the footer
const footer = document.querySelector<HTMLElement>('footer')!;
const version = getVersion();
const versionAsString = `bpmn-visualization@${version.lib}`;
const dependenciesAsString = [...version.dependencies].map(([name, version]) => `${name}@${version}`).join('/');
footer.innerText = `${versionAsString} with ${dependenciesAsString} | direct usage of mxGraph@${mxgraph.mxClient.VERSION}`;
16 changes: 16 additions & 0 deletions projects/typescript-vanilla-with-rsbuild/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "ES2020",
"lib": ["DOM", "ES2020"],
"module": "ESNext",
"noEmit": true,
"strict": true,
"skipLibCheck": true,
"isolatedModules": true,
"resolveJsonModule": true,
"moduleResolution": "bundler",
"useDefineForClassFields": true,
"allowImportingTsExtensions": true
},
"include": ["src"]
}

0 comments on commit a3e4b22

Please sign in to comment.