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.
 
 
 

74 lines
3.1 KiB

module.exports = function (grunt) {
"use strict";
grunt.initConfig({
clean: ["./dist", "./tmp"],
copy: {
build: {
files: [{
expand: true,
cwd: "./src",
src: ["**/license*"],
dest: "dist/",
filter: 'isFile'
}]
}
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.loadNpmTasks('grunt-file-append');
grunt.registerTask("add-rename-lua-module-tasks", "Finds and adds tasks to rename all main.lua to match their parent directory", function () {
grunt.file.expand({filter: 'isDirectory'}, "src/*").forEach(function (dir) {
var module_name = dir.substr(dir.lastIndexOf('/') + 1);
if (module_name !== 'lib') {
var copy_task = {};
try {
copy_task = grunt.config.get('copy');
} catch (exception) {
console.log("No copy config exists, generating empty one.");
}
copy_task[module_name] = {
src: [dir + '/**/main.lua'],
dest: 'dist/' + module_name + '/' + module_name + '.lua'
};
console.log("Added copy task for module [" + module_name + "]");
grunt.config.set('copy', copy_task);
}
});
});
grunt.registerTask("minify-lua-modules", "minifies lua modules", function () {
const fs = require('fs');
const luamin = require('luamin');
const util = require('util');
grunt.file.expand({filter: 'isDirectory'}, "dist/*").forEach(function (dir) {
grunt.file.expand({filter: 'isFile'}, dir + "/*.lua").forEach(function (file) {
const directory = file.substr(0, file.lastIndexOf('/'));
const file_name = file.substr(file.lastIndexOf('/') + 1);
const file_name_sans_ext = file_name.substr(0, file_name.lastIndexOf('.'));
const lua = fs.readFileSync(file, 'utf8');
const minified_contents = luamin.minify(lua);
const minified_file_name = file_name_sans_ext + '.min.lua';
const minified_file = directory + '/' + minified_file_name;
fs.writeFileSync(minified_file, minified_contents);
const file_stats = fs.statSync(file);
const minified_file_stats = fs.statSync(minified_file);
const saved_space = file_stats.size - minified_file_stats.size;
const saved_space_percent = (100 * (1 - minified_file_stats.size / file_stats.size)).toFixed(2);
console.log(util.format("Minified: %s: %f KB -> %s: %f KB. SAVED %d%% %d KB", file_name, file_stats.size / 1000, minified_file_name, minified_file_stats.size / 1000, saved_space_percent, saved_space / 1000));
});
});
});
grunt.registerTask("default", [
"clean",
"add-rename-lua-module-tasks",
"copy",
"minify-lua-modules"
]);
};