-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
210 lines (191 loc) · 4.91 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var concat = require('gulp-concat');
var connect = require('gulp-connect');
var del = require('del');
var fs = require('fs');
var gulp = require('gulp');
var gutils = require('gutils');
var inject = require('gulp-inject');
var karmaServer = require('karma').Server;
var less = require('gulp-less');
var merge = require('merge-stream');
var minifycss = require('gulp-minify-css');
var runsequence = require('run-sequence');
var uglify = require('gulp-uglify');
var watch = require('gulp-watch');
var src = {
folder: 'src/',
main: 'src/index.html',
less: [
'src/less/*.less',
'!src/less/_*.less'
],
js: 'src/js/app.js',
folders : ['src/**', '!src/js/*.*', '!src/css/*.*'],
bower: {
js: [
'bower_components/angular/angular.min.js',
'bower_components/angular-route/angular-route.min.js'
],
css: [
'bower_components/bootstrap/dist/css/bootstrap.min.css'
]
}
};
var dest = {
folder: 'build/',
main: 'build/index.html',
js: 'build/js/',
css: 'build/css/'
}
// noop for default task
gulp.task('default', gutils.noop);
// clean 'build' folder
gulp.task('clean', function(cb) {
del(dest.folder).then(function() {
cb();
});
});
// move bower files to 'build' folder
gulp.task('bower', function() {
fs.mkdirSync(dest.folder);
var js = gulp.src(src.bower.js.concat(src.js))
.pipe(gulp.dest(dest.js));
var css = gulp.src(src.bower.css)
.pipe(gulp.dest(dest.css));
var folders = gulp.src(src.folders)
.pipe(gulp.dest(dest.folder));
var html = gulp.src(src.main)
.pipe(gulp.dest(dest.folder));
return merge(js, css, folders, html);
});
// inject css and js files into index.html
gulp.task('html', function() {
return gulp.src(src.main).pipe(inject(gulp.src([
dest.js + 'angular.min.js',
dest.js + '*.js',
dest.css + 'bootstrap.min.css',
dest.css + '*.css',
]), {
relative: false,
ignorePath : dest.folder,
addRootSlash : false
}))
.pipe(gulp.dest(dest.folder));
});
// build folder to be served
gulp.task('build', function(cb) {
return runsequence('clean', 'bower', 'less', 'html', function() {
cb();
});
});
// compile css and javascript for distribution, remove useless files and folders
gulp.task('pre', function(cb) {
return runsequence('compile', 'useless', cb);
});
// uglify app.js
// @TODO investigate why minification lead to error when using $routeProvider
gulp.task('uglify', function() {
return gulp.src(dest.js + 'app.js')
.pipe(uglify({
mangle : true
}))
.pipe(gulp.dest(dest.js));
});
// compile css and javascript
gulp.task('compile', function() {
var css = gulp.src([
dest.css + 'bootstrap.min.css',
dest.css + '*.css',
])
.pipe(concat('app.min.css'))
.pipe(gulp.dest(dest.css));
var js = gulp.src([
dest.js + 'angular.min.js',
dest.js + 'angular-*.js',
dest.js + '*.js'
])
.pipe(concat('app.min.js'))
.pipe(gulp.dest(dest.js));
return merge(css, js);
});
// remove useless files and folders
gulp.task('useless', function(cb) {
del([
dest.css + '*.css',
'!' + dest.css + 'app.min.css',
dest.js + '*.js',
'!' + dest.js + 'app.min.js',
dest.folder + 'less'
]).then(function() {
cb();
})
});
// compile less files and reload
gulp.task('less', function() {
return gulp.src(src.less)
.pipe(less())
.pipe(minifycss())
.pipe(concat('app.min.css'))
.pipe(gulp.dest(dest.css))
.pipe(connect.reload());
});
// move javascript file and reload
gulp.task('js', function() {
return gulp.src(src.js)
.pipe(gulp.dest(dest.js))
.pipe(connect.reload());
});
// move folders and reload
gulp.task('folders', function() {
return gulp.src(src.folders)
.pipe(gulp.dest(dest.folder))
.pipe(connect.reload());
});
// watch source files and folders
gulp.task('watch', function(cb) {
var list = [
src.folder + 'src/*.*',
src.folder + 'src/**'
];
watch(list, function(vinyl) {
var newPath = vinyl.path.split('src').join('build');
var type = vinyl.event;
if (type === 'unlink') {
// @TODO better approach?
fs.unlinkSync(newPath);
} else {
fs.createReadStream(vinyl.path).pipe(fs.createWriteStream(newPath));
}
gulp.src(list)
.pipe(watch(list))
.on('end', cb);
});
gulp.watch(src.less, ['less']);
gulp.watch(src.js, ['js']);
gulp.watch(src.folders, ['folders', 'reload']);
gulp.watch(src.main, ['reload']);
});
// reload
gulp.task('reload', ['html'], function() {
return gulp.src(dest.main)
.pipe(connect.reload());
});
// serve (after build) with watch
gulp.task('serve', ['build'], function() {
gulp.start('watch');
connect.server({
root: dest.folder,
port: 12345,
livereload: true
});
});
// prepare folder to be distribted
gulp.task('dist', function(cb) {
return runsequence('clean', 'bower', 'less', 'pre', 'html', cb);
});
// run tests
gulp.task('test', function (cb) {
new karmaServer({
configFile: __dirname + '/karma.conf.js'
}, cb).start();
});