forked from sameenzm/sameenblog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
93 lines (85 loc) · 2.92 KB
/
Copy pathgulpfile.js
File metadata and controls
93 lines (85 loc) · 2.92 KB
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
var gulp = require('gulp');
var rename = require('gulp-rename');
var dot = require('gulp-seajs-dot');
var css = require('gulp-seajs-css');
var del = require('del');
var DIST = './dist/';
var DIST_APP = DIST + 'app/';
var SRC = 'src/';
var APP = SRC + 'app/';
gulp.task('clean', function () {
del.sync(DIST);
gulp.src(['./src/index.html', './src/lib/**/*', './src/mock/**/*', './src/vendor/**/*'], {base: './src/'})
.pipe(gulp.dest(DIST));
});
gulp.task('copy', handleCopy);
function handleCopy(path) {
path = typeof path === 'string' ? path : APP + '**/*.js';
gulp.src(path, {base: APP})
.pipe(gulp.dest(DIST_APP));
}
function handleDelete(path){
if(typeof path === 'string'){
path = path.substr(path.lastIndexOf(SRC.replace('/', '\\'))).replace(SRC.replace('/', ''), DIST);
var pathArr = path.split('\\');
var arr = pathArr[pathArr.length - 1].split('.');
if(path.indexOf('.html') != -1) {
pathArr[pathArr.length - 1] = 'tpl_' + arr[0] + '.js';
} else if (path.indexOf('.css') != -1) {
pathArr[pathArr.length - 1] = 'css_' + arr[0] + '.js';
}
path = pathArr.join('\\');
} else {
path = DIST_APP + '**/*.js';
}
del.sync(path);
}
// CSS压缩、包装成seajs module
gulp.task('css', handleCss);
function handleCss(path) {
path = typeof path === 'string' ? path : APP + '**/*.css';
gulp.src(path, {base: APP})
.pipe(css({prefix: 'css_'}))
.pipe(rename(function(path){
path.basename = 'css_'+path.basename;
}))
.pipe(gulp.dest(DIST_APP));
}
// doT模板预编译、包装成seajs module
gulp.task('dot', handleDoT);
function handleDoT(path) {
path = typeof path === 'string' ? path : APP + '**/*.html';
gulp.src(path, {base: APP})
.pipe(dot({prefix: 'tpl_'}))
.pipe(rename(function (path) {
path.basename = 'tpl_' + path.basename;
}))
.pipe(gulp.dest(DIST_APP));
}
gulp.task('watch', function (){
gulp.watch([SRC + '**/*.js', SRC + '**/*.json'], function (vinyl) {
console.log('watch copy', vinyl);
switch ( vinyl.type ){
case 'added' :
case 'changed' : handleCopy(vinyl.path); break;
case 'deleted' : handleDelete(vinyl.path); break;
}
});
gulp.watch(APP + '**/*.html', function (vinyl) {
console.log('watch Dot', vinyl);
switch ( vinyl.type ){
case 'added' :
case 'changed' : handleDoT(vinyl); break;
case 'deleted' : handleDelete(vinyl.path); break;
}
});
gulp.watch(APP + '**/*.css', function (vinyl) {
console.log('watch Css', vinyl);
switch ( vinyl.type ) {
case 'added' :
case 'changed' : handleCss(vinyl); break;
case 'deleted' : handleDelete(vinyl.path); break;
}
});
});
gulp.task('default', ['clean', 'copy', 'dot', 'css']);