65 lines
2.5 KiB

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. var fs = require('fs');
  41. var luamin = require('luamin');
  42. grunt.file.expand({filter: 'isDirectory'}, "dist/*").forEach(function (dir) {
  43. grunt.file.expand({filter: 'isFile'}, dir + "/*.lua").forEach(function (file) {
  44. var directory = file.substr(0, file.lastIndexOf('/'));
  45. var file_name = file.substr(file.lastIndexOf('/') + 1, file.lastIndexOf('.'));
  46. var lua = fs.readFileSync(file, 'utf8');
  47. var minified = luamin.minify(lua);
  48. var minified_file_name = file_name + '.min.lua';
  49. fs.writeFileSync(directory + '/' + minified_file_name, minified);
  50. console.log("Minified: " + file_name + ' -> ' + minified_file_name);
  51. });
  52. });
  53. });
  54. grunt.registerTask("default", [
  55. "clean",
  56. "add-rename-lua-module-tasks",
  57. "copy",
  58. "minify-lua-modules"
  59. ]);
  60. };