Gruntfile.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. module.exports = function(grunt) {
  2. function createBanner(fileName) {
  3. return "/*!\n" +
  4. "* " + fileName + "\n" +
  5. "* http://github.com/RobinHerbots/jquery.inputmask\n" +
  6. "* Copyright (c) 2010 - <%= grunt.template.today('yyyy') %> <%= pkg.author.name %>\n" +
  7. "* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)\n" +
  8. "* Version: <%= pkg.version %>\n" +
  9. "*/\n";
  10. }
  11. function createUglifyConfig(path) {
  12. var uglifyConfig = {};
  13. var srcFiles = grunt.file.expand(path + "/*.js");
  14. for (var srcNdx in srcFiles) {
  15. var dstFile = srcFiles[srcNdx].replace("js/", ""),
  16. dstFileMin = dstFile.replace(".js", ".min.js");
  17. wrapModuleLoaders(srcFiles[srcNdx], "build/" + dstFile, dstFile.indexOf("jquery") == -1 ?
  18. (dstFile.indexOf("extension") == -1 ? ["jquery"] : ["jquery", "./inputmask"]) : ["jquery", "./inputmask"]);
  19. uglifyConfig[dstFile] = {
  20. dest: 'dist/inputmask/' + dstFile,
  21. src: "build/" + dstFile,
  22. options: {
  23. banner: createBanner(dstFile),
  24. beautify: true,
  25. mangle: false,
  26. preserveComments: "some",
  27. ASCIIOnly: true
  28. }
  29. };
  30. uglifyConfig[dstFileMin] = {
  31. dest: "dist/inputmask/" + dstFileMin,
  32. src: "build/" + dstFile,
  33. options: {
  34. banner: createBanner(dstFileMin),
  35. preserveComments: "some",
  36. ASCIIOnly: true
  37. }
  38. };
  39. }
  40. srcFiles = grunt.file.expand(path + "/*.extensions.js");
  41. srcFiles.splice(0, 0, "js/jquery.inputmask.js");
  42. srcFiles.splice(0, 0, "js/inputmask.js");
  43. uglifyConfig["inputmaskbundle"] = {
  44. files: {
  45. 'dist/<%= pkg.name %>.bundle.js': srcFiles
  46. },
  47. options: {
  48. banner: createBanner('<%= pkg.name %>.bundle'),
  49. beautify: true,
  50. mangle: false,
  51. preserveComments: "some",
  52. ASCIIOnly: true
  53. }
  54. }
  55. uglifyConfig["inputmaskbundlemin"] = {
  56. files: {
  57. 'dist/<%= pkg.name %>.bundle.min.js': srcFiles
  58. },
  59. options: {
  60. banner: createBanner('<%= pkg.name %>.bundle'),
  61. preserveComments: "some",
  62. ASCIIOnly: true
  63. }
  64. }
  65. return uglifyConfig;
  66. }
  67. function wrapModuleLoaders(src, dst, dependencies) {
  68. function stripClosureExecution() {
  69. return srcFile.replace(new RegExp("\\(jQuery\\)[\\s\\S]*$"), "");
  70. }
  71. function createCommonJsRequires(dependencies) {
  72. var res = [];
  73. dependencies.forEach(function(dep) {
  74. res.push("require('" + dep + "')");
  75. });
  76. return res.join(", ");
  77. }
  78. var srcFile = grunt.file.read(src),
  79. dstContent = "(function (factory) {" +
  80. "if (typeof define === 'function' && define.amd) {" +
  81. "define(" + JSON.stringify(dependencies) + ", factory);" +
  82. "} else if (typeof exports === 'object') {" +
  83. "module.exports = factory(" + createCommonJsRequires(dependencies) + ");" +
  84. "} else {" +
  85. "factory(jQuery);" +
  86. "}}\n" + stripClosureExecution() + ");";
  87. grunt.file.write(dst, dstContent);
  88. }
  89. // Project configuration.
  90. grunt.initConfig({
  91. pkg: grunt.file.readJSON('package.json'),
  92. uglify: createUglifyConfig("js"),
  93. clean: ["dist"],
  94. karma: {
  95. options: {
  96. configFile: 'karma.conf.js'
  97. },
  98. unit: {
  99. runnerPort: 9999,
  100. singleRun: true,
  101. // browsers: ['PhantomJS'], // The tests fail...
  102. logLevel: 'ERROR'
  103. }
  104. },
  105. bump: {
  106. options: {
  107. files: ['package.json', 'bower.json', 'composer.json', 'component.json'],
  108. updateConfigs: ['pkg'],
  109. commit: false,
  110. createTag: false,
  111. push: false
  112. }
  113. },
  114. release: {
  115. options: {
  116. bump: false,
  117. commitMessage: 'jquery.inputmask <%= version %>'
  118. }
  119. },
  120. nugetpack: {
  121. dist: {
  122. src: function() {
  123. return process.platform === "linux" ? 'nuspecs/jquery.inputmask.linux.nuspec' : 'nuspecs/jquery.inputmask.nuspec';
  124. }(),
  125. dest: 'dist/',
  126. options: {
  127. version: '<%= pkg.version %>'
  128. }
  129. }
  130. },
  131. nugetpush: {
  132. dist: {
  133. src: 'dist/jQuery.InputMask.<%= pkg.version %>.nupkg'
  134. }
  135. },
  136. shell: {
  137. options: {
  138. stderr: false
  139. },
  140. gitcommitchanges: {
  141. command: ['git add .',
  142. 'git reset -- package.json',
  143. 'git commit -m "jquery.inputmask <%= pkg.version %>"'
  144. ].join('&&')
  145. }
  146. },
  147. eslint: {
  148. target: grunt.file.expand("js/*.js")
  149. }
  150. });
  151. // Load the plugin that provides the tasks.
  152. require('load-grunt-tasks')(grunt);
  153. grunt.registerTask('publish:patch', ['clean', 'bump:patch', 'uglify', 'shell:gitcommitchanges', 'release', 'nugetpack', 'nugetpush']);
  154. grunt.registerTask('publish:minor', ['clean', 'bump:minor', 'uglify', 'shell:gitcommitchanges', 'release', 'nugetpack', 'nugetpush']);
  155. grunt.registerTask('publish:major', ['clean', 'bump:major', 'uglify', 'shell:gitcommitchanges', 'release', 'nugetpack', 'nugetpush']);
  156. grunt.registerTask('validate', ['eslint', 'karma']);
  157. // Default task(s).
  158. grunt.registerTask('default', ['bump:prerelease', 'clean', 'uglify']);
  159. };