diff options
author | Camil Staps | 2015-02-17 15:31:56 +0100 |
---|---|---|
committer | Camil Staps | 2015-02-17 15:31:56 +0100 |
commit | 4d2c34e36b2573cbca7eca5f8b186f20590c0ae3 (patch) | |
tree | 6146588728bd2b8a4f6260717d188d465833d15d | |
parent | Added gulp default task (diff) |
Added bootswatch gulp stuff
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | gulpfile.js | 135 | ||||
-rw-r--r-- | package.json | 12 |
3 files changed, 104 insertions, 44 deletions
@@ -1,2 +1,3 @@ bower_components/* node_modules/* +assets/* diff --git a/gulpfile.js b/gulpfile.js index b37e899..2030db9 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,51 +1,100 @@ -// include gulp -var gulp = require('gulp'); - -// include plugins -var jshint = require('gulp-jshint'); -var changed = require('gulp-changed'); -var minifyHTML = require('gulp-minify-html'); -var concat = require('gulp-concat'); -var stripDebug = require('gulp-strip-debug'); -var uglify = require('gulp-uglify'); -var autoprefix = require('gulp-autoprefixer'); -var minifyCSS = require('gulp-minify-css'); - -// Default task -gulp.task('default', ['jshint', 'htmlpage', 'scripts', 'styles'], function(){ -}); +// see https://github.com/tugberkugurlu/gulp-bootswatch-sample/ + +var gulp = require('gulp'), + minifycss = require('gulp-minify-css'), + concat = require('gulp-concat'), + less = require('gulp-less'), + gulpif = require('gulp-if'), + order = require('gulp-order'), + gutil = require('gulp-util'), + rename = require('gulp-rename'), + foreach = require('gulp-foreach'), + debug = require('gulp-debug'), + path =require('path'), + merge = require('merge-stream'), + del = require('del'); -// JS hint task -gulp.task('jshint', function(){ - gulp.src('./src/scripts/*.js') - .pipe(jshint()) - .pipe(jshint.reporter('default')); +var bootswatch_theme = 'superhero'; + +gulp.task('default', ['clean'], function() { + gulp.start('fonts', 'styles'); }); -// minify new/changed HTML -gulp.task('htmlpage', function(){ - var src = './src/*.html'; - var dst = './build'; - gulp.src(src) - .pipe(changed(dst)) - .pipe(minifyHTML()) - .pipe(gulp.dest(dst)); +gulp.task('clean', function(cb) { + del(['assets/css', 'assets/js', 'assets/less', 'assets/img', 'assets/fonts'], cb) }); -// JS concat, strip debugging and minify -gulp.task('scripts', function(){ - gulp.src(['./src/scripts/lib.js', './src/scripts/*.js']) - .pipe(concat('script.js')) - .pipe(stripDebug()) - .pipe(uglify()) - .pipe(gulp.dest('./build/scripts')); +gulp.task('fonts', function() { + + var fileList = [ + 'bower_components/bootstrap/dist/fonts/*', + 'bower_components/fontawesome/fonts/*' + ]; + + return gulp.src(fileList) + .pipe(gulp.dest('assets/fonts')); }); -// CSS concat, auto-prefix and minify -gulp.task('styles', function(){ - gulp.src(['./src/styles/*.css']) - .pipe(concat('styles.css')) - .pipe(autoprefix('last 2 versions')) - .pipe(minifyCSS()) - .pipe(gulp.dest('./build/styles/')); +gulp.task('styles', function() { + + var baseContent = '@import "bower_components/bootstrap/less/bootstrap.less";@import "bower_components/bootswatch/' + bootswatch_theme + '/variables.less";@import "bower_components/bootswatch/' + bootswatch_theme + '/bootswatch.less";@import "bower_components/bootstrap/less/utilities.less";'; + var isBootswatchFile = function(file) { + var suffix = 'bootswatch.less'; + return file.path.indexOf(suffix, file.path.length - suffix.length) !== -1; + } + + var isBootstrapFile = function(file) { + var suffix = 'bootstrap-', + fileName = path.basename(file.path); + + return fileName.indexOf(suffix) == 0; + } + + var fileList = [ + 'client/less/main.less', + 'bower_components/bootswatch/**/bootswatch.less', + 'bower_components/fontawesome/css/font-awesome.css' + ]; + + return gulp.src(fileList) + .pipe(gulpif(isBootswatchFile, foreach(function(stream, file) { + var themeName = path.basename(path.dirname(file.path)), + content = replaceAll(baseContent, bootswatch_theme, themeName), + file = string_src('bootstrap-' + themeName + '.less', content); + + return file; + }))) + .pipe(less()) + .pipe(gulp.dest('assets/css')) + .pipe(gulpif(isBootstrapFile, foreach(function(stream, file) { + var fileName = path.basename(file.path), + themeName = fileName.substring(fileName.indexOf('-') + 1, fileName.indexOf('.')); + + // http://stackoverflow.com/questions/21719833/gulp-how-to-add-src-files-in-the-middle-of-a-pipe + // https://github.com/gulpjs/gulp/blob/master/docs/recipes/using-multiple-sources-in-one-task.md + return merge(stream, gulp.src(['assets/css/font-awesome.css', 'assets/css/main.css'])) + .pipe(concat('style-' + themeName + ".css")) + .pipe(gulp.dest('assets/css')) + .pipe(rename({suffix: '.min'})) + .pipe(minifycss()) + .pipe(gulp.dest('assets/css')); + }))) }); + +// http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript +function escapeRegExp(string) { + return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); +} + +function replaceAll(string, find, replace) { + return string.replace(new RegExp(escapeRegExp(find), 'g'), replace); +} + +function string_src(filename, string) { + var src = require('stream').Readable({ objectMode: true }) + src._read = function () { + this.push(new gutil.File({ cwd: "", base: "", path: filename, contents: new Buffer(string) })) + this.push(null) + } + return src +}
\ No newline at end of file diff --git a/package.json b/package.json index e5ab04e..f6d5fa9 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,17 @@ "gulp-strip-debug": "^1.0.2", "gulp-uglify": "^1.1.0" }, - "devDependencies": {}, + "devDependencies": { + "del": "^1.1.1", + "gulp-debug": "^2.0.0", + "gulp-foreach": "^0.1.0", + "gulp-if": "^1.2.5", + "gulp-less": "^3.0.0", + "gulp-order": "^1.1.1", + "gulp-rename": "^1.2.0", + "gulp-util": "^3.0.3", + "merge-stream": "^0.1.7" + }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, |