gulpfile.js 3.66 KB
var gulp=require('gulp'),
    fs=require('fs'),
    path=require('path'),
    plumber = require('gulp-plumber'),
    sass = require('gulp-sass'),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify'),
    md5 = require("gulp-md5"),
    autoprefixer = require('gulp-autoprefixer'),
    server = require('gulp-develop-server'),
    webpack = require('webpack');


var wconfig = require('./webpack.config');
var env = process.env.NODE_ENV || 'development';




var config = JSON.parse(fs.readFileSync('./package.json').toString());
var assets_dir = 'dist/' + config.name + '/assets';
var public_dir = '../public';
var server_dir = '../server';
var dist_dir = {
    js: 'dist/' + config.name + '/' + config.version,
    css: 'dist/' + config.name + '/' + config.version,
    image: assets_dir + '/images',
    font: assets_dir + '/fonts'
};

var cdn_domain = 'http://cdn.yoho.cn/';

var ftpConfig = {
    host: '218.94.75.58',
    user: 'php',
    pass: 'yoho9646'
};


gulp.task('default',["sass","js"]);

gulp.task('start',["default-watch","server"]);

gulp.task('build',["buildjs","assets","static-config"]);

//预编译css
gulp.task('sass', function() {
    gulp.src('sass/**/*.scss')
        .pipe(plumber())
        .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
        .pipe(autoprefixer({
            browsers: ['last 5 versions'],
            cascade: false
        }))
        .pipe(gulp.dest(path.join(public_dir,"css")));
});
//预编译js
gulp.task('js',function(cb){
    wconfig.output.path=dist_dir.js;
    webpack(wconfig, cb);
});
//监控css和js
gulp.task("default-watch",function(){
    gulp.watch('sass/**/*.scss', ['sass']);
});

//发布js文件
gulp.task("buildjs",["js"],function(){
    gulp.src(dist_dir.js + '/libs.js')
        .pipe(uglify())
        .pipe(md5())
        .pipe(gulp.dest('./dist/libs'));
});
//发布静态资源
gulp.task('assets', function() {
    gulp.src(public_dir + '/img/**')
        .pipe(gulp.dest(dist_dir.image));
    gulp.src(public_dir + '/fonts/*')
        .pipe(gulp.dest(dist_dir.font));
    
    var str=fs.readFileSync(public_dir + '/css/all.css','utf-8');
    attr=str.replace(/(\n|\r)*/g,'').replace(/(\n)+@import url\(\'/g,function(name){
        return public_dir + '/css/';
    }).replace(/\'\)/g,'').split(';');
    gulp.src(attr)
    .pipe(concat('all.css'))
    .pipe(gulp.dest(dist_dir.css))    
});

//配置静态资源文件
gulp.task("static-config",function(){
    var files = fs.readdirSync('./dist/libs');
    var staticConfig={
        test:{
            libs: '/dist/libs-all.js',
            js: '/dist/index-debug.js',
            css:'/css/all.css'
        },
        preview: {
            libs: cdn_domain + 'libs/' + files[0],
            js: cdn_domain + config.name + '/' + config.version + '/index.js',
            css: cdn_domain + config.name + '/' + config.version + '/all.css'
        },
        production: {
            libs: cdn_domain + 'libs/' + files[0],
            js: cdn_domain + config.name + '/' + config.version + '/index.js',
            css: cdn_domain + config.name + '/' + config.version + '/all.css'
        }
    }
    fs.writeFileSync('../server/staticConfig.js', "exports.staticDir = " + JSON.stringify(staticConfig));
});

//发布到CDN
gulp.task('dist', function() {
    var ftpstream = ftp(ftpConfig);
    return gulp.src('dist/**/')
        .pipe(ftpstream)
        .pipe(gutil.noop());
});

//启动服务
gulp.task('server', function() {
    server.listen({
        path: server_dir + '/app.js'
    });
    gulp.watch([
        server_dir + '/app.js',
        server_dir+"/{interface,adapter,config,mid,stub,util}/**/*.js",
        server_dir+"/views/**/*.html"], server.restart);

});