var path = require('path');
var fs = require('fs');
var yargs = require('yargs').argv;
var gulp = require('gulp');
var less = require('gulp-less');
var header = require('gulp-header');
var tap = require('gulp-tap');
var nano = require('gulp-cssnano');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var rename = require('gulp-rename');
var sourcemaps = require('gulp-sourcemaps');
var browserSync = require('browser-sync');
var pkg = require('./package.json');
var option = {
base: 'src'}
;
var dist = __dirname + '/dist';
gulp.task('build:style', function (){
var banner = ['/*!', ' * WeUI v<%= pkg.version %> (<%= pkg.homepage %>)', ' * Copyright <%= new Date().getFullYear() %> Tencent, Inc.', ' * Licensed under the <%= pkg.license %> license', ' */', ''] .join('\n');
gulp.src('src/style/weui.less', option).pipe(_AN_Call_init('init', sourcemaps)).pipe(less().on('error', function (e){
console.error(e.message);
this.emit('end');
}
)).pipe(postcss([autoprefixer] )).pipe(header(banner, {
pkg: pkg}
)).pipe(_AN_Call_write('write', sourcemaps)).pipe(gulp.dest(dist)).pipe(_AN_Call_reload('reload', browserSync, {
stream: true }
)).pipe(nano()).pipe(rename(function (path){
path.basename += '.min';
}
)).pipe(gulp.dest(dist));
}
);
gulp.task('build:example:assets', function (){
gulp.src('src/example/**/*.?(png|jpg|gif|js)', option).pipe(gulp.dest(dist)).pipe(_AN_Call_reload('reload', browserSync, {
stream: true }
));
}
);
gulp.task('build:example:style', function (){
gulp.src('src/example/example.less', option).pipe(less().on('error', function (e){
console.error(e.message);
this.emit('end');
}
)).pipe(postcss([autoprefixer] )).pipe(nano()).pipe(gulp.dest(dist)).pipe(_AN_Call_reload('reload', browserSync, {
stream: true }
));
}
);
gulp.task('build:example:html', function (){
gulp.src('src/example/index.html', option).pipe(tap(function (file){
var dir = path.dirname(file.path);
var contents = file.contents.toString();
contents = _AN_Call_replace('replace', contents, //gi, function (match, $1){
var filename = path.join(dir, $1);
var id = path.basename(filename, '.html');
var content = fs.readFileSync(filename, 'utf-8');
return '';
}
);
file.contents = new Buffer(contents);
}
)).pipe(gulp.dest(dist)).pipe(_AN_Call_reload('reload', browserSync, {
stream: true }
));
}
);
gulp.task('build:example', ['build:example:assets', 'build:example:style', 'build:example:html'] );
gulp.task('release', ['build:style', 'build:example'] );
gulp.task('watch', ['release'] , function (){
gulp.watch('src/style/**/*', ['build:style'] );
gulp.watch('src/example/example.less', ['build:example:style'] );
gulp.watch('src/example/**/*.?(png|jpg|gif|js)', ['build:example:assets'] );
gulp.watch('src/**/*.html', ['build:example:html'] );
}
);
gulp.task('server', function (){
yargs.p = yargs.p || 8080;
_AN_Call_init('init', browserSync, {
server: {
baseDir: "./dist"}
,
ui: {
port: yargs.p + 1,
weinre: {
port: yargs.p + 2}
}
,
port: yargs.p,
startPath: '/example'}
);
}
);
gulp.task('default', ['release'] , function (){
if (yargs.s) {
gulp.start('server');
}
if (yargs.w) {
gulp.start('watch');
}
}
);