Gruntfile.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. *
  3. * Run 'grunt' to generate JS and CSS in folder 'dist' and site in folder '_site'
  4. * *
  5. * Run 'grunt watch' to automatically regenerate '_site' when you change files in 'src' or in 'website'
  6. *
  7. */
  8. module.exports = function(grunt) {
  9. 'use strict';
  10. var jekyllConfig = "isLocal : false \r\n"+
  11. "permalink: /:title/ \r\n"+
  12. "exclude: ['.json', '.rvmrc', '.rbenv-version', 'README.md', 'Rakefile'," +
  13. "'changelog.md', 'compiler.jar', 'private', '.htaccess'," +
  14. "'photoswipe.sublime-project', 'photoswipe.sublime-workspace'] \r\n"+
  15. "auto: true \r\n"+
  16. "pswpversion: <%= pkg.version %> \r\n"+
  17. "siteversion: 1.0.4 \r\n"+
  18. "markdown: redcarpet \r\n"+
  19. "kramdown: \r\n"+
  20. " input: GFM \r\n";
  21. var awsDefaults = {};
  22. if( grunt.file.exists('./aws-keys.json') ) {
  23. awsDefaults = grunt.file.readJSON('./aws-keys.json');
  24. }
  25. grunt.initConfig({
  26. pkg: grunt.file.readJSON('photoswipe.json'),
  27. banner: '/*! PhotoSwipe - v<%= pkg.version %> - ' +
  28. '<%= grunt.template.today("yyyy-mm-dd") %>\n' +
  29. '<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
  30. '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>; */\n',
  31. defaultUIBanner: '/*! PhotoSwipe Default UI - <%= pkg.version %> - ' +
  32. '<%= grunt.template.today("yyyy-mm-dd") %>\n' +
  33. '<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
  34. '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>; */\n',
  35. // Task configuration.
  36. clean: {
  37. files: ['dist']
  38. },
  39. sass: {
  40. dist: {
  41. files: {
  42. 'dist/photoswipe.css': 'src/css/main.scss',
  43. 'dist/default-skin/default-skin.css': 'src/css/default-skin/default-skin.scss'
  44. }
  45. }
  46. },
  47. // https://github.com/nDmitry/grunt-autoprefixer
  48. autoprefixer: {
  49. options: {
  50. browsers: ['last 3 versions', 'android 3', 'ie 9', 'bb 10']
  51. },
  52. no_dest: {
  53. src: ['dist/photoswipe.css', 'dist/default-skin/default-skin.css']
  54. }
  55. },
  56. jshint: {
  57. all: [
  58. 'Gruntfile.js',
  59. 'dist/photoswipe.js',
  60. 'dist/photoswipe-ui-default.js'
  61. ],
  62. options: {
  63. jshintrc: '.jshintrc'
  64. }
  65. },
  66. pswpbuild: {
  67. all: {
  68. src: [
  69. 'framework-bridge',
  70. 'core',
  71. 'gestures',
  72. 'show-hide-transition',
  73. 'items-controller',
  74. 'tap',
  75. 'desktop-zoom',
  76. 'history'
  77. ],
  78. basePath: 'src/js/',
  79. dest: 'dist/photoswipe.js',
  80. uidest: 'dist/photoswipe-ui-default.js',
  81. banner: '<%= banner %>',
  82. defaultUIBanner: '<%= defaultUIBanner %>'
  83. }
  84. },
  85. jekyll: {
  86. dev: {
  87. options: {
  88. src: 'website',
  89. dest: '_site',
  90. url: 'local',
  91. raw: jekyllConfig + "url: local"
  92. }
  93. },
  94. production: {
  95. options: {
  96. src: 'website',
  97. dest: '_production',
  98. url: 'production',
  99. raw: jekyllConfig + "url: production"
  100. }
  101. }
  102. },
  103. copy: {
  104. dev: {
  105. files: [
  106. {src: ['src/css/default-skin/default-skin.svg'], dest: 'dist/default-skin/default-skin.svg'},
  107. {src: ['src/css/default-skin/default-skin.png'], dest: 'dist/default-skin/default-skin.png'},
  108. {src: ['src/css/default-skin/preloader.gif'], dest: 'dist/default-skin/preloader.gif'},
  109. {expand: true, src: ['dist/**'], dest: '_site/'}
  110. ]
  111. },
  112. main: {
  113. files: [
  114. {expand: true, src: ['dist/**'], dest: 'website/'}
  115. ]
  116. }
  117. },
  118. uglify: {
  119. my_target: {
  120. files: {
  121. 'dist/photoswipe.min.js': ['dist/photoswipe.js'],
  122. 'dist/photoswipe-ui-default.min.js': ['dist/photoswipe-ui-default.js']
  123. },
  124. preserveComments: /^!/i
  125. },
  126. options: {
  127. preserveComments: /^!/i
  128. }
  129. },
  130. watch: { // for development run 'grunt watch'
  131. jekyll: {
  132. files: ['website/**', 'website/documentation/**', '_includes/**'],
  133. tasks: ['jekyll:dev', 'copy:dev']
  134. },
  135. files: ['src/**'],
  136. tasks: [ 'sass', 'autoprefixer', 'pswpbuild', 'copy:dev', 'uglify']
  137. },
  138. cssmin: {
  139. compress: {
  140. files: {
  141. "website/site-assets/all.min.css": ["website/site-assets/site.css", "website/dist/photoswipe.css"]
  142. }
  143. }
  144. },
  145. svgmin: {
  146. dist: {
  147. files: {
  148. 'src/css/default-skin/default-skin.svg': 'src/css/default-skin/default-skin.svg'
  149. }
  150. }
  151. },
  152. aws_s3: {
  153. options: {
  154. accessKeyId: awsDefaults ? awsDefaults.AWSAccessKeyId : '', // Use the variables
  155. secretAccessKey: awsDefaults ? awsDefaults.AWSSecretKey : '', // You can also use env variables
  156. region: 'eu-west-1',
  157. uploadConcurrency: 5, // 5 simultaneous uploads
  158. downloadConcurrency: 5 // 5 simultaneous downloads
  159. },
  160. main: {
  161. options: {
  162. bucket: 'photoswipe'
  163. },
  164. files: [
  165. { expand: true, cwd: 'dist/', src: ['**'], dest: 'pswp/dist/', params: {CacheControl: 'max-age=86400'} }
  166. ]
  167. }
  168. }
  169. });
  170. // grunt pswpbuild --pswp-exclude=ajax,image
  171. grunt.task.registerMultiTask('pswpbuild', 'Makes PhotoSwipe core JS file.', function() {
  172. var files = this.data.src,
  173. includes = grunt.option('pswp-exclude'),
  174. basePath = this.data.basePath,
  175. newContents = this.data.banner;
  176. newContents += "(function (root, factory) { \n"+
  177. "\tif (typeof define === 'function' && define.amd) {\n" +
  178. "\t\tdefine(factory);\n" +
  179. "\t} else if (typeof exports === 'object') {\n" +
  180. "\t\tmodule.exports = factory();\n" +
  181. "\t} else {\n" +
  182. "\t\troot.PhotoSwipe = factory();\n" +
  183. "\t}\n" +
  184. "})(this, function () {\n\n" +
  185. "\t'use strict';\n"+
  186. "\tvar PhotoSwipe = function(template, UiClass, items, options){\n";
  187. if(includes) {
  188. includes = includes.split(/[\s,]+/); // 'a,b,c' => ['a','b','c']
  189. var removeA = function (arr) {
  190. var what, a = arguments, L = a.length, ax;
  191. while (L > 1 && arr.length) {
  192. what = a[--L];
  193. while ((ax= arr.indexOf(what)) !== -1) {
  194. arr.splice(ax, 1);
  195. }
  196. }
  197. return arr;
  198. };
  199. includes.forEach(function( name ) {
  200. if(name) {
  201. grunt.log.writeln( 'removed "'+name +'"' );
  202. files = removeA(files, name);
  203. }
  204. });
  205. }
  206. grunt.log.writeln( 'Your build is made of:'+files );
  207. files.forEach(function( name ) {
  208. // Wrap each module with a pience of code to be able to exlude it, stolen for modernizr.com
  209. newContents += "\n/*>>"+name+"*/\n";
  210. newContents += grunt.file.read( basePath + name + '.js' ) + '\n';
  211. newContents += "\n/*>>"+name+"*/\n";
  212. });
  213. newContents+= "\tframework.extend(self, publicMethods); };\n";
  214. newContents+= "\treturn PhotoSwipe;\n";
  215. newContents+= "});";
  216. grunt.file.write( this.data.dest, newContents );
  217. var uiContents = grunt.file.read( basePath + 'ui/photoswipe-ui-default.js' );
  218. uiContents = this.data.defaultUIBanner + uiContents;
  219. grunt.file.write( this.data.uidest, uiContents );
  220. });
  221. // These plugins provide necessary tasks.
  222. grunt.loadNpmTasks('grunt-contrib-clean');
  223. grunt.loadNpmTasks('grunt-contrib-concat');
  224. grunt.loadNpmTasks('grunt-contrib-uglify');
  225. grunt.loadNpmTasks('grunt-contrib-jshint');
  226. grunt.loadNpmTasks('grunt-contrib-watch');
  227. grunt.loadNpmTasks('grunt-sass');
  228. grunt.loadNpmTasks('grunt-autoprefixer');
  229. grunt.loadNpmTasks('grunt-contrib-copy');
  230. grunt.loadNpmTasks('grunt-jekyll');
  231. grunt.loadNpmTasks('grunt-contrib-cssmin');
  232. grunt.loadNpmTasks('grunt-aws-s3');
  233. grunt.loadNpmTasks('grunt-svgmin');
  234. // Default task.
  235. grunt.registerTask('default', ['sass', 'autoprefixer', 'pswpbuild','uglify', 'copy', 'jekyll:dev']);
  236. grunt.registerTask('production', ['sass', 'autoprefixer', 'pswpbuild', 'uglify', 'copy', 'cssmin', 'jekyll:production']);
  237. grunt.registerTask('nosite', ['sass', 'autoprefixer', 'pswpbuild', 'uglify']);
  238. grunt.registerTask('hint', ['jshint']);
  239. grunt.registerTask('awsupload', ['aws_s3']);
  240. };