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

7 years ago
7 years ago
7 years ago
  1. module.exports = function (grunt) {
  2. "use strict";
  3. grunt.initConfig({
  4. clean: ["./dist", "./tmp"],
  5. copy: {
  6. build: {
  7. files: [{
  8. expand: true,
  9. cwd: "./src",
  10. src: ["**/license*"],
  11. dest: "dist/",
  12. filter: 'isFile'
  13. }]
  14. }
  15. }
  16. });
  17. grunt.loadNpmTasks('grunt-contrib-clean');
  18. grunt.loadNpmTasks("grunt-contrib-copy");
  19. grunt.loadNpmTasks('grunt-file-append');
  20. grunt.registerTask("add-rename-lua-module-tasks", "Finds and adds tasks to rename all main.lua to match their parent directory", function () {
  21. grunt.file.expand({filter: 'isDirectory'}, "src/*").forEach(function (dir) {
  22. var module_name = dir.substr(dir.lastIndexOf('/') + 1);
  23. if (module_name !== 'lib') {
  24. var copy_task = {};
  25. try {
  26. copy_task = grunt.config.get('copy');
  27. } catch (exception) {
  28. console.log("No copy config exists, generating empty one.");
  29. }
  30. copy_task[module_name] = {
  31. src: [dir + '/**/main.lua'],
  32. dest: 'dist/' + module_name + '/' + module_name + '.lua'
  33. };
  34. console.log("Added copy task for module [" + module_name + "]");
  35. grunt.config.set('copy', copy_task);
  36. }
  37. });
  38. });
  39. grunt.registerTask("minify-lua-modules", "minifies lua modules", function () {
  40. const fs = require('fs');
  41. const luamin = require('luamin');
  42. const util = require('util');
  43. grunt.file.expand({filter: 'isDirectory'}, "dist/*").forEach(function (dir) {
  44. grunt.file.expand({filter: 'isFile'}, dir + "/*.lua").forEach(function (file) {
  45. const directory = file.substr(0, file.lastIndexOf('/'));
  46. const file_name = file.substr(file.lastIndexOf('/') + 1);
  47. const file_name_sans_ext = file_name.substr(0, file_name.lastIndexOf('.'));
  48. const lua = fs.readFileSync(file, 'utf8');
  49. const minified_contents = luamin.minify(lua);
  50. const minified_file_name = file_name_sans_ext + '.min.lua';
  51. const minified_file = directory + '/' + minified_file_name;
  52. fs.writeFileSync(minified_file, minified_contents);
  53. const file_stats = fs.statSync(file);
  54. const minified_file_stats = fs.statSync(minified_file);
  55. const saved_space = file_stats.size - minified_file_stats.size;
  56. const saved_space_percent = (100 * (1 - minified_file_stats.size / file_stats.size)).toFixed(2);
  57. 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));
  58. });
  59. });
  60. });
  61. grunt.registerTask("default", [
  62. "clean",
  63. "add-rename-lua-module-tasks",
  64. "copy",
  65. "minify-lua-modules"
  66. ]);
  67. };