Skip to content

Instantly share code, notes, and snippets.

@thujeevan
Created February 19, 2015 11:31
Show Gist options
  • Save thujeevan/5f61092aa3e0544c95c4 to your computer and use it in GitHub Desktop.
Save thujeevan/5f61092aa3e0544c95c4 to your computer and use it in GitHub Desktop.
gulpfile.js for an Ember project using bower, browserify and emblem
'use strict';
var through = require('through2');
var path = require('path');
var gutil = require('gulp-util');
var Emblem = require('emblem');
var EmberHandlebars = require('ember-template-compiler').EmberHandlebars;
var getTemplateName = function(filepath, options) {
var name;
if (options.rootPath) {
name = filepath.replace(new RegExp('\\\\', 'g'), '/').replace(/\.\w+$/, '').replace(new RegExp('.*' + options.rootPath), '');
} else {
name = path.basename(filepath).slice(0, -path.extname(filepath).length).replace(/\./g, '/');
}
while (name[0] == '.' || name[0] == '/') {
name = name.slice(1);
}
return name;
};
module.exports = function(options) {
var opts = options || {};
var compilerOptions = opts.compilerOptions || {};
return through.obj(function(file, enc, callback) {
if (file.isNull()) {
this.push(file); // pass along
return callback();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-em-blem', 'Streams are not supported!'));
return callback();
}
var contents = file.contents.toString();
var compiled = null;
try {
compiled = Emblem.precompile(EmberHandlebars, contents, compilerOptions).toString();
} catch (err) {
this.emit('error', err);
}
if (compiled) {
var name = getTemplateName(file.path, opts);
var output = "Ember.TEMPLATES['" + name + "'] = Ember.Handlebars.template(" + compiled + ");";
file.contents = new Buffer(output);
file.path = file.path.slice(0, -path.extname(file.path).length) + '.js';
this.push(file);
return callback();
}
});
};
var gulp = require('gulp');
var concat = require('gulp-concat');
var emblem = require('./scripts/gulp-em-blem.js');
var myth = require('gulp-myth');
var hint = require('gulp-jshint');
var cache = require('gulp-cache');
var gif = require('gulp-if');
var uglify = require('gulp-uglify');
var browserify = require('browserify');
var shim = require('browserify-shim');
var buffer = require('vinyl-buffer');
var sourcemaps = require('gulp-sourcemaps');
var minimist = require('minimist');
var stringify = require('stringify');
var through = require('through2');
var vinyl = require('vinyl');
var del = require('del');
var knownOptions = {
string: 'env',
default: {
env: process.env.NODE_ENV || 'development'
}
};
var options = minimist(process.argv.slice(2), knownOptions);
var debug = options.env !== 'production';
var bower = './bower_components';
var getBundleName = function() {
var version = require('./package.json').version;
var name = require('./package.json').name;
return name + '.' + version + (debug ? '' : '.min');
};
gulp.task('vendor-css', function() {
var ext = debug ? '.css' : '.min.css';
return gulp.src([bower + '/bootstrap/dist/css/bootstrap.min.css',
bower + '/bootcards/dist/css/bootcards-desktop-lite.min.css',
bower + '/font-awesome/css/font-awesome.min.css'
])
.pipe(concat('vendor' + ext))
.pipe(gulp.dest('dist/css'));
});
gulp.task('vendor-fonts', function(cb) {
return gulp.src([bower + '/bootcards/dist/fonts/*.*',
bower + '/bootstrap/dist/fonts/*.*',
bower + '/font-awesome/fonts/*.*'
])
.pipe(gulp.dest('dist/fonts'));
});
gulp.task('vendor', ['vendor-css', 'vendor-fonts'], function() {
var ember = debug ? 'ember.js' : 'ember.prod.js';
var ext = debug ? '.js' : '.min.js';
return gulp.src([bower + '/jquery/dist/jquery.js',
bower + '/handlebars/handlebars.js',
bower + '/ember/' + ember,
bower + '/ember-data/ember-data.js',
'node_modules/ember-localstorage-adapter/localstorage_adapter.js',
bower + '/bootstrap/dist/js/bootstrap.min.js',
bower + '/bootcards/dist/js/bootcards.min.js'
])
.pipe(concat('vendor' + ext))
.pipe(gif(!debug, uglify()))
.pipe(gulp.dest('dist/js'));
});
gulp.task('hint', function() {
return gulp.src('src/**/*.js')
.pipe(hint());
});
gulp.task('scripts', ['hint', 'templates'], function() {
var bundler = function() {
var b = browserify({
debug: true,
insertGlobals: true
}),
stream = through.obj(function(file, enc, next) {
// add each file to the bundle
b.add(file.path);
next();
}, function(next) {
b.transform(stringify({
extensions: ['.yml']
}))
.bundle(function(err, src) {
// create a new vinyl file with bundle contents
// and push it to the stream
stream.push(new vinyl({
path: 'bundle.js', // this path is relative to dest path
contents: src
}));
next();
});
});
return stream;
};
return gulp.src('src/**/*.js')
// multiple entry files going to bundler
// you can apply jslint, jsbeautify on src files here
.pipe(bundler())
// single bundle file
// apply minification or/and rename bundle file here
.pipe(concat(getBundleName() + '.js'))
.pipe(gif(!debug, uglify()))
.pipe(gulp.dest('dist/js'));
});
gulp.task('templates', function() {
return gulp.src(['src/**/*.emblem', 'src/**/*.em'])
.pipe(cache(emblem()))
.pipe(concat('templates.js'))
.pipe(gulp.dest('dist/'));
});
gulp.task('styles', function() {
return gulp.src('src/**/*.css')
.pipe(myth())
.pipe(concat(getBundleName() + '.css'))
.pipe(gulp.dest('dist/css'));
});
gulp.task('clean:dist', function(cb) {
del.sync('dist/**', {
force: true
});
cb();
});
gulp.task('default', ['styles', 'scripts'], function() {});
gulp.task('all', ['vendor', 'styles', 'scripts'], function() {});
gulp.task('watch', function() {
gulp.watch('src/**/*.js', ['scripts']);
gulp.watch('src/**/*.emblem', ['scripts']);
gulp.watch('src/**/*.css', ['styles']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment