add some simple minification filters to preproc, which can shrink the kernel significantly

This commit is contained in:
Izaya 2023-08-04 20:32:42 +10:00
parent ad99c438b3
commit ff7ec50a94
2 changed files with 31 additions and 2 deletions

View File

@ -1,4 +1,4 @@
local preproc = require "preproc"
--local tA = {...}
preproc.minify = true
preproc(...)

View File

@ -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})