var gulp = require('gulp');
var jshint = require('gulp-jshint'); var concat = require('gulp-concat'); var uglify = require('gulp-uglify'); var rename = require('gulp-rename'); var gutil = require("gulp-util"); var glob = require('glob'); var md5 = require('md5-file'); var replace = require('gulp-replace-task'); var source = require('vinyl-source-stream'); var buffer = require('vinyl-buffer'); var browserify = require('browserify'); var nodemon = require('gulp-nodemon'); var sass = require('gulp-sass');
var oPath = { 'sass': 'common/css/*.css', 'directives': 'common/engine/directives/**/*.js', 'services': 'common/engine/services/**/*.js', 'templates': '@(supplyCloud|common|integrated|platformService)/views/**/*.html', 'controllers': '@(supplyCloud|common|integrated|platformService)/controllers/**/*.js', 'bundles': 'public/@(js|css)/!(version).*', 'core': [ 'common/engine/setting.js', 'common/engine/tools.js', 'common/engine/common.js', 'common/js/zhl.js', 'common/engine/app.js', 'common/engine/route_cloud.js', 'common/engine/directives.js', 'common/engine/filters.js', 'common/engine/factory.js' ], 'vendor': [ "common/vendors/lodash.min.js", "common/vendors/angular/angular.min.js", "common/vendors/angular/angular-translate.min.js", "common/vendors/angular/angular-translate-loader-static-files.min.js", "common/vendors/angular/angular-sanitize.min.js", "common/vendors/angular/angular-resource.min.js", "common/vendors/bootstrap/js/bootstrap.min.js", "common/vendors/angular/angular-route.min.js", "common/vendors/angular/angular-cookies.min.js", "common/vendors/file-upload/dist/angular-file-upload.min.js", "common/vendors/md5.min.js", "common/vendors/big.min.js", "common/vendors/chart.min.js", "common/vendors/jquery/jquery-ui.min.js", "common/vendors/jquery/jquery.mousewheel.min.js", "common/vendors/datetimepicker/js/bootstrap-datetimepicker.min.js", "common/vendors/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js", "common/vendors/web-im/strophe-1.2.8.min.js", "common/vendors/web-im/webim.config.js", "common/vendors/web-im/websdk-1.1.3.js" ], 'vendorCss': [ 'common/vendors/bootstrap/css/bootstrap.min.css', 'common/vendors/Font-Awesome/css/font-awesome.min.css', 'common/vendors/datetimepicker/css/bootstrap-datetimepicker.min.css' ] };
var NODE_ENV = 'production';
var createErrorHandler = function (name) { return function (err) { delete err.cause.stack; console.error('Error from ' + name + ' in compress task', err.cause); }; };
var start = function (env) { NODE_ENV = env; nodemon({ script: 'server.js', ext: 'js html json', watch: [ 'server.js', 'env.json', 'app', 'config', 'resources' ], delay: '2000', ignor: ['gulpfile.js'], env: {'NODE_ENV': env} }); };
gulp.task('development', function () { start('development'); }); gulp.task('production', function () { start('production'); });
gulp.task('compress', function (cb) { pump([ gulp.src('lib/*.js'), uglify(), gulp.dest('dist') ], cb ); });
gulp.task('browserify', function () { browserify() .require('./common/engine/util.js', {expose: 'util'}) .require('./common/vendors/moment.js', {expose: 'moment'}) .exclude('version') .bundle() .pipe(source('common.bundle.js')) .pipe(buffer()) .pipe(uglify()) .on('error', createErrorHandler('uglify')) .pipe(gulp.dest('public/js')); });
gulp.task('lint', function () { gulp.src([oPath.directives, oPath.services]) .pipe(jshint()) .pipe(jshint.reporter('default')); });
gulp.task('min-directives', function () { gulp.src(oPath.directives) .pipe(concat('directive.min.js')) .pipe(uglify()) .on('error', createErrorHandler('uglify')) .pipe(gulp.dest('public/js')); });
gulp.task('min-services', function () { gulp.src(oPath.services) .pipe(concat('services.min.js')) .pipe(uglify()) .on('error', createErrorHandler('uglify')) .pipe(gulp.dest('public/js')); });
gulp.task('min-core', function () { gulp.src(oPath.core) .pipe(concat('core.min.js')) .pipe(uglify()) .on('error', createErrorHandler('uglify')) .pipe(gulp.dest('public/js')); });
gulp.task('min-vendor', function () { gulp.src(oPath.vendor) .pipe(concat('vendor.min.js')) .pipe(uglify()) .on('error', createErrorHandler('uglify')) .pipe(gulp.dest('public/js')); });
gulp.task("sass", function () { gulp.src(oPath.sass) .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError)) .pipe(concat('zhl.css')) .pipe(gulp.dest("public/css")); gulp.src(oPath.vendorCss) .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError)) .pipe(concat('vendor.min.css')) .pipe(gulp.dest('public/css')); });
gulp.task("version", function () {
var objHash = {}, arrHash = [];
objHash.build = (new Date()).toLocaleString();
glob.sync(oPath.templates).forEach(function (file) { objHash[file] = md5.sync(file).slice(0,10); });
glob.sync(oPath.controllers).forEach(function (file) { objHash[file] = md5.sync(file).slice(0,10); });
glob.sync(oPath.bundles).forEach(function (file) { objHash[file] = md5.sync(file).slice(0,10); });
Object.keys(objHash).forEach(function (key) { arrHash.push('"' + key + '"' + ' : ' + '"' + objHash[key] + '"'); });
gulp.src('./resources/config/version.js.tpl') .pipe(replace({ patterns: [{ match: 'hash', replacement: arrHash.join(' , ') } ]})) .pipe(rename('version.js')) .pipe(gulp.dest('./config'))
browserify() .require('./config/version.js', {expose: 'version'}) .bundle() .pipe(source('version.js')) .pipe(buffer()) .pipe(uglify()) .on('error', createErrorHandler('uglify')) .pipe(gulp.dest('./public/js'));
});
gulp.task('watch', ['production', 'min', 'browserify', 'version'], function () { gulp.watch([oPath.sass, oPath.vendorCss], ['sass']); gulp.watch(oPath.directives, ['min-directives']); gulp.watch(oPath.services, ['min-services']); gulp.watch(oPath.core, ['min-core']); gulp.watch('common/engine/util.js', ['browserify']); gulp.watch([oPath.templates, oPath.controllers, oPath.bundles], ['version']); });
gulp.task('min', ['min-directives', 'min-services', 'min-core', 'min-vendor', 'sass']);
gulp.task('build', ['min', 'browserify', 'version']);
gulp.task('default', ['production', 'min', 'browserify', 'version', 'watch']);
|