-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathminify.lua
340 lines (311 loc) · 7.35 KB
/
minify.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
--[[
Introduction and details :
Copyright Conor Mcknight
https://github.com/C0nw0nk/Nginx-Lua-minification-library
Disclaimer :
I am not responsible for what you do with this script nor liable,
This script was released under default Copyright Law as a proof of concept.
For those who want to know what that means for your use of this script read the following : http://choosealicense.com/no-license/
Information :
I built this script to compress and keep the specified mime types outputs small and minify the bandwidth that my servers have to use when serving these files to users.
If you have any bugs issues or problems just post a Issue request. https://github.com/C0nw0nk/Nginx-Lua-minification-library/issues
If you fork or make any changes to improve this or fix problems please do make a pull request for the community who also use this. https://github.com/C0nw0nk/Nginx-Lua-minification-library/pulls
Usage :
Add this to your Nginx configuration folder.
nginx/conf/lua/minify
Once installed into your nginx/conf/ folder.
Add this to your HTTP block or it can be in a server or location block depending where you want this script to run for individual locations the entire server or every single website on the server.
header_filter_by_lua_file conf/lua/minify/minify_header.lua
body_filter_by_lua_file conf/lua/minify/minify.lua;
Example nginx.conf :
This will run for all websites on the nginx server
http {
#nginx config settings etc
header_filter_by_lua_file conf/lua/minify/minify_header.lua
body_filter_by_lua_file conf/lua/minify/minify.lua;
#more config settings and some server stuff
}
This will make it run for this website only
server {
#nginx config settings etc
header_filter_by_lua_file conf/lua/minify/minify_header.lua
body_filter_by_lua_file conf/lua/minify/minify.lua;
#more config settings and some server stuff
}
This will run in this location block only
location / {
#nginx config settings etc
header_filter_by_lua_file conf/lua/minify/minify_header.lua
body_filter_by_lua_file conf/lua/minify/minify.lua;
#more config settings and some server stuff
}
]]
--[[
Settings used to modify and compress each invidual mime type output you specify on the fly.
I decided to make it as easy to use and customisable as possible to help the community that will use this.
]]
local content_type_list = {
{
"text/html",
{
--[[
Usage :
Regex, Replacement
Text, Replacement
]]
--Example :
--{"replace me", " with me! ",},
{"<!--[^>]-->", "",}, --remove nulled out html example !! I DO NOT RECOMMEND REMOVING COMMENTS, THIS COULD BREAK YOUR ENTIRE WEBSITE FOR OLD BROWSERS, BE AWARE
{"(//[^.*]*.\n)", "",}, -- Example: this //will remove //comments (result: this remove)
{"(/%*[^*]*%*/)", "",}, -- Example: this /*will*/ remove /*comments*/ (result: this remove)
{"<style>(.*)%/%*(.*)%*%/(.*)</style>", "<style>%1%3</style>",},
{"<script>(.*)%/%*(.*)%*%/(.*)</script>", "<script>%1%3</script>",},
{"%s%s+", "",}, --remove blank characters from html
{"[ \t]+$", "",}, --remove break lines (execution order of regex matters keep this last)
}
},
{
"text/css",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
--javascript has allot of different mime types i don't know why!?
{
"application/javascript",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*.*[^*].*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"application/ecmascript",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"application/x-ecmascript",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"application/x-javascript",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"text/ecmascript",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"text/javascript",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"text/javascript1.0",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"text/javascript1.1",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"text/javascript1.2",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"text/javascript1.3",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"text/javascript1.4",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"text/javascript1.5",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"text/jscript",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"text/livescript",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"text/x-ecmascript",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
{
"text/x-javascript",
{
--[[
Usage :
Regex, Replacement
]]
{"(//[^.*]*.\n)", "",},
{"(/%*[^*]*%*/)", "",},
{"%s%s+", "",},
{"[ \t]+$", "",},
}
},
}
--[[
DO NOT TOUCH ANYTHING BELOW THIS POINT UNLESS YOU KNOW WHAT YOU ARE DOING.
^^^^^ YOU WILL MOST LIKELY BREAK THE SCRIPT SO TO CONFIGURE THE FEATURES YOU WANT JUST USE WHAT I GAVE YOU ABOVE. ^^^^^
THIS BLOCK IS ENTIRELY WRITTEN IN CAPS LOCK TO SHOW YOU HOW SERIOUS I AM.
]]
local content_type = ngx.header["content-type"]
for key, value in ipairs(content_type_list) do
if string.find(content_type, ";") ~= nil then -- Check if content-type has charset config
content_type = string.match(content_type, "(.*)%;(.*)") --Split ;charset from header
end
if string.match(value[1], content_type) then
for k, v in ipairs(value[2]) do
local output = ngx.arg[1]
local output_minified = output
output_minified, occurrences, errors = string.gsub(output_minified, v[1], v[2])
if output_minified then
--Modify the output to replace it with our minified output
ngx.arg[1] = output_minified
end
end --end foreach regex check
end --end content_type if check
end --end content_type foreach mime type table check