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.

65 lines
2.8 KiB

7 years ago
7 years ago
7 years ago
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*", "**/*.lua", "**/*.cfg"],
  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("minify-lua-modules", "minifies lua modules", function () {
  21. const fs = require('fs');
  22. const luamin = require('luamin');
  23. const util = require('util');
  24. grunt.file.expand({filter: 'isDirectory'}, "dist/**").forEach(function (dir) {
  25. grunt.file.expand({filter: 'isFile'}, dir + "/*.lua").forEach(function (file) {
  26. const directory = file.substr(0, file.lastIndexOf('/'));
  27. const file_name = file.substr(file.lastIndexOf('/') + 1);
  28. const file_name_sans_ext = file_name.substr(0, file_name.lastIndexOf('.'));
  29. const lua = fs.readFileSync(file, 'utf8');
  30. const minified_contents = luamin.minify(lua);
  31. const minified_file_name = file_name_sans_ext + '.min.lua';
  32. const minified_file = directory + '/' + minified_file_name;
  33. fs.writeFileSync(minified_file, minified_contents);
  34. const file_stats = fs.statSync(file);
  35. const minified_file_stats = fs.statSync(minified_file);
  36. const saved_space = file_stats.size - minified_file_stats.size;
  37. const saved_space_percent = (100 * (1 - minified_file_stats.size / file_stats.size)).toFixed(2);
  38. 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));
  39. });
  40. });
  41. });
  42. grunt.registerTask("build-spm-bootstrap", "Make sure a single bootstrap file exists for SPM", function () {
  43. const fs = require('fs');
  44. grunt.file.expand({filter: 'isFile'}, "dist/**/bootstrap.min.lua").forEach(function (minified_file) {
  45. const directory = minified_file.substr(0, minified_file.lastIndexOf('/'));
  46. const minified_contents = fs.readFileSync(minified_file, 'utf8');
  47. fs.writeFileSync(directory + '/bootstrap.lua', minified_contents);
  48. fs.unlinkSync(minified_file);
  49. console.log("Removed unminified bootstrap file: " + directory + '/bootstrap.lua')
  50. });
  51. });
  52. grunt.registerTask("default", [
  53. "clean",
  54. "copy",
  55. "minify-lua-modules",
  56. "build-spm-bootstrap"
  57. ]);
  58. };