Skip to content

Commit 650a3e4

Browse files
authored
Dynamically load Chart.js bundle (hestiacp#3480)
* Remove fill from some lines in charts * Bump PostCSS version * Dynamically load Chart.js bundle
1 parent 50325c5 commit 650a3e4

File tree

8 files changed

+98
-42
lines changed

8 files changed

+98
-42
lines changed

build.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import { promises as fs } from 'node:fs';
55
import path from 'node:path';
66
import postcssConfig from './postcss.config.js';
77

8-
const esbuildConfig = {
9-
outfile: './web/js/dist/main.min.js',
10-
bundle: true,
11-
minify: true,
12-
sourcemap: true,
13-
};
8+
// Packages to build but exclude from bundle
9+
const externalPackages = ['chart.js/auto'];
1410

15-
// Build JavaScript
11+
// Build main bundle
1612
async function buildJS() {
1713
const inputPath = './web/js/src/main.js';
1814
try {
1915
await esbuild.build({
20-
...esbuildConfig,
2116
entryPoints: [inputPath],
17+
outfile: './web/js/dist/main.min.js',
18+
bundle: true,
19+
minify: true,
20+
sourcemap: true,
21+
external: externalPackages,
2222
});
2323
console.log('✅ JavaScript build completed for', inputPath);
2424
} catch (error) {
@@ -27,7 +27,29 @@ async function buildJS() {
2727
}
2828
}
2929

30-
// Process and build CSS
30+
// Build external packages
31+
async function buildExternalJS() {
32+
try {
33+
const buildPromises = externalPackages.map(async (pkg) => {
34+
const outputPath = `./web/js/dist/${pkg.replace('/', '-')}.min.js`;
35+
await esbuild.build({
36+
entryPoints: [pkg],
37+
outfile: outputPath,
38+
bundle: true,
39+
minify: true,
40+
format: 'esm',
41+
});
42+
console.log(`✅ Dependency build completed for ${pkg}`);
43+
});
44+
45+
await Promise.all(buildPromises);
46+
} catch (error) {
47+
console.error('❌ Error building external packages:', error);
48+
process.exit(1);
49+
}
50+
}
51+
52+
// Process a CSS file
3153
async function processCSS(inputFile, outputFile) {
3254
try {
3355
const css = await fs.readFile(inputFile);
@@ -43,7 +65,7 @@ async function processCSS(inputFile, outputFile) {
4365
}
4466
}
4567

46-
// Build CSS files
68+
// Build CSS
4769
async function buildCSS() {
4870
const themesSourcePath = './web/css/src/themes/';
4971
const cssEntries = await fs.readdir(themesSourcePath);
@@ -64,6 +86,7 @@ async function buildCSS() {
6486
async function build() {
6587
console.log('🚀 Building JS and CSS...');
6688
await buildJS();
89+
await buildExternalJS();
6790
await buildCSS();
6891
console.log('🎉 Build completed.');
6992
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"packageManager": "yarn@3.5.0",
2121
"dependencies": {
2222
"@fortawesome/fontawesome-free": "^6.4.0",
23+
"chart.js": "^4.2.1",
2324
"nanoid": "^4.0.2",
2425
"normalize.css": "^8.0.1"
2526
},
@@ -35,7 +36,7 @@
3536
"husky": "^8.0.3",
3637
"lint-staged": "^13.2.1",
3738
"markdownlint-cli2": "^0.6.0",
38-
"postcss": "^8.4.22",
39+
"postcss": "^8.4.23",
3940
"postcss-import": "^15.1.0",
4041
"postcss-path-replace": "^1.0.4",
4142
"postcss-preset-env": "^8.3.2",

web/js/dist/chart.js-auto.min.js

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/js/dist/main.min.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/js/pages/list_rrd.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
document.addEventListener('DOMContentLoaded', main);
1+
async function loadChartJs() {
2+
const module = await import('/js/dist/chart.js-auto.min.js');
3+
return module.Chart;
4+
}
25

36
async function main() {
7+
const Chart = await loadChartJs();
48
const chartCanvases = document.querySelectorAll('.js-rrd-chart');
59

610
for (const chartCanvas of chartCanvases) {
@@ -29,8 +33,6 @@ async function fetchRrdData(service, period) {
2933
}
3034

3135
function prepareChartData(rrdData, period) {
32-
const totalDatasets = rrdData.meta.legend.length;
33-
3436
return {
3537
labels: rrdData.data.map((_, index) => {
3638
const timestamp = rrdData.meta.start + index * rrdData.meta.step;
@@ -45,10 +47,8 @@ function prepareChartData(rrdData, period) {
4547
data: rrdData.data.map((dataPoint) => dataPoint[legendIndex]),
4648
tension: 0.3,
4749
pointStyle: false,
48-
fill: legendIndex === 0 && totalDatasets > 1,
4950
borderWidth: 2,
5051
borderColor: lineColor,
51-
backgroundColor: lineColor,
5252
};
5353
}),
5454
};
@@ -107,3 +107,5 @@ function getChartOptions(unit) {
107107
function getCssVariable(variableName) {
108108
return getComputedStyle(document.documentElement).getPropertyValue(variableName).trim();
109109
}
110+
111+
main();

web/js/vendor/chart.min.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

web/templates/pages/list_rrd.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
</div>
2020
<!-- End toolbar -->
2121

22-
<script defer src="/js/vendor/chart.min.js?<?= JS_LATEST_UPDATE ?>"></script>
23-
2422
<div class="container animate__animated animate__fadeIn">
2523
<div class="form-container form-container-wide">
2624
<!-- Begin graph list item loop -->

yarn.lock

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,13 @@ __metadata:
834834
languageName: node
835835
linkType: hard
836836

837+
"@kurkle/color@npm:^0.3.0":
838+
version: 0.3.2
839+
resolution: "@kurkle/color@npm:0.3.2"
840+
checksum: 79e97b31f8f6efb28c69d373f94b0c7480226fe8ec95221f518ac998e156444a496727ce47de6d728eb5c3369288e794cba82cae34253deb0d472d3bfe080e49
841+
languageName: node
842+
linkType: hard
843+
837844
"@nodelib/fs.scandir@npm:2.1.5":
838845
version: 2.1.5
839846
resolution: "@nodelib/fs.scandir@npm:2.1.5"
@@ -1629,6 +1636,15 @@ __metadata:
16291636
languageName: node
16301637
linkType: hard
16311638

1639+
"chart.js@npm:^4.2.1":
1640+
version: 4.2.1
1641+
resolution: "chart.js@npm:4.2.1"
1642+
dependencies:
1643+
"@kurkle/color": ^0.3.0
1644+
checksum: 7319fdfd1e29812e87bbc07737e8c9072ff659bbc09ade8dd262f83424cd7e48f89afe5220e11e002c9f236883fae3f3adb8adc16acb4682b0ddcc0b661c3af9
1645+
languageName: node
1646+
linkType: hard
1647+
16321648
"chownr@npm:^2.0.0":
16331649
version: 2.0.0
16341650
resolution: "chownr@npm:2.0.0"
@@ -2108,9 +2124,9 @@ __metadata:
21082124
linkType: hard
21092125

21102126
"electron-to-chromium@npm:^1.4.284":
2111-
version: 1.4.367
2112-
resolution: "electron-to-chromium@npm:1.4.367"
2113-
checksum: 721e4958945a16ff21a63896d4e112da7b8f3fd5540a27280d5777c196fe4e1f1b51359f86c6fe83a267e81f127e4753ec9171547f094f86481a421c809473b0
2127+
version: 1.4.368
2128+
resolution: "electron-to-chromium@npm:1.4.368"
2129+
checksum: b8ec4128a81c86c287cb2d677504c64d50f30c3c1d6dd9700a93797c6311f9f94b1c49a3e5112f5cfb3987a9bbade0133f9ec9898dae592db981059d5c2abdbb
21142130
languageName: node
21152131
linkType: hard
21162132

@@ -2803,6 +2819,7 @@ __metadata:
28032819
"@prettier/plugin-php": ^0.19.4
28042820
"@typescript-eslint/eslint-plugin": ^5.59.0
28052821
"@typescript-eslint/parser": ^5.59.0
2822+
chart.js: ^4.2.1
28062823
cssnano: ^6.0.0
28072824
esbuild: ^0.17.17
28082825
eslint: ^8.38.0
@@ -2813,7 +2830,7 @@ __metadata:
28132830
markdownlint-cli2: ^0.6.0
28142831
nanoid: ^4.0.2
28152832
normalize.css: ^8.0.1
2816-
postcss: ^8.4.22
2833+
postcss: ^8.4.23
28172834
postcss-import: ^15.1.0
28182835
postcss-path-replace: ^1.0.4
28192836
postcss-preset-env: ^8.3.2
@@ -4862,14 +4879,14 @@ __metadata:
48624879
languageName: node
48634880
linkType: hard
48644881

4865-
"postcss@npm:^8.1.10, postcss@npm:^8.4.21, postcss@npm:^8.4.22":
4866-
version: 8.4.22
4867-
resolution: "postcss@npm:8.4.22"
4882+
"postcss@npm:^8.1.10, postcss@npm:^8.4.21, postcss@npm:^8.4.23":
4883+
version: 8.4.23
4884+
resolution: "postcss@npm:8.4.23"
48684885
dependencies:
48694886
nanoid: ^3.3.6
48704887
picocolors: ^1.0.0
48714888
source-map-js: ^1.0.2
4872-
checksum: 7473dfb7ac5b4cb03576c39d687d7fc02c826ab08af97df15b5d3970662532d44a18a0994f392a9c3658ee17c292e7a55990e586b90ca0afcc9f36df13e07029
4889+
checksum: 8bb9d1b2ea6e694f8987d4f18c94617971b2b8d141602725fedcc2222fdc413b776a6e1b969a25d627d7b2681ca5aabb56f59e727ef94072e1b6ac8412105a2f
48734890
languageName: node
48744891
linkType: hard
48754892

0 commit comments

Comments
 (0)