fix: 30% smaler bundle - Replace mathjs with lightweight formula evaluator in admin UI- R19#2448
fix: 30% smaler bundle - Replace mathjs with lightweight formula evaluator in admin UI- R19#2448dirkwa wants to merge 2 commits intoSignalK:masterfrom
Conversation
|
R16 fails - it has own formulas, so I will rebase this on R19 sole #2441 |
Rename the React 19 admin UI package from @signalk/server-admin-ui-react19 to @signalk/server-admin-ui, replacing the old React 16 admin UI. The -react19 suffix was only needed during the coexistence/testing period. The SIGNALK_ADMIN_UI env var toggle is no longer needed. Legacy R16 plugins continue to work via the bridge in dynamicutilities.ts. The /@signalk/server-admin-ui route still redirects to /admin/ for backwards compatibility.
mathjs added ~660KB to the admin UI bundle for simple arithmetic formula evaluation. Use cached Function constructors instead, as the conversion formulas are server-controlled and contain only basic arithmetic with a single `value` variable.
42046cc to
634c4ce
Compare
| let cached = compiledFormulaCache.get(formula) | ||
| if (!cached) { | ||
| cached = compile(formula) | ||
| cached = new Function('value', `return (${formula})`) as ( |
There was a problem hiding this comment.
We chose mathjs explicitly so that we are not evaluating unvalidated user supplied code.
If we want to get rid of mathjs we can instead generate a library that will have all the known source-target conversions (there are only a finite number of them) as static js functions and do just a lookup on source and target.
This won't allow adding arbitratry dynamic target units, but will support almost all practical use cases in Data Browser.
There was a problem hiding this comment.
Re: server controlled: yes, the conversions are today server controlled, but I think this is just a vector I don't want to open.
There was a problem hiding this comment.
Really? All this balloning just to know how much my tank holds in tea spoons?
There was a problem hiding this comment.
Re: server controlled: yes, the conversions are today server controlled, but I think this is just a vector I don't want to open.
I briefly thought about it, as option, because the server has math.js anyway.
Balooning the bundle is not a good idea. I see the impact already on my PI 4 in the wifi. It gets boaring slow.
Let me change this to server side.
There was a problem hiding this comment.
And how do we want to bring the performance back of RPI over wifi?
Split the bundle?
There was a problem hiding this comment.
I tried the split and are quite impressed.
Initial loading is way quicker and a better expirience than waiting in front of a white browser being unsure if anything crashed.
Summary
mathjswas introduced in 6251163 ("Implement display units system in metadata", #2225) to evaluate unit conversion formulas. It's a full computer algebra system (~18MB on disk, 396 transitive packages) but the formulas are simple arithmetic likevalue * 1.94384orvalue - 273.15.This replaces
mathjswith cachednew Function()constructors. The formulas are server-controlled, not user input, so this is safe.Bundle reduction: 2,148 KB → 1,506 KB (−30%), removes 152 transitive packages.
Test plan
Note: