gulpfile.js 21.0 KB
Newer Older
C
Catouse 已提交
1 2 3
var extend = require('extend'),
    runSequence = require('run-sequence'),
    fs = require('fs'),
C
Catouse 已提交
4
    chmod = require('gulp-chmod'),
C
Catouse 已提交
5 6 7 8 9 10 11 12 13
    moment = require('moment'),
    less = require('gulp-less'),
    cssmin = require('gulp-cssmin'),
    csscomb = require('gulp-csscomb'),
    autoprefixer = require('gulp-autoprefixer'),
    concat = require('gulp-concat'),
    header = require('gulp-header'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename'),
14
    change = require('gulp-change'),
C
Catouse 已提交
15 16 17 18 19 20 21
    sourcemaps = require('gulp-sourcemaps'),
    prettify = require('gulp-jsbeautifier'),
    mkdirp = require('mkdirp'),
    del = require('del'),
    format = require('string-format').extend(String.prototype),
    colors = require('colors'),
    gulp = require('gulp'),
C
Catouse 已提交
22
    jsonminify = require('gulp-jsonminify'),
C
Catouse 已提交
23 24
    zui = require('./zui.json'),
    pkg = require('./package.json'),
C
Catouse 已提交
25
    babel = require('gulp-babel'),
C
Catouse 已提交
26
    showFileDetail = true;
C
Catouse 已提交
27 28 29 30

// Disable the 'possible EventEmitter memory leak detected' warning.
gulp.setMaxListeners(0);

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
// Workaround for https://github.com/gulpjs/gulp/issues/71#issuecomment-68062782
var origSrc = gulp.src;
gulp.src = function () {
    return fixPipe(origSrc.apply(this, arguments));
};
function fixPipe(stream) {
    var origPipe = stream.pipe;
    stream.pipe = function (dest) {
        arguments[0] = dest.on('error', function (error) {
            var nextStreams = dest._nextStreams;
            if (nextStreams) {
                nextStreams.forEach(function (nextStream) {
                    nextStream.emit('error', error);
                });
            } else if (dest.listeners('error').length === 1) {
                throw error;
            }
        });
        var nextStream = fixPipe(origPipe.apply(this, arguments));
        (this._nextStreams || (this._nextStreams = [])).push(nextStream);
        return nextStream;
    };
    return stream;
}
55 56 57 58
// try load zui.templates.json and merge into zui.
try {
    extend(true, zui, require('./zui.templates.json'));
} catch (e) { }
59

C
Catouse 已提交
60 61
// try load zui.custom.json and merge into zui.
try {
C
Catouse 已提交
62 63
    var zuiCustom = require('./zui.custom.json');
    if (zuiCustom) extend(true, zui, zuiCustom);
64
} catch (e) { }
C
Catouse 已提交
65

66 67
// try load specific config from file by process argument
const configFileArg = process.argv[4];
C
Catouse 已提交
68
if (configFileArg && configFileArg.startsWith('--config=')) {
69 70 71 72 73 74 75 76 77
    let configFile = configFileArg.substring('--config='.length);
    if (configFile) {
        try {
            const zuiCustom = require(configFile.replace('~t/', './templates/'));
            if (zuiCustom) extend(true, zui, zuiCustom);
        } catch (e) { }
    }
}

C
Catouse 已提交
78
var today = moment();
C
Catouse 已提交
79
var typeSet = ['less', 'js', 'resource'],
C
Catouse 已提交
80 81
    lib = zui.lib,
    builds = zui.builds,
C
Catouse 已提交
82
    BANNER = ('/*!\n' +
C
Catouse 已提交
83 84 85 86
        ' * {title} - v{version} - {date}\n' +
        ' * {homepage}\n' +
        ' * GitHub: {repo} \n' +
        ' * Copyright (c) {year} {author}; Licensed {license}\n' +
C
Catouse 已提交
87 88
        ' */\n\n'),
    BANNER_OPTONS = {
C
Catouse 已提交
89 90 91 92 93 94 95 96
        title: pkg.title || pkg.name,
        version: pkg.version,
        date: today.format('YYYY-MM-DD'),
        homepage: pkg.homepage,
        repo: pkg.repository.url,
        year: today.format('YYYY'),
        author: pkg.author,
        license: pkg.license
C
Catouse 已提交
97 98 99 100
    },
    BOOTSTRAP_STATEMENT = '/*! Some code copy from Bootstrap v3.0.0 by @fat and @mdo. (Copyright 2013 Twitter, Inc. Licensed under http://www.apache.org/licenses/)*/\n\n';

function formatBanner(options) {
101
    if (options && options.title) {
C
Catouse 已提交
102 103 104 105 106
        options.title = BANNER_OPTONS.title + ': ' + options.title;
    }
    options = Object.assign({}, BANNER_OPTONS, options);
    return BANNER.format(options);
}
C
Catouse 已提交
107 108 109 110

function tryStatSync(path) {
    try {
        return fs.statSync(path);
111
    } catch (e) {
C
Catouse 已提交
112 113 114 115 116 117 118 119 120
        return false;
    }
}

function isFileExist(path) {
    var stats = tryStatSync(path);
    return stats && stats.isFile();
}

121
function getItemList(list, items, ignoreDpds, ignoreBasic) {
C
Catouse 已提交
122 123
    items = items || [];

124 125 126
    if (Array.isArray(list)) {
        list.forEach(function (name) {
            if (name === 'basic' && ignoreBasic) return;
127
            getItemList(name, items, ignoreDpds, ignoreBasic);
C
Catouse 已提交
128
        });
129
    } else if (!(list === 'basic' && ignoreBasic)) {
C
Catouse 已提交
130
        var item = lib[list];
131 132
        if (item && items.indexOf(list) < 0) {
            if (!ignoreDpds && item.dpds) {
133
                getItemList(item.dpds, items, ignoreDpds, ignoreBasic);
C
Catouse 已提交
134
            }
C
Catouse 已提交
135 136 137
            if (item.libDpds) {
                getItemList(item.libDpds, items, ignoreDpds, ignoreBasic);
            }
138
            if (item.src) items.push(list);
C
Catouse 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        }
    }

    return items;
}

function getBuildSource(build) {
    var list = [];

    var sources = {
        less: [],
        js: [],
        resource: []
    };

154
    if (!Array.isArray(list)) list = [list];
C
Catouse 已提交
155

156
    if (build.settingDpds) list = getItemList(build.settingDpds, list);
157
    list = getItemList(build.includes, list, build.ignoreDpds, build.ignoreBasic);
C
Catouse 已提交
158

159
    list.forEach(function (item) {
C
Catouse 已提交
160
        var libItem = lib[item];
161 162 163 164 165
        if (libItem && libItem.src) {
            typeSet.forEach(function (type) {
                if (libItem.src[type]) {
                    libItem.src[type].forEach(function (file) {
                        if (sources[type].indexOf(file) < 0) {
C
Catouse 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178
                            sources[type].push(file);
                        }
                    });
                }
            });
        }
    });

    return sources;
}

function getSourceConfig(src) {
    var idx = src.lastIndexOf('//');
179
    if (idx > 0) {
C
Catouse 已提交
180 181
        return {
            base: src.substr(0, idx + 1),
C
Catouse 已提交
182 183
            src: src.replace(/\/\//g, '/'),
            file: src.substr(idx + 2)
C
Catouse 已提交
184 185 186 187 188
        };
    }
    idx = src.lastIndexOf('/');
    return {
        base: src.substr(0, idx + 1),
C
Catouse 已提交
189 190
        src: src,
        file: src.substr(idx + 1)
C
Catouse 已提交
191 192 193 194 195
    }
}

function getBuildPath(build, type) {
    var path = build.dest;
196
    if (build.subdirectories) {
C
Catouse 已提交
197 198
        path += '/' + type + '/';
    }
199
    if (path.indexOf('.') !== 0) {
C
Catouse 已提交
200 201 202 203 204 205 206 207 208 209 210
        path = './' + path;
    }
    return path.replace(/\/\//g, '/').replace(/\/\//g, '/');
}

function getBuildDestFilename(build, type, suffix) {
    var file = getBuildPath(build, type);
    file += '/' + build.filename + '.' + (suffix || type);
    return file.replace(/\/\//g, '/');
}

211 212 213 214 215 216 217 218
function gulpBuildColorsetJS(build, lessSrc, bannerContent) {
    var name = 'build:' + build.name;
    var destPath = getBuildPath(build, 'js');
    return gulp.src(lessSrc)
        .pipe(less())
        .pipe(rename({
            extname: '.js'
        }))
219
        .pipe(change(function (css, done) {
220 221 222 223 224 225 226 227
            css = css.replace(/\/\*\*/g, '').replace(/\*\*\//g, '');
            css = css.replace(/\.color-(\w+) \{\n  color: (#?\w+);\n\}/g, "        $1: '$2',");
            css = css.replace(',\n    };', '\n    };');
            css = css.replace('\n\n', '\n');
            done(null, css);
        }))
        .pipe(header(bannerContent))
        .pipe(gulp.dest(destPath))
228
        .on('end', function () {
229 230 231 232
            console.log('     js > '.yellow.bold + (destPath + build.filename + '.js').italic.underline);
        });
}

C
Catouse 已提交
233
function buildBundle(name, callback, type) {
C
Catouse 已提交
234
    name = name || 'all';
C
Catouse 已提交
235
    var build = builds[name];
C
Catouse 已提交
236 237
    var taskList = [],
        depTaskList = [];
C
Catouse 已提交
238 239

    // clean files
240
    if (!type && name === 'dist') {
C
Catouse 已提交
241
        del.sync('./dist/**/*');
242
    } else if (!type && name === 'doc') {
C
Catouse 已提交
243 244 245
        del.sync([
            './docs/js/**/*',
            './docs/css/**/*',
C
Catouse 已提交
246 247
            './docs/fonts/**/*'
        ]);
C
Catouse 已提交
248 249
    }

250 251
    if (!build) {
        if (name === 'all') {
C
Catouse 已提交
252
            console.log('           ========== BUILD ALL =========='.blue.bold);
C
Catouse 已提交
253
            var buildList = Object.keys(builds);
254
            buildList.forEach(function (nm) {
C
Catouse 已提交
255
                build = builds[nm];
256
                if (build && !build.bundles) {
C
Catouse 已提交
257
                    var taskName = 'build:' + nm;
258
                    gulp.task(taskName, function (cb) {
C
Catouse 已提交
259 260 261 262 263
                        buildBundle(nm, cb, type)
                    });
                    taskList.push(taskName);
                }
            });
264
            if (taskList.length) runSequence(taskList, function () {
C
Catouse 已提交
265
                console.log(('        √  Build ' + 'ALL'.bold + ' success! ').green);
C
Catouse 已提交
266 267 268 269 270
                callback && callback();
            });
            return;
        } else {
            var buildLib = lib[name];
271
            if (buildLib) {
C
Catouse 已提交
272 273 274
                build = {
                    title: buildLib.name,
                    dest: 'dist/lib/' + name + '/',
C
Catouse 已提交
275
                    filename: buildLib.filename || ((buildLib.source && buildLib.source !== 'Bootstrap') ? name : ('zui.' + name)),
C
Catouse 已提交
276
                    includes: [name],
C
Catouse 已提交
277 278 279
                    source: buildLib.source,
                    settingDpds: (buildLib.src && buildLib.src.less && buildLib.src.less.length) ? ['setting'] : null,
                    ignoreBasic: true,
C
Catouse 已提交
280 281
                    ignoreDpds: buildLib.ignoreDpds !== undefined ? buildLib.ignoreDpds : true,
                    babel: buildLib.babel
C
Catouse 已提交
282 283
                };
            } else {
C
Catouse 已提交
284
                console.log(('           Cannot found the build config: ' + name).red);
C
Catouse 已提交
285 286 287
                return false;
            }
        }
288
    } else if (build.bundles) {
C
Catouse 已提交
289 290
        console.log(('           === BUILD BUNDLES ' + name.toUpperCase() + ' [' + build.bundles.join(', ') + '] ===').blue.bold);
        var bundlesTaskList = [];
291 292
        build.bundles.forEach(function (bundleName) {
            gulp.task('build:' + bundleName, function (cb) {
C
Catouse 已提交
293 294
                buildBundle(bundleName, cb, type);
            });
C
Catouse 已提交
295

C
Catouse 已提交
296
            bundlesTaskList.push('build:' + bundleName);
C
Catouse 已提交
297 298
        });

299 300 301
        gulp.task('build:' + name + ':bundles', function (cb) {
            runSequence(bundlesTaskList, function () {
                console.log(('         √ Build BUNDLES ' + name.toUpperCase() + ' [' + build.bundles.map(function (x) { return x.bold; }).join(', ') + '] success! ').green);
C
Catouse 已提交
302 303
                cb();
            });
C
Catouse 已提交
304
        });
C
Catouse 已提交
305 306

        depTaskList.push('build:' + name + ':bundles');
C
Catouse 已提交
307 308
    }

309 310 311 312 313 314 315 316 317 318 319
    if (build.preBuilds) {
        build.preBuilds.forEach(function (pBuildName) {
            var pBuild = builds[pBuildName];
            if (pBuild && pBuild.includes) {
                build.includes = pBuild.includes.concat(build.includes);
            }
        });
    }

    if (build.includes && build.includes.indexOf('colorset.js') > -1) {
        gulp.task('build:colorset.less2js', function (cb) {
320 321 322 323 324 325
            buildBundle('colorset.less2js', cb, 'less');
        });

        depTaskList.push('build:colorset.less2js');
    }

C
Catouse 已提交
326
    console.log(('           --- build ' + name + ' ---').cyan.bold);
C
Catouse 已提交
327

328
    var banner = formatBanner({ title: build.title || name });
C
Catouse 已提交
329
    var source = getBuildSource(build),
C
Catouse 已提交
330
        bannerContent = (build.source && build.source !== 'Bootstrap') ?
331
            '' : banner + (build.bootstrapStatement ? BOOTSTRAP_STATEMENT : '');
C
Catouse 已提交
332

333
    if (source.js && source.js.length && (!type || type === 'js')) {
C
Catouse 已提交
334
        console.log(('         + Ready to process ' + source.js.length + ' javascript files.').bold);
335 336
        source.js.forEach(function (f, idx) {
            if (f.indexOf('~/') === 0) {
C
Catouse 已提交
337 338
                source.js[idx] = f = 'src/js/' + f.substr(2);
            }
339
            if (showFileDetail) console.log(('         | ' + f).italic);
C
Catouse 已提交
340 341 342
        });

        //ar taskName = 'build:' + name + ':js';
343
        gulp.task('build:' + name + ':js', function () {
C
Catouse 已提交
344
            var destPath = getBuildPath(build, 'js');
C
Catouse 已提交
345 346 347 348 349 350 351
            var gulpPipe = gulp.src(source.js);
            if (build.babel) {
                gulpPipe = gulpPipe.pipe(babel({
                    presets: ['babel-preset-env']
                }));
            }
            return gulpPipe
C
Catouse 已提交
352 353
                .pipe(concat(build.filename + '.js'))
                .pipe(header(bannerContent))
C
Catouse 已提交
354
                .pipe(chmod(644))
C
Catouse 已提交
355
                .pipe(gulp.dest(destPath))
356
                .on('end', function () {
C
Catouse 已提交
357 358 359
                    console.log('      js > '.yellow.bold + (destPath + build.filename + '.js').italic.underline);
                })
                //.pipe(sourcemaps.init())
360
                .pipe(uglify({ preserveComments: 'some' }))
C
Catouse 已提交
361 362 363
                .pipe(rename({
                    suffix: '.min'
                }))
C
Catouse 已提交
364
                //.pipe(sourcemaps.write())
C
Catouse 已提交
365
                .pipe(chmod(644))
C
Catouse 已提交
366
                .pipe(gulp.dest(destPath))
367
                .on('end', function () {
C
Catouse 已提交
368 369
                    console.log('      js > '.yellow.bold + (destPath + build.filename + '.min.js').italic.underline);
                });
C
Catouse 已提交
370 371 372 373
        });
        taskList.push('build:' + name + ':js');
    }

374
    if (source.less && source.less.length && (!type || type === 'less')) {
C
Catouse 已提交
375
        var lessFileContent = '// \n// ' + build.title + '\n// Build config: ' + name + '\n//\n// This file generated by ZUI builder automatically at ' + today.toString() + '.\n//\n\n';
C
Catouse 已提交
376
        console.log(('         + Ready to process ' + source.less.length + ' less files.').bold);
377 378
        source.less.forEach(function (f, idx) {
            if (f.indexOf('~/') === 0) {
C
Catouse 已提交
379 380 381
                source.less[idx] = f = 'src/less/' + f.substr(2);
            }

382
            if (isFileExist(f)) {
C
Catouse 已提交
383 384 385
                lessFileContent += '@import "';
                lessFileContent += '../../' + f;
                lessFileContent += '";\n';
386
                if (showFileDetail) console.log(('         | ' + f).italic);
C
Catouse 已提交
387 388 389 390
            } else {
                lessFileContent += '// @import "';
                lessFileContent += '../../' + f;
                lessFileContent += '" // FILE NOT FOUND;\n';
391
                if (showFileDetail) console.log(('         - ' + f + ' [NOT FOUND]').red.italic);
C
Catouse 已提交
392 393 394
            }
        });

395
        gulp.task('build:' + name + ':less', function () {
C
Catouse 已提交
396 397 398 399 400 401
            var buildSourceFilePath = './build/less/' + build.filename + '.less';
            var destPath = getBuildPath(build, 'css');

            mkdirp.sync('./build/less/');
            fs.writeFileSync(buildSourceFilePath, lessFileContent);

402
            if (name === 'colorset.less2js') {
403 404 405
                return gulpBuildColorsetJS(build, buildSourceFilePath, bannerContent);
            }

C
Catouse 已提交
406 407
            return gulp.src(buildSourceFilePath)
                .pipe(less())
C
Catouse 已提交
408
                .pipe(autoprefixer({
C
Catouse 已提交
409 410 411 412 413 414 415 416
                    browsers: ["Android 2.3",
                        "Android >= 4",
                        "Chrome >= 20",
                        "Firefox >= 24",
                        "Explorer >= 8",
                        "iOS >= 6",
                        "Opera >= 12",
                        "Safari >= 6"],
C
Catouse 已提交
417 418
                    cascade: true
                }))
C
Catouse 已提交
419 420
                .pipe(csscomb())
                .pipe(header(bannerContent))
C
Catouse 已提交
421
                .pipe(chmod(644))
C
Catouse 已提交
422
                .pipe(gulp.dest(destPath))
423
                .on('end', function () {
C
Catouse 已提交
424
                    console.log('     css > '.yellow.bold + (destPath + build.filename + '.css').italic.underline);
C
Catouse 已提交
425 426
                })
                //.pipe(sourcemaps.init())
C
Catouse 已提交
427 428 429 430 431 432
                .pipe(cssmin({
                    compatibility: 'ie8',
                    keepSpecialComments: '*',
                    sourceMap: true,
                    advanced: false
                }))
C
Catouse 已提交
433 434 435
                .pipe(rename({
                    suffix: '.min'
                }))
C
Catouse 已提交
436

C
Catouse 已提交
437
                //.pipe(sourcemaps.write())
C
Catouse 已提交
438
                .pipe(chmod(644))
C
Catouse 已提交
439
                .pipe(gulp.dest(destPath))
440
                .on('end', function () {
C
Catouse 已提交
441
                    console.log('     css > '.yellow.bold + (destPath + build.filename + '.min.css').italic.underline);
C
Catouse 已提交
442
                });
C
Catouse 已提交
443 444 445 446
        });
        taskList.push('build:' + name + ':less');
    }

447
    if (source.resource && source.resource.length && (!type || type === 'resource')) {
C
Catouse 已提交
448
        console.log(('         + Ready to process ' + source.resource.length + ' resource files.').bold);
C
Catouse 已提交
449
        var destPath = getBuildPath(build, '');
450 451
        source.resource.forEach(function (f, idx) {
            if (f.indexOf('~/') === 0) {
C
Catouse 已提交
452 453
                source.resource[idx] = f = 'src//' + f.substr(2);
            }
454 455
            if (showFileDetail) console.log(('         | [' + idx + '] ' + f).italic);
            gulp.task('build:' + name + ':resource:' + idx, function () {
C
Catouse 已提交
456
                var sourceConfig = getSourceConfig(f);
C
Catouse 已提交
457
                return gulp.src(sourceConfig.src, {
458 459
                    base: sourceConfig.base
                })
C
Catouse 已提交
460
                    .pipe(chmod(644))
C
Catouse 已提交
461
                    .pipe(gulp.dest(destPath))
462
                    .on('end', function () {
C
Catouse 已提交
463 464
                        console.log('resource > '.yellow.bold + (destPath + sourceConfig.file).italic.underline);
                    });
C
Catouse 已提交
465 466 467 468 469
            });
            taskList.push('build:' + name + ':resource:' + idx);
        });
    }

470 471
    if (taskList.length || depTaskList.length) {
        var completeCallback = function () {
C
Catouse 已提交
472
            console.log(('         √ Build ' + name.bold + ' success! ').green);
C
Catouse 已提交
473
            callback && callback();
C
Catouse 已提交
474
        };
C
Catouse 已提交
475

476 477
        if (taskList.length) {
            if (depTaskList.length) {
C
Catouse 已提交
478 479 480 481 482 483 484
                runSequence(depTaskList, taskList, completeCallback);
            } else {
                runSequence(taskList, completeCallback);
            }
        } else {
            runSequence(depTaskList, completeCallback);
        }
C
Catouse 已提交
485

C
Catouse 已提交
486
    } else {
C
Catouse 已提交
487
        console.log(('           No source files for build: ' + name).red);
C
Catouse 已提交
488 489 490 491
        callback && callback();
    }
}

492
gulp.task('build', function (callback) {
C
Catouse 已提交
493
    var name = process.argv[3] || 'dist';
494 495
    if (name && name[0] === '-') name = name.substr(1);
    if (name === 'lib') name = 'seperate';
C
Catouse 已提交
496
    var type = process.argv.length > 4 ? process.argv[4] : false;
497 498
    if (type && (type === '-js' || type === '-less' || type === '-source')) type = type.substr(1);
    else type = null;
C
Catouse 已提交
499
    console.log('  BEGIN >> ' + (' Build ' + name.bold + ' ').inverse);
500
    buildBundle(name, function () {
C
Catouse 已提交
501
        console.log('    END >> ' + (' Build ' + name.bold + ' completed :)').green.inverse);
C
Catouse 已提交
502
    }, type);
C
Catouse 已提交
503 504
});

C
Catouse 已提交
505
function startWatchSrc(name, callback) {
506 507 508
    if (name === 'lib') name = 'seperate';
    var build = builds[name];
    var srcDir = build && build.srcDir || './src';
C
Catouse 已提交
509
    console.log(`           Start watching ${srcDir.blue}`);
510 511
    gulp.watch([srcDir + "/less/**/*"], function (event) {
        buildBundle(name, function () {
C
Catouse 已提交
512
            callback && callback(event, 'less');
C
Catouse 已提交
513 514 515 516
            console.log(''.green + (' WATCH ' + name.bold + ' COMPLETED. ').yellow.inverse);
        }, 'less');
    });

517 518 519
    gulp.watch([srcDir + "/js/**/*"], function (event) {
        if (event.path && (event.path.lastIndexOf('src/js/colorset.js') > -1) || event.path.lastIndexOf('src\\js\\colorset.js') > -1) return;
        buildBundle(name, function () {
C
Catouse 已提交
520
            callback && callback(event, 'js');
C
Catouse 已提交
521 522 523 524
            console.log(''.green + (' WATCH ' + name.bold + ' COMPLETED. ').yellow.inverse);
        }, 'js');
    });

525 526
    gulp.watch([srcDir + "/fonts/**/*"], function (event) {
        buildBundle(name, function () {
C
Catouse 已提交
527
            callback && callback(event, 'fonts');
C
Catouse 已提交
528 529 530 531 532
            console.log(''.green + (' WATCH ' + name.bold + ' COMPLETED. ').yellow.inverse);
        }, 'resource');
    });
}

533
gulp.task('watch', function (callback) {
C
Catouse 已提交
534
    var name = process.argv[3] || 'dist';
535
    if (name && name[0] === '-') name = name.substr(1);
C
Catouse 已提交
536 537 538
    startWatchSrc(name);
});

539
['dist', 'doc', 'theme', 'lib'].forEach(function (name) {
C
Catouse 已提交
540
    var depsTasks = (name == 'dist' || name == 'doc') ? ['minJSON'] : [];
541
    gulp.task(name, depsTasks, function (callback) {
C
Catouse 已提交
542
        console.log('  BEGIN >> ' + (' Build ' + name.bold + ' ').inverse);
543
        buildBundle(name == 'lib' ? 'seperate' : name, function () {
544 545 546
            console.log('    END >> ' + (' Build ' + name.bold + ' completed. ').green.inverse);
            callback();
        });
C
Catouse 已提交
547 548
    });

549
    gulp.task('watch:' + name, function () {
C
Catouse 已提交
550
        startWatchSrc(name);
C
Catouse 已提交
551 552 553
    });
});

554
gulp.task('minJSON', function (cb) {
C
Catouse 已提交
555 556
    gulp.src(['./docs/index.json', './docs/icons.json'])
        .pipe(jsonminify())
557
        .pipe(rename({ suffix: '.min' }))
C
Catouse 已提交
558 559 560
        .pipe(gulp.dest('./docs/'));
    gulp.src(['zui.json'])
        .pipe(jsonminify())
561
        .pipe(rename({ suffix: '.min' }))
C
Catouse 已提交
562 563 564 565
        .pipe(gulp.dest('./docs/'));
    cb();
});

566
gulp.task('prettify:js', function () {
C
Catouse 已提交
567 568 569
    return gulp.src('./src/js/**/*')
        .pipe(prettify({
            logSuccess: true,
C
Catouse 已提交
570
            config: './.jsbeautifyrc',
C
Catouse 已提交
571 572
            mode: 'VERIFY_AND_WRITE'
        }))
C
Catouse 已提交
573
        .pipe(gulp.dest('./src/js/'));
C
Catouse 已提交
574 575 576 577
});

gulp.task('prettify', ['prettify:js']);

578
gulp.task('default', function () {
C
Catouse 已提交
579 580
    buildBundle('all');
});
C
Catouse 已提交
581

582
// Init custom gulp tasks
583
if (isFileExist("gulpfile.custom.js")) {
584
    require("./gulpfile.custom.js")(gulp, {
C
Catouse 已提交
585
        chmod: chmod,
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
        less: less,
        cssmin: cssmin,
        csscomb: csscomb,
        autoprefixer: autoprefixer,
        concat: concat,
        header: header,
        uglify: uglify,
        rename: rename,
        change: change,
        sourcemaps: sourcemaps,
        prettify: prettify,
        buildBundle: buildBundle,
        zui: zui,
        pkg: pkg,
        del: del,
        mkdirp: mkdirp,
C
Catouse 已提交
602 603
        runSequence: runSequence,
        startWatchSrc: startWatchSrc
604 605
    });
}