From 4540b4e4d897d60e6cbdecf4ce712ebb72090ae2 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sat, 2 May 2026 13:28:26 +0200 Subject: Update various patches --- .../JSPatches/CompactSingleExportPathPatch.cs | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/CompactSingleExportPathPatch.cs (limited to 'ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/CompactSingleExportPathPatch.cs') diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/CompactSingleExportPathPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/CompactSingleExportPathPatch.cs new file mode 100644 index 0000000..9cf8102 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/CompactSingleExportPathPatch.cs @@ -0,0 +1,70 @@ +using System.Text; +using System.Text.RegularExpressions; +using ArcaneLibs.Extensions; + +namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; + +public partial class CompactSingleExportPathPatch : IPatch +{ + public int GetOrder() => 2; + + public string GetName() => "Patch single path export function to be more compact"; + public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); + + public async Task Execute(string relativePath, byte[] content) + { + var stringContent = Encoding.UTF8.GetString(content); + + // stringContent = SimpleSingleParamExportRegex().Replace( + // stringContent, + // m => + // $"{m.Groups[1].Value}({m.Groups[2].Value}) {{ {m.Groups[2].Value}.exports = \"{m.Groups[3].Value}\"; }}," + // ); + + var fnSigLength = new Dictionary(); + var lastMatchIdx = new Dictionary(); + stringContent = SimpleMultiParamExportRegex().Replace( + stringContent, + m => + { + // might aswell make it aligned... + int argCnt = m.Groups["fnParams"].Value.CountInstances(","); + lastMatchIdx.TryAdd(argCnt, 0); + fnSigLength.TryAdd(argCnt, 0); + + if (m.Index - lastMatchIdx[argCnt] > 1000) fnSigLength[argCnt] = 0; + lastMatchIdx[argCnt] = m.Index; + var currentFnSigLength = m.Groups["fnName"].Length + m.Groups["fnParams"].Length + 2; + if (currentFnSigLength > fnSigLength[argCnt]) fnSigLength[argCnt] = currentFnSigLength; + return + $"{m.Groups["fnName"].Value}({m.Groups["fnParams"].Value})".PadLeft(fnSigLength[argCnt]) + + $" {{ {m.Groups["exportTarget"].Value}.exports = {m.Groups["exportImpl"].Value}; }},"; + }); + + return Encoding.UTF8.GetBytes(stringContent); + } + + /* + * 12345(e) { + * "use strict"; + * e.exports = "/abcdef.svg"; + * } + */ + [GeneratedRegex(@"(\d+)\((.)\) {(?:\n\s+""use strict"";)?\n\s+\2\.exports = ""([/a-z0-9\.]+)"";\n\s+},", + RegexOptions.Compiled)] + private static partial Regex SimpleSingleParamExportRegex(); + + /* + * 12345(e, t, n) { + * "use strict"; + * e.exports = n.p + "/abcdef.svg"; + * } + */ + [GeneratedRegex(""" + (?[a-zA-Z0-9]+)\((?[a-zA-Z0-9\s,]*)\) { + (?:\s+"use strict";)? + \s+(?[a-zA-Z0-9]+)\.exports = (?[^;]+?); + \s+}, + """, RegexOptions.Compiled)] + private static partial Regex SimpleMultiParamExportRegex(); +} \ No newline at end of file -- cgit 1.5.1