Skip to content

Instantly share code, notes, and snippets.

@Kuznetsov-Ilia
Created March 27, 2014 08:40
Show Gist options
  • Save Kuznetsov-Ilia/9803061 to your computer and use it in GitHub Desktop.
Save Kuznetsov-Ilia/9803061 to your computer and use it in GitHub Desktop.
var gulp = require('gulp');
var gutil = require('gulp-util');
var rename = require('gulp-rename');
var log = gutil.log;
gulp.task('default', ['watch']);
/* CONCAT */
var concat = require('gulp-concat');
gulp.task('concat-services', function () {
return gulp
.src('src/services/*.styl')
.pipe(concat('services.styl'))
.pipe(gulp.dest('build/styles/'))
});
gulp.task('concat-services-js', function () {
return gulp
.src('src/services/*.js')
.pipe(concat('services.js'))
.pipe(gulp.dest('build'))
});
gulp.task('concat-plugins', function () {
return gulp
.src('src/plugins/*.js')
.pipe(concat('plugins.js'))
.pipe(gulp.dest('build'))
});
gulp.task('concat-polyfills', function () {
return gulp
.src('src/polyfills/*.js')
.pipe(concat('polyfills.js'))
.pipe(gulp.dest('build'))
});
gulp.task('concat-regions', function () {
return gulp
.src('src/regions/**/*.styl')
.pipe(concat('regions.styl'))
.pipe(gulp.dest('build/styles/'))
});
gulp.task('concat-pages', function () {
return gulp
.src('src/pages/**/*.styl')
.pipe(concat('pages.styl'))
.pipe(gulp.dest('build/styles/'))
});
gulp.task('concat-components', function () {
return gulp
.src('src/components/**/*.styl')
.pipe(concat('components.styl'))
.pipe(gulp.dest('build/styles/'))
});
gulp.task('concat', ['concat-components', 'concat-pages', 'concat-regions', 'concat-services', 'concat-services-js', 'concat-plugins', 'concat-polyfills'])
var festHardcore = require('gulp-fest-hardcore');
var beautify = require('gulp-beautify');
function festWatch(event) {
var path = event.path.split('/');
path.pop(); // нормализация под 'src/**/*.xml'
path.pop();
path.pop();
log('changed:', event.path);
return gulp
.src(event.path, {
base: path.join('/')
})
.pipe(rename(getTmplName))
.on('data', normalizeBase)
.pipe(festHardcore())
.pipe(beautify({
indentSize: 2
}))
.pipe(gulp.dest('build/templates'))
}
gulp.task('fest-dev', function () {
return gulp.src('src/**/*.xml')
.pipe(rename(getTmplName))
.on('data', normalizeBase)
.pipe(festHardcore())
.pipe(beautify({
indentSize: 2
}))
.pipe(gulp.dest('build/templates'))
})
gulp.task('fest-prod', function () {
return gulp.src('src/**/*.xml')
.pipe(rename(getTmplName))
.on('data', normalizeBase)
.pipe(festHardcore())
.pipe(gulp.dest('build/templates'))
});
function normalizeBase(file) {
file.base += file.path
.replace(file.base, '')
.replace(file.path.split('/').pop(), '');
}
function getTmplName(file) {
var componentName = file.dirname.split('/')[1];
if (file.basename !== 'index') {
file.basename = componentName + '-' + file.basename;
} else {
file.basename = componentName;
}
file.extname = '.js';
return file;
}
/* CSS */
var stylus = require('gulp-stylus');
gulp.task('stylus', function () {
return gulp.src('src/styl/base.styl')
.pipe(stylus({
use: ['nib'],
set: ['compress']
}))
.pipe(rename('s.css'))
.pipe(gulp.dest('app'));
});
var cssmin = require('gulp-cssmin');
gulp.task('cssmin', function () {
return gulp.src('app/build/*.css')
.pipe(cssmin())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('app/build'));
});
/* IMAGES */
var sprites = require('./tasks-gulp/sprites.js');
gulp.task('sprite', function (cb) {
sprites({
src: 'images/sprites/',
dest: 'build/styles/sprites.styl',
done: cb
});
});
var pngquant = require('./tasks-gulp/pngquant.js');
gulp.task('pngmin', function () {
return gulp
.src('build/icon.png', {
buffer: false
})
.pipe(pngquant([256, '--ext=.png', '--force', '--speed=1', '--quality=85-100']))
.pipe(gulp.dest('app/images/'))
})
/* JS */
var exec = require('child_process').exec;
gulp.task('lmd', function (cb) {
exec('node_modules/lmd/bin/lmd build app', {},
function (error, stdout, stderr) {
log(gutil.colors.red(stderr));
console.log(stdout);
cb();
});
});
var uglify = require('gulp-uglify');
gulp.task('uglify', function () {
return gulp.src('app/build/lmd.js')
.pipe(uglify())
.pipe(gulp.dest('app/build'))
});
var replace = require('gulp-replace');
gulp.task('replace', function () {
return gulp.src(['app/ttx/index.tx', 'app/ttx/inc/html.tx'], {
base: 'app/ttx'
})
.pipe(replace(/@@timestamp/g, new Date().getTime()))
.pipe(gulp.dest('app/ttx'));
});
var gzip = require('gulp-gzip');
gulp.task('gzip-js', function () {
return gulp.src('app/build/*.js')
.pipe(gzip())
.pipe(gulp.dest('app/build'));
});
gulp.task('gzip-css', function () {
return gulp.src('app/*.css')
.pipe(gzip())
.pipe(gulp.dest('app'));
});
gulp.task('gzip', ['gzip-js', 'gzip-css']);
var runSequence = require('run-sequence');
gulp.task('dev', ['concat', 'fest-dev', 'sprite', 'pngmin', 'stylus', 'lmd']);
gulp.task('prepare', ['concat', 'fest-prod']);
gulp.task('build', ['stylus', 'lmd']);
gulp.task('minification', ['cssmin', 'uglify']);
gulp.task('build-project', function (callback) {
runSequence('prepare', 'build', 'minification', 'replace', 'gzip', callback);
});
/* WATCH */
gulp.task('watch', function () {
gulp.watch('src/components/**/*.styl', ['concat-components']);
gulp.watch('src/pages/**/*.styl', ['concat-pages']);
gulp.watch('src/regions/**/*.styl', ['concat-regions']);
gulp.watch('src/services/**/*.styl', ['concat-services']);
gulp.watch('build/styles/*.styl', ['stylus']);
gulp.watch('src/styl/*.styl', ['stylus']);
gulp.watch('src/polyfills/*.js', ['concat-polyfills']);
gulp.watch('src/plugins/*.js', ['concat-plugins']);
gulp.watch('src/services/*.js', ['concat-services-js']);
gulp.watch('build/templates/*.js', ['lmd']);
gulp.watch('src/**/*.js', ['lmd']);
gulp.watch('src/**/*.xml', festWatch);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment