mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-08-23 10:16:43 +02:00
Feat: Calculate package size change on PRs
This commit is contained in:
45
.github/workflows/e2e.yml
vendored
45
.github/workflows/e2e.yml
vendored
@@ -15,6 +15,7 @@ on:
|
|||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
pull-requests: write
|
||||||
|
|
||||||
env:
|
env:
|
||||||
# For PRs and MergeQueues, the target commit is used, and for push events, github.event.previous is used.
|
# For PRs and MergeQueues, the target commit is used, and for push events, github.event.previous is used.
|
||||||
@@ -45,11 +46,26 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
ref: ${{ env.targetHash }}
|
ref: ${{ env.targetHash }}
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
if: ${{ steps.cache-snapshot.outputs.cache-hit != 'true' }}
|
||||||
|
uses: cypress-io/github-action@v6
|
||||||
|
with:
|
||||||
|
# just perform install
|
||||||
|
runTests: false
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
if: ${{ steps.cache-snapshot.outputs.cache-hit != 'true' && github.event_name == 'pull_request' }}
|
||||||
|
run: |
|
||||||
|
pnpm run build:viz
|
||||||
|
mkdir -p cypress/snapshots/stats/base
|
||||||
|
mv stats cypress/snapshots/stats/base
|
||||||
|
|
||||||
- name: Cypress run
|
- name: Cypress run
|
||||||
uses: cypress-io/github-action@v4
|
uses: cypress-io/github-action@v6
|
||||||
id: cypress-snapshot-gen
|
id: cypress-snapshot-gen
|
||||||
if: ${{ steps.cache-snapshot.outputs.cache-hit != 'true' }}
|
if: ${{ steps.cache-snapshot.outputs.cache-hit != 'true' }}
|
||||||
with:
|
with:
|
||||||
|
install: false
|
||||||
start: pnpm run dev
|
start: pnpm run dev
|
||||||
wait-on: 'http://localhost:9000'
|
wait-on: 'http://localhost:9000'
|
||||||
browser: chrome
|
browser: chrome
|
||||||
@@ -81,15 +97,27 @@ jobs:
|
|||||||
path: ./cypress/snapshots
|
path: ./cypress/snapshots
|
||||||
key: ${{ runner.os }}-snapshots-${{ env.targetHash }}
|
key: ${{ runner.os }}-snapshots-${{ env.targetHash }}
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
uses: cypress-io/github-action@v6
|
||||||
|
with:
|
||||||
|
runTests: false
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
if: ${{ github.event_name == 'pull_request' }}
|
||||||
|
run: |
|
||||||
|
pnpm run build:viz
|
||||||
|
mv stats cypress/snapshots/stats/head
|
||||||
|
|
||||||
# Install NPM dependencies, cache them correctly
|
# Install NPM dependencies, cache them correctly
|
||||||
# and run all Cypress tests
|
# and run all Cypress tests
|
||||||
- name: Cypress run
|
- name: Cypress run
|
||||||
uses: cypress-io/github-action@v4
|
uses: cypress-io/github-action@v6
|
||||||
id: cypress
|
id: cypress
|
||||||
# If CYPRESS_RECORD_KEY is set, run in parallel on all containers
|
# If CYPRESS_RECORD_KEY is set, run in parallel on all containers
|
||||||
# Otherwise (e.g. if running from fork), we run on a single container only
|
# Otherwise (e.g. if running from fork), we run on a single container only
|
||||||
if: ${{ ( env.CYPRESS_RECORD_KEY != '' ) || ( matrix.containers == 1 ) }}
|
if: ${{ ( env.CYPRESS_RECORD_KEY != '' ) || ( matrix.containers == 1 ) }}
|
||||||
with:
|
with:
|
||||||
|
install: false
|
||||||
start: pnpm run dev:coverage
|
start: pnpm run dev:coverage
|
||||||
wait-on: 'http://localhost:9000'
|
wait-on: 'http://localhost:9000'
|
||||||
browser: chrome
|
browser: chrome
|
||||||
@@ -136,6 +164,19 @@ jobs:
|
|||||||
pattern: snapshots-*
|
pattern: snapshots-*
|
||||||
merge-multiple: true
|
merge-multiple: true
|
||||||
|
|
||||||
|
- name: Calculate Size difference
|
||||||
|
id: size
|
||||||
|
if: ${{ github.event_name == 'pull_request' }}
|
||||||
|
run: echo "::set-output name=size_diff::$(npx tsx scripts/size.ts)"
|
||||||
|
|
||||||
|
- name: Comment PR size difference
|
||||||
|
if: ${{ github.event_name == 'pull_request' }}
|
||||||
|
uses: thollander/actions-comment-pull-request@v2
|
||||||
|
with:
|
||||||
|
message: |
|
||||||
|
${{ steps.size.outputs.size_diff }}
|
||||||
|
comment_tag: size-diff
|
||||||
|
|
||||||
# For successful push events, we save the snapshots cache
|
# For successful push events, we save the snapshots cache
|
||||||
- name: Save snapshots cache
|
- name: Save snapshots cache
|
||||||
id: cache-upload
|
id: cache-upload
|
||||||
|
@@ -110,6 +110,7 @@
|
|||||||
"jsdom": "^22.0.0",
|
"jsdom": "^22.0.0",
|
||||||
"langium-cli": "2.0.1",
|
"langium-cli": "2.0.1",
|
||||||
"lint-staged": "^13.2.1",
|
"lint-staged": "^13.2.1",
|
||||||
|
"markdown-table": "^3.0.3",
|
||||||
"nyc": "^15.1.0",
|
"nyc": "^15.1.0",
|
||||||
"path-browserify": "^1.0.1",
|
"path-browserify": "^1.0.1",
|
||||||
"pnpm": "^8.6.8",
|
"pnpm": "^8.6.8",
|
||||||
|
3
pnpm-lock.yaml
generated
3
pnpm-lock.yaml
generated
@@ -155,6 +155,9 @@ importers:
|
|||||||
lint-staged:
|
lint-staged:
|
||||||
specifier: ^13.2.1
|
specifier: ^13.2.1
|
||||||
version: 13.2.3
|
version: 13.2.3
|
||||||
|
markdown-table:
|
||||||
|
specifier: ^3.0.3
|
||||||
|
version: 3.0.3
|
||||||
nyc:
|
nyc:
|
||||||
specifier: ^15.1.0
|
specifier: ^15.1.0
|
||||||
version: 15.1.0
|
version: 15.1.0
|
||||||
|
57
scripts/size.ts
Normal file
57
scripts/size.ts
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
|
import type { Metafile } from 'esbuild';
|
||||||
|
import { readFile } from 'fs/promises';
|
||||||
|
import { globby } from 'globby';
|
||||||
|
import { markdownTable } from 'markdown-table';
|
||||||
|
export const getSizes = (metafile: Metafile) => {
|
||||||
|
const { outputs } = metafile;
|
||||||
|
const sizes = Object.keys(outputs)
|
||||||
|
.filter((key) => key.endsWith('js') && !key.includes('chunk'))
|
||||||
|
.map((key) => {
|
||||||
|
const { bytes } = outputs[key];
|
||||||
|
return [key.replace('dist/', ''), bytes];
|
||||||
|
});
|
||||||
|
return sizes;
|
||||||
|
};
|
||||||
|
|
||||||
|
const readStats = async (path: string): Promise<Record<string, number>> => {
|
||||||
|
const files = await globby(path);
|
||||||
|
const contents = await Promise.all(files.map((file) => readFile(file, 'utf-8')));
|
||||||
|
const sizes = contents.flatMap((content) => getSizes(JSON.parse(content)));
|
||||||
|
return Object.fromEntries(sizes);
|
||||||
|
};
|
||||||
|
|
||||||
|
const percentageDifference = (oldValue: number, newValue: number): string => {
|
||||||
|
const difference = Math.abs(newValue - oldValue);
|
||||||
|
const avg = (newValue + oldValue) / 2;
|
||||||
|
const percentage = (difference / avg) * 100;
|
||||||
|
const roundedPercentage = percentage.toFixed(2); // Round to two decimal places
|
||||||
|
if (roundedPercentage === '0.00') {
|
||||||
|
return '0.00%';
|
||||||
|
}
|
||||||
|
const sign = newValue > oldValue ? '+' : '-';
|
||||||
|
return `${sign}${roundedPercentage}%`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const main = async () => {
|
||||||
|
const oldStats = await readStats('./cypress/snapshots/base/*.json');
|
||||||
|
const newStats = await readStats('./cypress/snapshots/head/*.json');
|
||||||
|
const diff = Object.entries(newStats)
|
||||||
|
.map(([key, value]) => {
|
||||||
|
const oldValue = oldStats[key];
|
||||||
|
const delta = value - oldValue;
|
||||||
|
return [key, oldValue, value, delta, percentageDifference(oldValue, value)].map((v) =>
|
||||||
|
v.toString()
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.filter(([, , , delta]) => delta !== '0');
|
||||||
|
if (diff.length === 0) {
|
||||||
|
console.log('No changes in bundle sizes');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log(
|
||||||
|
markdownTable([['File', 'Previous Size', 'New Size', 'Difference', '% Change'], ...diff])
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
void main().catch((e) => console.error(e));
|
Reference in New Issue
Block a user