gulpfile.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. 'use strict';
  2. var argv = require('minimist')(process.argv.slice(2)),
  3. gulp = require('gulp'),
  4. header = require('gulp-header'),
  5. gutil = require('gulp-util'),
  6. ngAnnotate = require('gulp-ng-annotate'),
  7. compass = require('gulp-compass'),
  8. refresh = require('gulp-livereload'),
  9. prefix = require('gulp-autoprefixer'),
  10. minifyCss = require('gulp-minify-css'),
  11. uglify = require('gulp-uglify'),
  12. clean = require('gulp-rimraf'),
  13. concat = require('gulp-concat-util'),
  14. express = require('express'),
  15. express_lr = require('connect-livereload'),
  16. tinylr = require('tiny-lr'),
  17. opn = require('opn'),
  18. jshint = require('gulp-jshint'),
  19. jshintStylish= require('jshint-stylish'),
  20. pkg = require('./package.json'),
  21. lr,
  22. refresh_lr;
  23. var today = new Date();
  24. // Configuration
  25. var Config = {
  26. port: 9000,
  27. livereloadPort: 35728,
  28. testPage: 'test/ng-img-crop.html',
  29. cache: (typeof argv.cache !== 'undefined' ? !!argv.cache : true),
  30. paths: {
  31. source: {
  32. root: 'source',
  33. js: 'source/js',
  34. scss: 'source/scss'
  35. },
  36. compileUnminified: {
  37. root: 'compile/unminified',
  38. js: 'compile/unminified',
  39. css: 'compile/unminified'
  40. },
  41. compileMinified: {
  42. root: 'compile/minified',
  43. js: 'compile/minified',
  44. css: 'compile/minified'
  45. }
  46. },
  47. banners: {
  48. unminified: '/*!\n' +
  49. ' * ' + pkg.prettyName + ' v' + pkg.version + '\n' +
  50. ' * ' + pkg.homepage + '\n' +
  51. ' *\n' +
  52. ' * Copyright (c) ' + (today.getFullYear()) + ' ' + pkg.author.name +'\n' +
  53. ' * License: ' + pkg.license + '\n' +
  54. ' *\n' +
  55. ' * Generated at ' + gutil.date(today, 'dddd, mmmm dS, yyyy, h:MM:ss TT') + '\n' +
  56. ' */',
  57. minified: '/*! ' + pkg.prettyName + ' v' + pkg.version + ' License: ' + pkg.license + ' */'
  58. }
  59. };
  60. // Tasks
  61. // =====
  62. // Compile Styles
  63. gulp.task('styles', function(){
  64. return gulp.src(Config.paths.source.scss + '/'+pkg.name+'.scss')
  65. .pipe(compass({
  66. sass: Config.paths.source.scss,
  67. css: Config.paths.compileUnminified.css,
  68. errLogToConsole: true
  69. }))
  70. .pipe(prefix('last 2 version', '> 5%', 'safari 5', 'ie 8', 'ie 7', 'opera 12.1', 'ios 6', 'android 4'))
  71. .pipe(gulp.dest(Config.paths.compileUnminified.css));
  72. });
  73. // Compile Scripts
  74. gulp.task('scripts', function(){
  75. return gulp.src([
  76. Config.paths.source.js + '/init.js',
  77. Config.paths.source.js + '/classes/*.js',
  78. Config.paths.source.js + '/ng-img-crop.js'
  79. ])
  80. .pipe(concat(pkg.name+'.js', {
  81. separator: '\n\n',
  82. process: function(src) {
  83. // Remove all 'use strict'; from the code and
  84. // replaces all double blank lines with one
  85. return src.replace(/\r\n/g, '\n')
  86. .replace(/'use strict';\n+/g, '')
  87. .replace(/\n\n\s*\n/g, '\n\n');
  88. }
  89. }))
  90. .pipe(concat.header(Config.banners.unminified + '\n' +
  91. '(function() {\n\'use strict\';\n\n'))
  92. .pipe(concat.footer('\n}());'))
  93. .pipe(gulp.dest(Config.paths.compileUnminified.js));
  94. });
  95. // Make a Distrib
  96. gulp.task('dist:js:clean', function(){
  97. return gulp.src([Config.paths.compileMinified.root + '/**/*.js'], { read: false })
  98. .pipe(clean());
  99. });
  100. gulp.task('dist:css:clean', function(){
  101. return gulp.src([Config.paths.compileMinified.root + '/**/*.css'], { read: false })
  102. .pipe(clean());
  103. });
  104. gulp.task('dist:js', ['dist:js:clean', 'scripts'], function(){
  105. return gulp.src(Config.paths.compileUnminified.js + '/**/*.js')
  106. .pipe(ngAnnotate())
  107. .pipe(uglify())
  108. .pipe(header(Config.banners.minified))
  109. .pipe(gulp.dest(Config.paths.compileMinified.js));
  110. });
  111. gulp.task('dist:css', ['dist:css:clean', 'styles'], function(){
  112. return gulp.src(Config.paths.compileUnminified.css + '/**/*.css')
  113. .pipe(minifyCss())
  114. .pipe(gulp.dest(Config.paths.compileMinified.css));
  115. });
  116. // Server
  117. gulp.task('server', function(){
  118. express()
  119. .use(express_lr())
  120. .use(express.static('.'))
  121. .listen(Config.port);
  122. gutil.log('Server listening on port ' + Config.port);
  123. });
  124. // LiveReload
  125. gulp.task('livereload', function(){
  126. lr = tinylr();
  127. lr.listen(Config.livereloadPort, function(err) {
  128. if(err) {
  129. gutil.log('Livereload error:', err);
  130. }
  131. });
  132. refresh_lr=refresh(lr);
  133. });
  134. // Watches
  135. gulp.task('watch', function(){
  136. gulp.watch(Config.paths.source.scss + '/**/*.scss', ['styles']);
  137. gulp.watch([Config.paths.source.js + '/**/*.js'], ['scripts']);
  138. gulp.watch([
  139. Config.paths.compileUnminified.css + '/**/*.css',
  140. Config.paths.compileUnminified.js + '/**/*.js',
  141. Config.testPage
  142. ], function(evt){
  143. refresh_lr.changed(evt.path);
  144. });
  145. });
  146. // User commands
  147. // =============
  148. // Code linter
  149. gulp.task('lint', function() {
  150. return gulp.src(Config.paths.source.js + '/**/*.js')
  151. .pipe(jshint())
  152. .pipe(jshint.reporter(jshintStylish));
  153. });
  154. // Build
  155. gulp.task('build', ['dist:js', 'dist:css']);
  156. // Start server and watch for changes
  157. gulp.task('default', ['server', 'livereload', 'styles', 'scripts', 'watch'], function(){
  158. // use the -o arg to open the test page in the browser
  159. if(argv.o) {
  160. opn('http://localhost:' + Config.port+'/'+Config.testPage);
  161. }
  162. });