gulpfile.js 4.11 KB
var gulp=require('gulp'),
    fs=require('fs'),
    ftp = require('gulp-ftp'),
    gutil = require('gulp-util'),
    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'),
     minifycss = require('gulp-minify-css'),
    webpack = require('webpack');
var crypto = require('crypto');
var rename = require("gulp-rename");
var clean = require('gulp-clean');


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 md5= "";//"-"+crypto.createHash('md5').update(new Date().toString()).digest('hex');;

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


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

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

gulp.task('build',["clean"],function(){
    gulp.start(["buildjs","assets","static-config"]);
});//"static-config"

//预编译css
gulp.task('sass', function() {
    gulp.src('sass/index.scss')
        .pipe(plumber())
        .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
        .pipe(minifycss())
        .pipe(autoprefixer({
            browsers: ['last 5 versions'],
            cascade: false
        }))
        .pipe(gulp.dest(path.join(public_dir,"dist")));
});
//预编译js
gulp.task('js',function(cb){
    wconfig.output.path=path.join(public_dir,"dist");
    webpack(wconfig, cb);
});
//监控css和js
gulp.task("default-watch",function(){
    gulp.watch('sass/**/*.scss', ['sass']);
    gulp.watch('js/**/*.js', ['js']);
});
gulp.task("clean",function(){
    return gulp.src(dist_dir.js+"/", {read: false})
    .pipe(clean());
});
//发布js文件
gulp.task("buildjs",["js"],function(){
    gulp.src(path.join(public_dir,"dist") + '/*.js')
        .pipe(uglify())
        .pipe(rename({
            suffix:md5
        }))
        .pipe(gulp.dest(dist_dir.js));
});
//发布静态资源
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));

     gulp.src(path.join(public_dir,"dist") + '/*.css')
        .pipe(minifycss())
        .pipe(gulp.dest(dist_dir.css));
    
    // 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(minifycss())
    // .pipe(gulp.dest(dist_dir.css))    
});

//配置静态资源文件
gulp.task("static-config",function(){
    // var files = fs.readdirSync('./dist/libs');
    var staticConfig={
        test:{
            path:'/dist'
        },
        preview: {
            path: cdn_domain + config.name + '/' + config.version,
            md5:md5
        },
        production: {
            path: cdn_domain + config.name + '/' + config.version,
            md5:md5
        }
    }
    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);

});