Proof-of-concept Server-Sent CSMs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

minify.lua 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. --
  2. -- A primitive code minifier
  3. --
  4. -- © 2019 by luk3yx
  5. --
  6. return function(code)
  7. assert(type(code) == 'string')
  8. local res, last, ws1, ws2, escape = '', false, '\n', '\n', false
  9. local sp = {['"'] = true, ["'"] = true, ['['] = true}
  10. for i = 1, #code do
  11. local char = code:sub(i, i)
  12. if char == '\r' then char = '\n' end
  13. if last == '--' or last == '--.' or last == '--[' then
  14. ws1 = ws2
  15. if char == '\n' then
  16. if ws1 ~= '\n' then res = res .. '\n' end
  17. last = false
  18. ws1 = '\n'
  19. elseif char == '[' and last ~= '--.' then
  20. last = last .. char
  21. else
  22. last = '--.'
  23. end
  24. elseif last == '--[[' or last == '-]' then
  25. ws1 = ws2
  26. if last == '-]' then
  27. if char == ']' then
  28. last = false
  29. else
  30. last = '--[['
  31. end
  32. elseif char == ']' then
  33. last = '-]'
  34. end
  35. elseif last == '[[' or last == ']' then
  36. if last == ']' then
  37. if char == ']' then
  38. last = false
  39. else
  40. last = '[['
  41. end
  42. elseif char == ']' then
  43. last = ']'
  44. end
  45. res = res .. char
  46. elseif escape then
  47. res = res .. '\\' .. char
  48. escape = false
  49. elseif char == '\\' then
  50. escape = true
  51. elseif last == '"' or last == "'" then
  52. if char == last then last = false end
  53. res = res .. char
  54. elseif last == '[' then
  55. if char == last then
  56. last = last .. char
  57. else
  58. last = false
  59. end
  60. res = res .. char
  61. elseif last == '-' then
  62. if char == '-' then
  63. last, ws1 = '--', ws2
  64. else
  65. res = res .. '-' .. char
  66. last, ws1 = false, false
  67. end
  68. elseif char == '-' then
  69. last = char
  70. ws1 = ws2
  71. elseif char == '\n' then
  72. if ws2 == ' ' then
  73. res = res:sub(1, #res - 1) .. '\n'
  74. elseif ws2 ~= '\n' then
  75. res = res .. '\n'
  76. end
  77. ws1 = '\n'
  78. elseif char == ' ' or char == '\t' then
  79. if not ws2 then res = res .. ' ' end
  80. ws1 = ws2 or ' '
  81. else
  82. if sp[char] then last = char end
  83. res = res .. char
  84. end
  85. ws2 = ws1
  86. ws1 = false
  87. end
  88. return res
  89. end