-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathgulpfile.js
154 lines (135 loc) · 4.29 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var fs = require('fs'),
Stream = require('stream'),
exec = require('child_process').exec,
path = require('path'),
gulp = require('gulp'),
insert = require('gulp-insert'),
eslint = require('gulp-eslint'),
uglify = require('gulp-uglify'),
stylus = require('gulp-stylus'),
rename = require('gulp-rename'),
mochaPhantomJS = require('gulp-mocha-phantomjs');
var pkg = require('./package.json'),
DIST = 'dist',
banner = '/*! '+ pkg.name +' '+ pkg.version +'\n'+
' * (c) 2012-'+ new Date().getFullYear() +' '+ pkg.author +', MIT Licensed\n'+
' * '+ pkg.homepage +'\n'+
' */';
// run eslint
gulp.task('lint', function () {
gulp.src(['src/*.js', 'src/local/*.js', 'test/unit/*.js'])
.pipe(eslint({
globals: ['jQuery'],
envs: ['browser']
}))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
// build js
gulp.task('build-js', function () {
gulp.src('src/jquery.validator.js')
.pipe(insert.prepend(banner + '\n'))
.pipe(gulp.dest(DIST))
.pipe(uglify())
.pipe(insert.prepend(banner + '\n'))
.pipe(rename({suffix:'.min'}))
.pipe(gulp.dest(DIST));
});
// build css
gulp.task('build-css', function () {
gulp.src('src/jquery.validator.styl')
.pipe(stylus())
.pipe(gulp.dest(DIST));
});
// copy images
gulp.task('copy-images', function () {
gulp.src(['src/images/*'])
.pipe(rename({dirname:'images'}))
.pipe(gulp.dest(DIST));
});
// build local settings
gulp.task('i18n', function () {
var compiler = tpl( fs.readFileSync( 'src/local/_config.tpl' ).toString() );
gulp.src('src/local/*.js')
.pipe(i18n())
.pipe(rename({dirname:'local'}))
.pipe(gulp.dest(DIST));
function i18n() {
var stream = new Stream.Transform({objectMode: true});
stream._transform = function(chunk, encoding, callback) {
var data = require(chunk.path),
str = compiler.render(data);
chunk._contents = new Buffer(str);
this.push(chunk);
callback();
};
return stream;
}
});
// run unit tests
gulp.task('test', function () {
return gulp
.src('test/index.html')
.pipe(mochaPhantomJS({
reporter: 'spec',
phantomjs: {
useColors:true
}
}));
});
gulp.task('zip', function () {
var zip = require('gulp-zip');
gulp.src([
"dist/**",
"demo/**",
"package.json",
"README.md"
], {base: './'})
.pipe(zip(pkg.name + '-' + pkg.version + '.zip'))
.pipe(gulp.dest('./'));
});
// when release a version
gulp.task('release', ['build', 'test', 'zip'], function () {
exec('git checkout master && npm publish --registry=https://registry.npmjs.org', function(e, stdout, stderr) {
console.log(stdout);
console.log(stderr);
})
});
gulp.task('build', ['lint', 'build-js', 'build-css', 'copy-images', 'i18n']);
gulp.task('default', ['build', 'test']);
// tiny template engine
function Compiler(html) {
html = html || '';
if (/\.(?=tpl|html)$/.test(html)) html = fs.readFileSync(html);
var begin = '<#',
end = '#>',
ecp = function(str){
return str.replace(/('|\\)/g, '\\$1').replace(/\r\n/g, '\\r\\n').replace(/\n/g, '\\n');
},
str = "var __='',echo=function(s){__+=s};with(_$||{}){",
blen = begin.length,
elen = end.length,
b = html.indexOf(begin),
e,
tmp;
while(b != -1) {
e = html.indexOf(end);
if(e < b) break;
str += "__+='" + ecp(html.substring(0, b)) + "';";
tmp = html.substring(b+blen, e).trim();
if( tmp.indexOf('=') === 0 ) {
tmp = tmp.substring(1);
str += "typeof(" + tmp + ")!='undefined'&&(__+=" + tmp + ");";
} else {
str += tmp;
}
html = html.substring(e + elen);
b = html.indexOf(begin);
}
str += "__+='" + ecp(html) + "'}return __";
this.render = new Function("_$", str);
}
function tpl(html, data) {
var me = new Compiler(html);
return data ? me.render(data) : me;
};