/
gulpfile.js
194 lines (165 loc) · 4.21 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
var gulp = require('gulp');
var path = require('path');
var vinylPaths = require('vinyl-paths');
var del = require('del');
var serve = require('browser-sync');
var runSequence = require('run-sequence');
var glob = require('glob');
var eslint = require('gulp-eslint');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var ngAnnotate = require('gulp-ng-annotate');
var htmlReplace = require('gulp-html-replace');
var html2js = require('gulp-ng-html2js');
var minifyCss = require('gulp-minify-css');
var ESLINT_FILE = '.eslintrc';
var root = 'src';
var dist = 'dist';
var patterns = {
js: '**/*.js',
css: '**/*.css',
html: '**/*.html',
spec: '**/*.spec.js',
tpls: '**/*.tpl.html'
}
var paths = {
js: path.join(root, '**/*.js'),
css: path.join(root, '**/*.css'),
html: path.join(root, '**/*.html'),
spec: path.join(root, '**/*.spec.js'),
tpls: path.join(root, '**/*.tpl.html'),
vendor: {
css: [
'node_modules/material-design-lite/material.css'
],
js: [
'node_modules/angular/angular.js',
'node_modules/angular-route/angular-route.js'
]
}
};
var reload = serve.reload;
function resolveGlob(pattern) {
return glob.sync(pattern).map(function (filename) {
return filename.replace('dist/', '');
});
}
var order = paths.vendor.css
.concat(paths.vendor.js)
.map(function (file) {
return path.basename(file);
});
function comparer(file1, file2) {
var idx1 = order.indexOf(file1);
var idx2 = order.indexOf(file2);
if (idx1 > -1 && idx2 > -1) {
return idx1 < idx2 ? -1 : 1;
}
if (idx1 > -1 && idx2 == -1) {
return -1;
}
if (idx1 === -1 && idx2 > -1) {
return 1;
}
return 0;
}
gulp.task('build', function (cb) {
runSequence(
'clean',
['data', 'html2js','scripts', 'scripts:vendor', 'styles', 'styles:vendor'],
'html',
cb
);
});
gulp.task('build:prod', function (cb) {
runSequence(
'build',
'scripts:prod',
'styles:prod',
'clean:dist:app',
'html',
cb
);
});
gulp.task('clean', function () {
return gulp.src(dist)
.pipe(vinylPaths(del));
});
gulp.task('clean:dist:app', function () {
return gulp.src('dist/app')
.pipe(vinylPaths(del));
});
gulp.task('scripts', function () {
return gulp.src([paths.js, '!' + paths.spec])
.pipe(eslint({
configFile: path.join(__dirname, ESLINT_FILE)
}))
.pipe(eslint.formatEach('stylish'))
.pipe(ngAnnotate())
.pipe(gulp.dest(dist));
});
gulp.task('scripts:prod', function () {
return gulp.src(path.join(dist, patterns.js))
.pipe(concat('app.min.js'))
.pipe(ngAnnotate())
.pipe(uglify())
.pipe(gulp.dest(dist));
});
gulp.task('scripts:vendor', function () {
return gulp.src(paths.vendor.js)
.pipe(gulp.dest(dist));
});
gulp.task('data', function () {
return gulp.src('./src/app/data/data.json')
.pipe(gulp.dest(dist));
});
gulp.task('styles', function () {
return gulp.src(paths.css)
.pipe(gulp.dest(dist));
});
gulp.task('styles:prod', function () {
return gulp.src(path.join(dist, patterns.css))
.pipe(concat('app.min.css'))
.pipe(minifyCss())
.pipe(gulp.dest(dist))
});
gulp.task('styles:vendor', function () {
return gulp.src(paths.vendor.css)
.pipe(gulp.dest(dist));
});
gulp.task('html', function () {
return gulp.src(path.join(root, 'index.html'))
.pipe(htmlReplace({
js: resolveGlob(path.join(dist, patterns.js)).sort(comparer),
css: resolveGlob(path.join(dist, patterns.css)).sort(comparer)
}))
.pipe(gulp.dest(dist));
});
gulp.task('html2js', function () {
return gulp.src(paths.tpls)
.pipe(html2js({
moduleName: 'templateCache',
rename: function (templateUrl, templateFile) {
return path.basename(templateUrl);
}
}))
.pipe(gulp.dest(dist))
});
gulp.task('serve', ['build'], function () {
serve({
port: process.env.PORT || 3000,
open: false,
files: [].concat(
paths.js,
paths.css,
paths.html
),
server: {
baseDir: dist
},
});
gulp.watch(paths.html, ['html', reload]);
gulp.watch(paths.tpls, ['html2js', reload]);
gulp.watch(paths.css, ['styles', reload]);
gulp.watch(paths.js, ['scripts', reload]);
});