Skip to content

Instantly share code, notes, and snippets.

@apollo13
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apollo13/14fb5836b6ffb0fe1048 to your computer and use it in GitHub Desktop.
Save apollo13/14fb5836b6ffb0fe1048 to your computer and use it in GitHub Desktop.
{
"name": "conticki",
"dependencies": {
"foundation": "~5.5.0"
},
"apps": {
"conticki": {
"app_concat.js": [
"jquery/dist/jquery.js",
"foundation/js/foundation.js",
"conticki/js/app.js"
],
"app_concat.css": [
"conticki/css/app.css"
]
}
},
"sassImportPaths": [
"foundation/scss"
],
"overrides": {
"fastclick": {"ignore": true},
"jquery.cookie": {"ignore": true},
"jquery-placeholder": {"ignore": true},
"foundation": {"main": "js/foundation.js"}
}
}
var gulp = require('gulp'),
bower = require('gulp-bower'),
sass = require('gulp-sass'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
mainBowerFiles = require('main-bower-files'),
concat = require('gulp-concat'),
es = require('event-stream'),
gulpFilter = require('gulp-filter'),
print = require('gulp-print'),
newer = require('gulp-newer'),
sourcemaps = require('gulp-sourcemaps'),
please = require('gulp-pleeease'),
lazypipe = require('lazypipe'),
order = require('gulp-order'),
gulpSequence = require('gulp-sequence');
var config = require('./bower.json'),
apps = config.apps,
app_keys = Object.keys(apps);
var libraryApp = config.name;
var sassImportPaths = config.sassImportPaths || [];
var componentDir = libraryApp + '/bower_components';
if (!String.prototype.endsWith) {
Object.defineProperty(String.prototype, 'endsWith', {
value: function(searchString, position) {
var subjectString = this.toString();
if (position === undefined || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
}
});
}
findSourceFiles = function(ext) {
// Collect all source files from static folders.
return es.concat.apply(null, app_keys.map(function(app) {
// Ignore concatenated and minimized files
var exclusions = findExclusionGlobs(ext);
s = gulp.src([app + '/static/**/*' + ext].concat(exclusions))
return s;
}));
};
findExclusionGlobs = function(ext) {
var exclusions = [];
for (app in apps) {
for (entry in apps[app]) {
if (entry.endsWith(ext)) {
exclusions.push('!' + app + '/static/' + entry);
}
}
}
exclusions.push('!**/*.min' + ext);
return exclusions
};
builder = function(ext, chain) {
var sources = findSourceFiles(ext);
var tasks = [];
for (app in apps) {
for (file in apps[app]) {
var targetDir = app + '/static/';
task = sources
// Only include wanted files
.pipe(gulpFilter(apps[app][file]))
.pipe(order(apps[app][file]))
// Don't continue if the concatenated version is newer
.pipe(newer(targetDir + file))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(concat(file))
// Save the unminified version
.pipe(gulp.dest(targetDir))
// Minimize and rename
.pipe(chain())
.pipe(rename({extname: '.min' + ext}))
.pipe(sourcemaps.write('maps'))
.pipe(gulp.dest(targetDir))
// Filter out map files
.pipe(gulpFilter(['**/*' + ext]))
// Print processed files
.pipe(print());
tasks.push(task);
}
}
if (tasks.length) {
return es.concat.apply(null, tasks);
}
}
gulp.task('bower_install', function() {
return bower({directory: componentDir});
});
gulp.task('bower_extract', ['bower_install'], function() {
return gulp.src(mainBowerFiles({
paths: { bowerDirectory:componentDir }
}), {base: componentDir})
.pipe(gulp.dest(libraryApp + '/static'));
});
gulp.task('build_js', function() {
var chain = lazypipe().pipe(uglify);
return builder('.js', chain);
});
gulp.task('build_css', function() {
var chain = lazypipe().pipe(please);
return builder('.css', chain);
});
gulp.task('sass', function() {
return es.concat.apply(null, app_keys.map(function(app) {
return gulp.src(app + '/static/**/*.scss', {base: './'})
.pipe(newer({dest: './', ext: '.css'}))
//.pipe(sourcemaps.init())
.pipe(sass({includePaths: sassImportPaths.map(function(e) {
if (e[0] == '/') {
return e.slice(1);
} else {
return componentDir + '/' + e;
}
})}))
//.pipe(sourcemaps.write())
.pipe(gulp.dest('./'))
.pipe(print());
}));
});
gulp.task('watch', function() {
var sassSourceFiles = [],
cssSourceFiles = [],
jsSourceFiles = [];
for (app in apps) {
sassSourceFiles.push(app + '/static/**/*.scss');
cssSourceFiles.push(app + '/static/**/*.css');
jsSourceFiles.push(app + '/static/**/*.js');
};
jsSourceFiles = jsSourceFiles.concat(findExclusionGlobs('.js'));
cssSourceFiles = cssSourceFiles.concat(findExclusionGlobs('.css'));
gulp.watch(sassSourceFiles, ['sass']);
gulp.watch(cssSourceFiles, ['build_css']);
gulp.watch(jsSourceFiles, ['build_js']);
});
gulp.task('bower', ['bower_extract']);
gulp.task('build_styles', gulpSequence('sass', 'build_css'));
gulp.task('build', ['build_js', 'build_styles']);
gulp.task('default', gulpSequence('build', 'watch'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment