From ff7ec50a94cfa036d1472a9bbb3b8e77d96e60b5 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Fri, 4 Aug 2023 20:32:42 +1000 Subject: [PATCH] add some simple minification filters to preproc, which can shrink the kernel significantly --- build.lua | 2 +- preproc.lua | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/build.lua b/build.lua index 6bb1576..9f4de05 100644 --- a/build.lua +++ b/build.lua @@ -1,4 +1,4 @@ local preproc = require "preproc" ---local tA = {...} +preproc.minify = true preproc(...) diff --git a/preproc.lua b/preproc.lua index ba3bce7..8ec5e4b 100644 --- a/preproc.lua +++ b/preproc.lua @@ -82,11 +82,40 @@ function preproc.directives.includepkglib(package, file, name) -- string string return string.format("package.loaded['%s'] = (function()\n%s\nend)()", name, preproc.directives.includepkgfile(package, file)) end +local minify = true +local minifyFilters = { + {"%-%-%[%[.-%]%]",""}, + {"%-%-.-\n","\n"}, + {"\n[ \t]+","\n"}, + {"%s?%.%.%s?",".."}, + {"%s?==%s?","=="}, + {"%s?~=%s?","~="}, + {"%s?>=%s?",">="}, + {"%s?<=%s?","<="}, + {"%s?>%s?",">"}, + {"%s?<%s?","<"}, + {"%s?=%s?","="}, + {"%s?,%s?",","}, + {",\n",","}, + {"\n\n+","\n"}, + {"[ \t]\n","\n"}, + {"%{%s+","{"}, + {"%s+%}","}"} +} + return setmetatable(preproc,{__call=function(_,...) local tA = {...} local out = table.remove(tA,#tA) local f,e = io.open(out,"wb") if not f then error("unable to open file "..out..": "..e) end - f:write(preproc.preproc(table.unpack(tA))) + local out = preproc.preproc(table.unpack(tA)) + if preproc.minify then + local olen = #out + for k,v in ipairs(minifyFilters) do + out = out:gsub(v[1],v[2]) + end + print(olen, #out) + end + f:write(out) f:close() end})