Skip to content

Commit beb7fd4

Browse files
committed
Add export plugin that minifies Lua scripts on release
1 parent 0eb4ae1 commit beb7fd4

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22
## [Unreleased]
3+
### Added
4+
- `EditorExportPlugin` for minifying Lua scripts with `LuaSrcDiet` on
5+
release exports. Minification may be turned off with the
6+
`Lua PluginScript/Export/Minify On Release Export` project setting.
7+
38
### Changed
49
- Release builds' init Lua script are minified with `LuaSrcDiet` and libraries
510
are now `strip`ed, resulting in smaller dynamic libraries

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,20 @@ In the `Project -> Project Settings...` window, some settings are available:
4343
- **Lua PluginScript/Package Path/Behavior**: Whether templates will replace
4444
[package.path](https://www.lua.org/manual/5.1/manual.html#pdf-package.path),
4545
be appended to it or prepended to it.
46-
Default behavior: replace.
46+
Default behavior: `replace`.
4747
- **Lua PluginScript/Package Path/Templates**: String array of templates to be
4848
injected into `package.path`.
4949
Default templates: `res://?.lua` and `res://?/init.lua`.
5050
- **Lua PluginScript/Package C Path/Behavior**: Whether templates will replace
5151
[package.cpath](https://www.lua.org/manual/5.1/manual.html#pdf-package.cpath),
5252
be appended to it or prepended to it.
53-
Default behavior: replace.
53+
Default behavior: `replace`.
5454
- **Lua PluginScript/Package C Path/Templates**: String array of templates to be
5555
injected into `package.cpath`.
5656
Default templates: `!/?.dll` and `!/loadall.dll` on Windows, `!/?.so` and `!/loadall.so` elsewhere.
57+
- **Lua PluginScript/Export/Minify On Release Export**: Whether Lua scritps
58+
should be minified on release exports.
59+
Defaults to `true`.
5760

5861
Also, an editor plugin is included, currently with a barebones REPL for Lua
5962
expressions, located in the bottom panel of the editor.
@@ -196,7 +199,7 @@ return MyClass
196199
- [X] API documentation
197200
- [ ] Unit tests
198201
- [ ] Example projects
199-
- [ ] Export plugin to minify Lua scripts
202+
- [X] Export plugin to minify Lua scripts
200203
- [X] Drop-in binary release in GitHub
201204
- [X] Submit to Asset Library
202205

plugin/export_plugin.lua

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
-- @file plugin/export_plugin.lua EditorExportPlugin for minifying Lua scripts on release exports
2+
-- This file is part of Godot Lua PluginScript: https://github.com/gilzoide/godot-lua-pluginscript
3+
--
4+
-- Copyright (C) 2021 Gil Barbosa Reis.
5+
--
6+
-- Permission is hereby granted, free of charge, to any person obtaining a copy
7+
-- of this software and associated documentation files (the “Software”), to
8+
-- deal in the Software without restriction, including without limitation the
9+
-- rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10+
-- sell copies of the Software, and to permit persons to whom the Software is
11+
-- furnished to do so, subject to the following conditions:
12+
--
13+
-- The above copyright notice and this permission notice shall be included in
14+
-- all copies or substantial portions of the Software.
15+
--
16+
-- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22+
-- IN THE SOFTWARE.
23+
local package_path = package.path
24+
package.path = 'res://addons/godot-lua-pluginscript/plugin/?.lua;res://addons/godot-lua-pluginscript/plugin/?/init.lua;' .. package_path
25+
local luasrcdiet = require 'luasrcdiet'
26+
package.path = package_path
27+
28+
local LuaExportPlugin = {
29+
is_tool = true,
30+
extends = 'EditorExportPlugin',
31+
}
32+
33+
local SHOULD_MINIFY_RELEASE_SETTING = 'lua_pluginscript/export/minify_on_release_export'
34+
35+
local function add_project_setting(name, initial_value)
36+
if not ProjectSettings:has_setting(name) then
37+
ProjectSettings:set_setting(name, initial_value)
38+
end
39+
ProjectSettings:set_initial_value(name, initial_value)
40+
end
41+
add_project_setting(SHOULD_MINIFY_RELEASE_SETTING, true)
42+
43+
function LuaExportPlugin:_export_begin(features, is_debug, path, flags)
44+
self.ignore_path = self:get_script().resource_path:get_base_dir()
45+
self.should_minify = not is_debug and ProjectSettings:get_setting(SHOULD_MINIFY_RELEASE_SETTING)
46+
self.file = File:new()
47+
end
48+
49+
function LuaExportPlugin:_export_file(path, type, features)
50+
if path:begins_with(self.ignore_path) then
51+
self:skip()
52+
elseif self.should_minify and path:ends_with('.lua') then
53+
if self.file:open(path, File.READ) == GD.OK then
54+
local source = tostring(self.file:get_as_text())
55+
self.file:close()
56+
local optsource = luasrcdiet.optimize(luasrcdiet.MAXIMUM_OPTS, source)
57+
print(string.format('[LuaPluginScript] Minified %s: %s -> %s (%d%% reduction)',
58+
path,
59+
String.humanize_size(#source),
60+
String.humanize_size(#optsource),
61+
100 - math.floor(#optsource / #source * 100))
62+
)
63+
self:add_file(path, PoolByteArray.from(optsource), false)
64+
end
65+
end
66+
end
67+
68+
function LuaExportPlugin:_export_end()
69+
self.ignore_path = nil
70+
self.file = nil
71+
end
72+
73+
return LuaExportPlugin

plugin/plugin.gd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,22 @@ tool
2424
extends EditorPlugin
2525

2626
var _lua_repl
27+
var _lua_export_plugin
2728

2829
func _enter_tree() -> void:
2930
_lua_repl = preload("lua_repl.tscn").instance()
30-
var repl_button = add_control_to_bottom_panel(_lua_repl, "Lua REPL")
31+
add_control_to_bottom_panel(_lua_repl, "Lua REPL")
32+
33+
_lua_export_plugin = preload("export_plugin.lua").new()
34+
add_export_plugin(_lua_export_plugin)
3135

3236

3337
func _exit_tree() -> void:
3438
if _lua_repl:
3539
remove_control_from_bottom_panel(_lua_repl)
3640
_lua_repl.free()
3741
_lua_repl = null
42+
if _lua_export_plugin:
43+
remove_export_plugin(_lua_export_plugin)
44+
_lua_export_plugin.unreference()
45+
_lua_export_plugin = null

0 commit comments

Comments
 (0)