forked from ChrisBoesch/singpathfire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
279 lines (240 loc) · 6.42 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* eslint-env node */
/* eslint strict: [2, "global"] */
'use strict';
var concat = require('gulp-concat');
var debug = require('gulp-debug');
var del = require('del');
var gulp = require('gulp');
var gulpIf = require('gulp-if');
var lazypipe = require('lazypipe');
var minifyCSS = require('gulp-minify-css');
var minimist = require('minimist');
var path = require('path');
var rename = require('gulp-rename');
var rev = require('gulp-rev');
var revReplace = require('gulp-rev-replace');
var targetHTML = require('gulp-targethtml');
var templateCache = require('gulp-angular-templatecache');
var uglify = require('gulp-uglify');
var useref = require('gulp-useref');
var argv = minimist(process.argv);
var apps = [
'classmentors',
'singpath'
];
var config = {
src: 'src/',
watch: ['src/**/*.html', 'src/**/*.css', 'src/**/*.js', '!src/vendor/**/*'],
apps: apps,
pages: 'src/*.html',
appFiles: apps.map(function(app) {
return 'src/' + app + '/**/*';
}),
vendorFiles: 'src/vendor/**/*',
assetsFiles: 'src/assets/**/*',
sharedFiles: 'src/shared/**/*',
build: {
concat: './build',
debug: './build-debug',
dev: './build-dev',
dist: './dist',
e2e: './build-e2e'
},
ace: {
assets: [
'src/vendor/ace-builds/mode-html.js',
'src/vendor/ace-builds/mode-javascript.js',
'src/vendor/ace-builds/mode-python.js',
'src/vendor/ace-builds/theme-twilight.js',
'src/vendor/ace-builds/worker-html.js',
'src/vendor/ace-builds/worker-javascript.js',
'src/vendor/ace-builds/worker-html.js'
],
base: 'src/vendor/ace-builds'
},
// only used for build:concat and dist
dest: argv.dest ? path.resolve(argv.dest) : null,
noduleNames: {
singpath: 'spf',
classmentors: 'clm'
}
};
/**
* Simply copy build and process index.html for targets
*
*/
function copyBuid(target, dest) {
dest = dest || config.build[target];
return gulp.src([config.pages], {
base: config.src
})
.pipe(targetHTML(target))
.pipe(gulp.src([config.vendorFiles, config.assetsFiles, config.sharedFiles].concat(config.appFiles), {
base: config.src,
passthrough: true
}))
.pipe(gulp.dest(dest));
}
var compilers = config.apps.reduce(function(prev, appName) {
prev[appName] = lazypipe()
.pipe(gulp.src, [
config.src + appName + '/**/*.html',
config.src + 'shared/**/*.html',
config.src + appName + '/**/*.svg',
config.src + 'shared/**/*.svg'
], {
base: config.src,
passthrough: true
})
.pipe(function() {
return gulpIf(/.+\.(html|svg)/, templateCache({
module: config.noduleNames[appName],
base: path.resolve(config.src)
}));
})
.pipe(concat, 'app.js');
return prev;
}, {});
/**
* Stream that build and dist tasks will share
*
*/
function concatBuild(appName) {
var assets = useref.assets({searchPath: config.src});
return gulp.src([config.src + appName + '.html'], {
base: config.src
})
.pipe(rename('index.html'))
// remove some of the html block
.pipe(targetHTML('live'))
// Concat scrips (css and js).
.pipe(assets)
.pipe(assets.restore())
.pipe(useref())
// Add compiled html templates and svg icons into app.js
.pipe(gulpIf(/app\.js/, compilers[appName]()))
// Add ace assets
.pipe(gulp.src(config.ace.assets, {
base: config.ace.base,
passthrough: true
}));
}
/**
* Stream shared by the 3 app concat build tasks.
*
*/
function buildApp(appName, dest) {
dest = dest || config.build.concat;
// Append a hash to most assets file
return concatBuild(appName)
.pipe(gulpIf(/(app|vendor)\.(js|css)/, rev()))
.pipe(revReplace())
.pipe(gulp.dest(dest + '/' + appName));
}
/**
* Stream shared by the 3 app dist tasks
*
*/
function buildAppDist(appName, dest) {
dest = dest || config.build.dist;
// Append a hash to all assets file
return concatBuild(appName)
.pipe(gulpIf('*.js', uglify()))
.pipe(gulpIf('*.css', minifyCSS()))
.pipe(gulpIf(/(app|vendor)\.(js|css)/, rev()))
.pipe(revReplace())
.pipe(gulp.dest(dest + '/' + appName));
}
/**
* Delete all build reportories (build/, dist/, debug/ and e2e/)
*
*/
function clean(done) {
var folders = Object.keys(config.build).map(function(k) {
return config.build[k];
});
if (config.dest) {
folders.push(config.dest);
}
del(folders, {
force: true
}, done);
}
gulp.task('clean', clean);
/**
* Copy src/ to build-dev/ and tweak mocking.
*
*/
function buildDev() {
return copyBuid('dev', config.build.dev);
}
gulp.task('build:dev', gulp.series(clean, buildDev));
/**
* Copy src/ to debug/ and remove any mocking.
*
*/
function buildDebug() {
return copyBuid('live', config.build.debug);
}
gulp.task('build:debug', gulp.series(clean, buildDebug));
/**
* Copy src/ to e2e/ and remove the mocked spf module. But unlike debug,
* it keeps the loading angular-mocks and and spf ficture data. It's up
* to each e2e scenario to mock th http response.
*
*/
function buildE2E() {
return copyBuid('e2e');
}
gulp.task('build:e2e', gulp.series(clean, buildE2E));
/**
* Build the apps into build/<app name> by removing any mocking and by concataning
* assets for each app.
*/
var buildConcat = gulp.parallel(config.apps.map(function(appName) {
var taskName = 'build/concat: ' + appName;
gulp.task(taskName, function() {
return buildApp(appName, config.dest);
});
return taskName;
}));
gulp.task('build:concat', gulp.series(clean, buildConcat));
/**
* Build all app type.
*/
gulp.task(
'build',
gulp.series(
clean,
gulp.parallel(buildDev, buildDebug, buildE2E, buildConcat)
)
);
/**
* Watch tasks
*/
gulp.task('watch', gulp.parallel('build', function buildWather() {
gulp.watch(config.watch, gulp.task('build'));
}));
['dev', 'debug', 'e2e', 'concat'].forEach(function(buildType) {
var taskName = 'watch:' + buildType;
var buildTaskName = 'build:' + buildType;
gulp.task(taskName, gulp.parallel(buildTaskName, function SomeBuildWatcher() {
gulp.watch(config.watch, gulp.task(buildTaskName));
}));
});
/**
* Like build but minify css and js files too.
*
*/
var dist = gulp.parallel(config.apps.map(function(appName) {
var taskName = 'build/concat: ' + appName;
gulp.task(taskName, function() {
return buildAppDist(appName, config.dest);
});
return taskName;
}));
gulp.task('dist', gulp.series(clean, dist));
/**
* Default task
*/
gulp.task('default', gulp.parallel('build:concat'));