-
Notifications
You must be signed in to change notification settings - Fork 5
/
gulpfile.js
302 lines (271 loc) · 9.61 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
'use strict';
// Load Node Modules/Plugins
const browserSync = require('browser-sync');
const config = require('./gulpfile-config');
const del = require('del');
const childProcess = require('child_process');
const childProcessPromise = require('child-process-promise');
const gulp = require('gulp-help')(require('gulp'));
const gulpPlugin = require('gulp-load-plugins')({lazy: true});
const merge = require('merge-stream');
var packJson = require('./package.json');
const runSequence = require('run-sequence');
//NOTE: this hides tasks from 'gulp help' console output,
// but leaves them runnable as dependent tasks
let hideFromTaskList = false;
/**
* Builds runnable app
*/
gulp.task('build',
'Builds developer-mode app in the build directory',
['build-html', 'build-js', 'build-jspm', 'build-css', 'build-img', 'build-bower'],
(cb) => {
// the 'lint' task will be run after all of the
// dependent tasks have completed
runSequence('lint', cb);
});
/**
* Builds Bower Components
*/
gulp.task('build-bower',
hideFromTaskList,
() => {
return gulp.src(config.src.bower)
.pipe(gulp.dest(`${config.dir.build}/bower_components`));
});
/**
* Builds CSS
*/
gulp.task('build-css',
hideFromTaskList,
() => {
return gulp.src(config.src.css)
.pipe(gulp.dest(config.dir.build));
});
/**
* Builds HTML
*/
gulp.task('build-html',
hideFromTaskList,
() => {
return gulp.src(config.src.html)
.pipe(gulp.dest(config.dir.build));
});
/**
* Builds Images
*/
gulp.task('build-img',
hideFromTaskList,
() => {
return gulp.src(config.src.img)
.pipe(gulp.dest(config.dir.build));
});
/**
* Builds JavaScript
*/
gulp.task('build-js',
hideFromTaskList,
() => {
let appjs = gulp.src(config.src.js)
.pipe(gulp.dest(config.dir.build))
.on('end', () => {
// after *.js is copied from src to build,
// copy config.service.dev.js to config.service.js
gulp.src(`${config.dir.build}/components/config/config.service.dev.js`)
.pipe(gulpPlugin.rename('components/config/config.service.js'))
.pipe(gulp.dest(config.dir.build));
});
let config_js = gulp.src('./config.js')
.pipe(gulp.dest(`${config.dir.build}/components`));
return merge(appjs, config_js);
});
/**
* Ensures that JSPM artifacts are copied to the build directory
*/
gulp.task('build-jspm',
hideFromTaskList,
() => {
// copy all jspm_packages
let jspm_packages = gulp.src(`${config.dir.jspm}/**/*`)
.pipe(gulp.dest(`${config.dir.build}/jspm_packages`));
return merge(jspm_packages);
});
/**
* Only rebuilds src code, ignoring bower, jspm dependencies
*/
gulp.task('build-src',
hideFromTaskList,
['build-html', 'build-js', 'build-css', 'build-img'],
(cb) => {
// the 'lint' task will be run after all of the
// dependent tasks have completed
runSequence('lint', cb);
});
/**
* Deletes generated artifacts
*/
gulp.task('clean',
'Deletes generated artifacts (build, dist)',
(cb) => {
del.sync([config.dir.build, config.dir.dist]);
return cb();
});
/**
* Copies the build directory -> dist directory
*/
gulp.task('copy-build-to-dist',
hideFromTaskList,
() => {
return gulp.src(`${config.dir.build}/**/*.*`)
.pipe(gulp.dest(config.dir.dist))
.on('end', () => {
// after *.js is copied from build to dist,
// copy config.service.prod.js to config.service.js
gulp.src(`${config.dir.dist}/components/config/config.service.prod.js`)
.pipe(gulpPlugin.rename('components/config/config.service.js'))
.pipe(gulp.dest(config.dir.dist));
});
});
/**
* Default task
*/
gulp.task('default',
'*** Default task ***',
['help']);
/**
* Builds production app
*/
gulp.task('dist',
'Builds production-ready app in the dist directory',
(cb) => {
runSequence(
['reset', 'reset-npm'],
'npm-install',
'build',
'copy-build-to-dist',
'enable-prod-mode',
'jspm-bundle',
cb
);
},
console.log('\n ///////////////////////////////////'),
console.log(' Version: ' + packJson.version),
console.log(' /////////////////////////////////// \n'));
/**
* Enables ProdMode in Angular2
*/
gulp.task('enable-prod-mode',
hideFromTaskList,
() => {
return gulp.src(`${config.dir.dist}/components/main.js`)
.pipe(gulpPlugin.replace('false;//set by gulpfile.js', 'true;'))
.pipe(gulp.dest(`${config.dir.dist}/components/`));
});
/**
* Performs a 'jspm bundle' to concatenate + minify JS dependencies
* in dist directory
*/
gulp.task('jspm-bundle',
hideFromTaskList,
() => {
return gulp.src(`${config.dir.dist}/components/main.js`)
.pipe(gulpPlugin.sourcemaps.init())
.pipe(gulpPlugin.jspm({minify:true, mangle:false}))
.pipe(gulpPlugin.sourcemaps.write('.'))
.pipe(gulp.dest(config.dir.dist));
});
/**
* Lints JavaScript files for syntax, styling errors and warnings
*/
gulp.task('lint',
hideFromTaskList,
() => {
return gulp.src(['src/**/*.js','!node_modules/**', '!jspm_packages/**', '!config.js'])
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe(gulpPlugin.eslint())
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(gulpPlugin.eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(gulpPlugin.eslint.failAfterError());
});
/**
* Installs node dependencies listed in package.json
*/
gulp.task('npm-install',
hideFromTaskList,
() => {
let options = {'capture': ['stdout', 'stderr']};
return childProcessPromise.spawn('npm', ['install'], options)
.catch(function (err) {
console.error('npm install ERROR: ', err.stderr);
});
});
/**
* Deletes generated and downloaded artifacts (jspm_packages, bower_components)
*/
gulp.task('reset',
'Deletes generated (build, dist) ' +
'and downloaded artifacts (jspm_packages, bower_components)',
(cb) => {
del.sync([config.dir.build, config.dir.dist, config.dir.jspm, config.dir.bower]);
if (process.argv.indexOf('--hard') >= 0) {
del.sync([config.dir.npm]);
}
return cb();
});
/**
* Deletes node_modules directory
*/
gulp.task('reset-npm',
hideFromTaskList,
(cb) => {
del.sync([config.dir.npm]);
return cb();
});
/**
* Runs website (dev-mode)
*/
gulp.task('run',
'Runs developer-mode website from the build directory',
(cb) => {
// run server
let server = childProcess.spawn('npm', ['start']);
server.stdout.on('data', (data) => console.log(`${data}`) );
server.stderr.on('data', (data) => console.log(`${data}`) );
server.on('close', (code) => console.log(`Server stopped: ${code}`) );
console.log('\n ///////////////////////////////////');
console.log(' Version: ' + packJson.version);
console.log(' /////////////////////////////////// \n');
cb();
});
/**
* Runs browsersync to keep browser in sync with source code during development
*/
gulp.task('sync',
'Runs Browsersync to keep browser in sync with source code during development',
['run'],
() => {
let hostname = 'localhost';
let webport = process.env.PORT || 8000;
// Browsersync will watch for changes in /build
// and restart automagically
let options = {
browser: ['google chrome'],
files: [config.dir.build + '/**/*.*'],
open: false,
port: 4000,
proxy: {
target: `${hostname}:${webport}`,
ws: true
},
reloadOnRestart: true
};
browserSync.init(options);
console.log(`Visit http://${hostname}:${options.port} for resync`);
// Gulp will watch for changes in /src/client
// and rebuild automagically, triggering a BrowserSync restart
return gulp.watch(`${config.dir.client}/**/*.*`, ['build-src']);
});