forked from binbobo/lieguanjia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
186 lines (163 loc) · 5.93 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//load plugins
var gulp = require('gulp'),
compass = require('gulp-compass'),
//scss = require('gulp-scss'),
autoprefixer = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
livereload = require('gulp-livereload'),
plumber = require('gulp-plumber'),
ngAnnotate = require('gulp-ng-annotate'),
del = require('del'),
path = require('path'),
gulpif = require('gulp-if'),
rev = require('gulp-rev'),
revReplace = require('gulp-rev-replace'),
replace = require('gulp-replace'),
useref = require('gulp-useref'),
htmlmin = require('gulp-html-minifier'),
print = require('gulp-print');
//the title and icon that will be used for the Grunt notifications
var notifyInfo = {
title: 'Gulp',
icon: path.join(__dirname, 'gulp.png')
};
//error notification settings for plumber
var plumberErrorHandler = {
// errorHandler: notify.onError({
// title: notifyInfo.title,
// icon: notifyInfo.icon,
// message: "Error: <%= error.message %>"
// })
};
function swallowError(error) {
// If you want details of the error in the console
console.log(error.toString());
this.emit('end');
}
//styles
gulp.task('styles', function () {
var scss = require("gulp-scss");
return gulp.src(['app/styles/*.scss'])
.pipe(scss())
// .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 7', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(gulp.dest('app/styles'));
});
gulp.task('mystyles', function () {
var scss = require("gulp-scss");
return gulp.src('app/styles/tiger.scss')
.pipe(scss())
.on('error', swallowError)
.pipe(gulp.dest('app/styles'))
.pipe(livereload());
});
var t = Math.round(new Date().getTime() / 1000);
var htmlminPipe = htmlmin({
collapseWhitespace: true,
removeComments: true,
minifyCSS: true
});
gulp.task('compile', function () {
del.sync('dist/styles/*');
del.sync('dist/scripts/*');
return gulp.src('app/index.html')
.pipe(useref())
.pipe(gulpif('**/tiger.min.js', ngAnnotate()))
.pipe(gulpif('*.min.js', uglify()))
.pipe(gulpif('*.fix.js', uglify({
mangle: false,
compress: false
})))
.pipe(gulpif('*.css', replace(/['"]views([^'"]*)\.html['"]/g, '"views$1.html?_t=' + t + '"')))
.pipe(gulpif('*.css', minifycss()))
.pipe(gulpif('!*.html', rev()))
.pipe(revReplace())
.pipe(gulp.dest('dist'));
});
gulp.task('replace-template', ['compile'], function () {
return gulp.src('dist/scripts/tiger*.js')
.pipe(replace(/['"]views([^'"]*)\.html['"]/g, '"views$1.html?_t=' + t + '"'))
.pipe(gulp.dest('dist/scripts'));
});
gulp.task('replace-micro-service-url', ['replace-template'], function () {
return gulp.src('dist/scripts/tiger*.js')
.pipe(replace('http://t.dev.lieguanjia.com/v1', 'https://t.lieguanjia.com/v1'))
.pipe(replace('http://wechat.dev.lieguanjia.com', 'https://wechat.lieguanjia.com'))
.pipe(gulp.dest('dist/scripts'));
});
gulp.task('compile-index', ['compile'], function () {
return gulp.src('dist/index.html')
.pipe(replace(/ng-include=['"] *['"]views([^'"]*)\.html['"] *['"]/g, 'ng-include="\'views$1.html?_t=' + t + '\'"'))
.pipe(replace(/ng-include="'version'"/g, 'ng-include="\'version?_t=' + t + '\'"'))
.pipe(htmlminPipe)
.pipe(gulp.dest('dist'));
});
gulp.task('template', function () {
del.sync(['dist/views/**', '!dist/views']);
return gulp.src([
'app/views/**/*.html',
'!app/views/pages/demo.html'
])
.pipe(gulpif('*about/release_note.html', replace(/<dev>([\s\S]*?)<\/dev>/igm, '')))
.pipe(replace(/ng-include=['"] *['"]views([^'"]*)\.html['"] *['"]/g, 'ng-include="\'views$1.html?_t=' + t + '\'"'))
.pipe(htmlmin({
collapseWhitespace: true,
removeComments: true
}))
.pipe(gulp.dest('dist/views'));
});
gulp.task('template-dev', function () {
del.sync(['dist/views/**', '!dist/views']);
return gulp.src([
'app/views/**/*.html',
'!app/views/pages/demo.html'
])
.pipe(htmlmin({
collapseWhitespace: true,
removeComments: true
}))
.pipe(gulp.dest('dist/views'));
});
gulp.task('copy', function () {
del.sync('dist/+(fonts|i18n|images|pdf_viewer)/**/*');
del.sync('dist/**/iconfont/*');
return gulp.src([
'app/+(fonts|i18n|images|pdf_viewer)/**/*',
'app/**/iconfont/*',
'app/favicon.ico'
]).pipe(gulp.dest('dist'));
});
gulp.task('assist-lib', ['compile'], function () {
return gulp.src('app/scripts/text-encoding/*')
.pipe(uglify())
.pipe(gulp.dest('dist/scripts/text-encoding'));
});
gulp.task('build', ['copy', 'template', 'compile', 'replace-template', 'compile-index', 'assist-lib', 'replace-micro-service-url'], function () {
});
gulp.task('build-dev', ['copy', 'template-dev', 'compile', 'replace-template', 'compile-index', 'assist-lib'], function () {
});
//watch
gulp.task('live', [
'styles', 'mystyles'
], function () {
livereload.listen({start: true});
//watch .scss files
gulp.watch('app/styles/**/*.scss', ['mystyles']);
//watch .js files
// gulp.watch('app/scripts/**/*.js', ['scripts']);
//reload when a template file, the minified css, or the minified js file changes
// gulp.watch('templates/**/*.html', 'html/css/styles.min.css', 'html/js/main.min.js', function(event) {
// gulp.src(event.path)
// .pipe(plumber())
// .pipe(livereload())
// .pipe(notify({
// title: notifyInfo.title,
// icon: notifyInfo.icon,
// message: event.path.replace(__dirname, '').replace(/\\/g, '/') + ' was ' + event.type + ' and reloaded'
// })
// );
// });
});