-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
149 lines (139 loc) · 5.26 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
var gulp = require('gulp');
var minifyHtml = require('gulp-htmlmin');
var cleanCss = require('gulp-clean-css');
// var jshint = require('gulp-jshint');
var uglify = require('gulp-uglify');
var imagemin = require('gulp-imagemin');
var gulpif = require('gulp-if');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var minimist = require('minimist');
var path = require('path');
var tap = require('gulp-tap');
var notify = require('gulp-notify');
var rev = require('gulp-rev');
var revCollector = require('gulp-rev-collector');
var clean = require('gulp-clean');
var sequence = require('gulp-sequence');
var changed = require('gulp-changed');
var filter = require('gulp-filter');
// var ejs = require('gulp-ejs');
var srcPath = "src";
var buildPath = "static";
var revPath = "rev";
var jsRevPath = revPath + "/js";
var cssRevPath = revPath + "/css";
var allRevPath = revPath + '/**/*.json';
var imageSrcPath = srcPath + "/assets/**/*.*";
// var ejsSrcPath = srcPath + "/template/**/*.*";
var ejsUnbuildPath = srcPath + "/template/common/**/*.*";
var ejsBuildPath = srcPath + "/html";
var imageBuildPath = buildPath + "/assets";
var jsSrcPath = srcPath + "/js/**/*.*";
var jsBuildPath = buildPath + "/js";
var cssSrcPath = srcPath + "/css/**/*.*";
var cssBuildPath = buildPath + "/css";
var htmlSrcPath = srcPath + "/html/**/*.*";
var htmlBuildPath = buildPath + "/html";
var viewSrcPath = srcPath+"/templete/**/*.*";
var viewBuildPath = "views/";
var profilesPath = srcPath + "/profiles";
var profileJSName = '';
var knownOptions = {string: 'profiles', default: {profiles: process.env.NODE_ENV || 'dev'}};
var profiles = minimist(process.argv.slice(2), knownOptions).profiles;
var COMPRESS_PROFILES = ['uat', 'prod'];
var ONLINE_PROFILES = ['test', 'uat', 'prod'];
function isCompress() {
return (COMPRESS_PROFILES.indexOf(profiles) > -1) && ['**/*.js','**/*.css','**/*.ejs','**/*.json'];
}
function isReplace() {
return ONLINE_PROFILES.indexOf(profiles) > -1;
}
gulp.task('clean', function () {
return gulp.src([buildPath, revPath], {read: false})
.pipe(clean({force: true}))
.pipe(notify({
message: "clean <%= file.path %> directory success",
notifier: function (options) {}
}));
});
gulp.task('image', function () {
return gulp.src(imageSrcPath)
.pipe(changed(imageBuildPath))
.pipe(gulpif(isCompress(), imagemin()))
.pipe(gulp.dest(imageBuildPath))
.pipe(gulpif(isCompress(), notify({
message: "compress <%= file.path %> success",
notifier: function (options) {}
})));
});
gulp.task('js', function () {
const f = filter(['**','!src/js/lib/**/*.*'], {restore: true});
return gulp.src(jsSrcPath)
.pipe(changed(jsBuildPath))
.pipe(f)
.pipe(gulpif(isCompress(), rev()))
.pipe(gulpif(isCompress(), uglify()))
.pipe(f.restore)
.pipe(gulp.dest(jsBuildPath))
.pipe(gulpif(isCompress(), notify({
message: "compress <%= file.path %> success",
notifier: function (options) {}
})))
.pipe(gulpif(isCompress(), rev.manifest()))
.pipe(gulpif(isCompress(), gulp.dest(jsRevPath)));
});
gulp.task('css', function () {
return gulp.src(cssSrcPath)
.pipe(changed(cssBuildPath))
.pipe(gulpif(isCompress(), rev()))
.pipe(gulpif(isCompress(), cleanCss()))
.pipe(gulp.dest(cssBuildPath))
.pipe(gulpif(isCompress(), notify({
message: "compress <%= file.path %> success",
notifier: function (options) {}
})))
.pipe(gulpif(isCompress(), rev.manifest()))
.pipe(gulpif(isCompress(), gulp.dest(cssRevPath)));
});
gulp.task('view', function () {
return gulp.src([allRevPath, viewSrcPath])
.pipe(gulpif(isCompress(), revCollector({replaceReved: true})))
.pipe(gulp.dest(viewBuildPath))
.pipe(gulpif(isCompress(), notify({
message: "compress <%= file.path %> success",
notifier: function (options) {}
})));
});
gulp.task('profiles-replace', function () {
return gulp.src(jsBuildPath + '/profiles*.js')
.pipe(gulpif(isReplace(), clean({force: true})))
.pipe(gulpif(isReplace(), tap(function (file) {
profileJSName = path.basename(file.path);
})))
.pipe(gulpif(isReplace(), notify({
message: "delete <%= file.path %> success",
notifier: function (options) {}
})));
});
gulp.task('profiles', ['profiles-replace'], function () {
return gulp.src(profilesPath + '/' + profiles + '/profiles.js')
.pipe(gulpif(isCompress(), uglify()))
.pipe(gulpif(isCompress(), rename(profileJSName)))
.pipe(gulpif(isReplace(), notify({
message: "replace <%= file.path %> success",
notifier: function (options) {}
})))
.pipe(gulp.dest(jsBuildPath))
.pipe(gulpif(isCompress(), notify({
message: "compress <%= file.path %> success",
notifier: function (options) {}
})));
});
gulp.task('watch',function () {
gulp.watch(jsSrcPath,['js']);
gulp.watch(cssSrcPath,['css']);
gulp.watch(imageSrcPath,['image']);
gulp.watch(viewSrcPath,['view']);
});
gulp.task('default', sequence('clean', ['js', 'image', 'css'],'view'));