-
Notifications
You must be signed in to change notification settings - Fork 10
/
gulpfile.js
410 lines (378 loc) · 11.2 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright (c) 2015-2017, Michael A. Updike All rights reserved.
* Licensed under the BSD-3-Clause
* https://opensource.org/licenses/BSD-3-Clause
* https://github.com/opus1269/photo-screen-saver/blob/master/LICENSE.md
*/
'use strict';
// paths and files
const base = {
app: 'photo-screen-saver',
src: 'app/',
dist: 'dist/',
dev: 'dev/',
store: 'store/',
docs: 'docs/',
tmp_docs: '../tmp_jsdoc_photoscreensaver/',
};
const path = {
scripts: `${base.src}scripts/`,
html: `${base.src}html/`,
elements: `${base.src}elements/`,
styles: `${base.src}styles/`,
images: `${base.src}images/`,
assets: `${base.src}assets/`,
lib: `${base.src}lib/`,
locales: `${base.src}_locales/`,
bower: `${base.src}bower_components/`,
bowerScripts: `${base.src}bower_components/chrome-extension-utils/`,
bowerElements: `${base.src}bower_components/setting-elements/`,
};
const files = {
manifest: `${base.src}manifest.json`,
scripts: `${path.scripts}**/*.js`,
html: `${path.html}**/*.html`,
styles: `${path.styles}**/*.*`,
elements: `${path.elements}**/*.html`,
images: `${path.images}*.*`,
assets: `${path.assets}*.*`,
lib: `${path.lib}**/*.*`,
locales: `${path.locales}**/*.*`,
bower: [
`${path.bower}**/*`,
`!${path.bowerScripts}**/*`,
`!${path.bower}**/test/*`,
`!${path.bower}**/demo/*`,
`!${path.bower}jquery/**`,
],
bowerScripts: `${path.bowerScripts}**/*.js`,
bowerElements: `${path.bowerElements}**/*.html`,
prodDelete: [
`${base.dist}app/bower_components/`,
`${base.dist}app/scripts/**/*.js`,
`!${base.dist}app/scripts/background/background.js`,
`!${base.dist}app/scripts/options/options.js`,
`!${base.dist}app/scripts/screensaver/screensaver.js`,
],
};
files.js = [files.scripts, files.bowerScripts, `${base.src}*.js`];
files.lintdevjs = '*.js';
// command options
const watchOpts = {
verbose: true,
base: '.',
};
const minifyOpts = {
output: {
beautify: true,
comments: '/Copyright/',
},
};
const crisperOpts = {
scriptInHead: false,
};
const vulcanizeOpts = {
stripComments: true,
inlineCss: true,
inlineScripts: true,
excludes: [`${path.lib}Snoocore-browser.min.js`],
};
// flag for watching
let isWatch = false;
// flag for production release build
let isProd = false;
// flag to keep key in production build for testing purposes
let isProdTest = false;
const gulp = require('gulp');
const del = require('del');
const runSequence = require('run-sequence');
const If = require('gulp-if');
const util = require('gulp-util');
const watch = require('gulp-watch');
const plumber = require('gulp-plumber');
// for ECMA6
const uglifyjs = require('uglify-es');
const composer = require('gulp-uglify/composer');
const minify = composer(uglifyjs, console);
// load the rest
const plugins = require('gulp-load-plugins')({
pattern: ['gulp-*', 'gulp.*'],
replaceString: /\bgulp[-.]/,
});
// to get the current task name
let currentTaskName = '';
gulp.Gulp.prototype.__runTask = gulp.Gulp.prototype._runTask;
gulp.Gulp.prototype._runTask = function(task) {
currentTaskName = task.name;
this.__runTask(task);
};
// Default - watch for changes in development
gulp.task('default', ['incrementalBuild']);
// Incremental Development build
gulp.task('incrementalBuild', (cb) => {
isWatch = true;
runSequence('lint', [
'bower',
'manifest',
'html',
'lintdevjs',
'scripts',
'styles',
'elements',
'images',
'assets',
'lib',
'locales',
], cb);
});
// Development build
gulp.task('dev', (cb) => {
isProd = false;
isProdTest = false;
runSequence('clean', [
'bower',
'manifest',
'html',
'scripts',
'styles',
'elements',
'images',
'assets',
'lib',
'locales',
], cb);
});
// Production build
gulp.task('prod', (cb) => {
isProd = true;
isProdTest = false;
runSequence('clean', 'html', [
'manifest',
'scripts',
'styles',
'vulcanize_options',
'vulcanize_screensaver',
'vulcanize_background',
'images',
'assets',
'lib',
'locales',
'docs',
], 'prod_delete', 'zip', cb);
});
// Production test build
gulp.task('prodTest', (cb) => {
isProd = true;
isProdTest = true;
runSequence('clean', 'html', [
'manifest',
'scripts',
'styles',
'vulcanize_options',
'vulcanize_screensaver',
'vulcanize_background',
'images',
'assets',
'lib',
'locales',
], 'prod_delete', 'zip', cb);
});
// Generate JSDoc
gulp.task('docs', (cb) => {
const config = require('./jsdoc.json');
const README = 'README.md';
gulp.src([
README,
files.scripts,
files.bowerScripts,
files.elements,
files.bowerElements,
], {read: true}).
pipe(If('*.html', plugins.crisper(crisperOpts))).
pipe(gulp.dest(base.tmp_docs)).
pipe(plugins.jsdoc3(config, cb));
});
// polylint elements
gulp.task('polylint', () => {
const input = files.elements;
const opts = {
verbose: true,
name: currentTaskName,
};
return gulp.src(input).
pipe(isWatch ? watch(input, opts) : util.noop()).
pipe(plugins.polylint({noRecursion: true})).
pipe(plugins.polylint.reporter(plugins.polylint.reporter.stylishlike)).
pipe(plugins.polylint.reporter.fail({
buffer: false,
ignoreWarnings: false,
}));
});
// clean output directories
gulp.task('clean', () => {
return del(isProd ? base.dist : base.dev);
});
// clean output directories
gulp.task('clean_all', () => {
return del([base.dist, base.dev]);
});
// manifest.json
gulp.task('manifest', () => {
const input = files.manifest;
watchOpts.name = currentTaskName;
return gulp.src(input, {base: '.'}).
pipe(isWatch ? watch(input, watchOpts) : util.noop()).
pipe(plumber()).
pipe((isProd && !isProdTest) ? plugins.stripLine('"key":') : util.noop()).
pipe(isProd ? gulp.dest(base.dist) : gulp.dest(base.dev));
});
// prep bower files
gulp.task('bower', () => {
const input = files.bower;
watchOpts.name = currentTaskName;
return gulp.src(input, {base: '.'}).
pipe(isWatch ? watch(input, watchOpts) : util.noop()).
pipe(plumber()).
pipe(If('*.html', plugins.crisper(crisperOpts))).
pipe(gulp.dest(base.dev));
});
// lint development js files
gulp.task('lintdevjs', () => {
const input = files.lintdevjs;
watchOpts.name = currentTaskName;
return gulp.src(input, {base: '.'}).
pipe(isWatch ? watch(input, watchOpts) : util.noop()).
pipe(plugins.eslint()).
pipe(plugins.eslint.formatEach()).
pipe(plugins.eslint.failOnError());
});
// lint scripts
gulp.task('lint', () => {
const input = files.js;
watchOpts.name = currentTaskName;
return gulp.src(input, {base: '.'}).
pipe(plugins.eslint()).
pipe(plugins.eslint.formatEach()).
pipe(plugins.eslint.failAfterError());
});
// scripts
gulp.task('scripts', () => {
const input = files.js;
watchOpts.name = currentTaskName;
return gulp.src(input, {base: '.'}).
pipe(isWatch ? watch(input, watchOpts) : util.noop()).
pipe(isProd ? util.noop() : plugins.replace('const _DEBUG = false',
'const _DEBUG = true')).
pipe(plugins.eslint()).
pipe(plugins.eslint.formatEach()).
pipe(plugins.eslint.failAfterError()).
pipe(isProd ? minify(minifyOpts).on('error', util.log) : util.noop()).
pipe(isProd ? gulp.dest(base.dist) : gulp.dest(base.dev));
});
// html
gulp.task('html', () => {
const input = files.html;
watchOpts.name = currentTaskName;
return gulp.src(input, {base: '.'}).
pipe(isWatch ? watch(input, watchOpts) : util.noop()).
pipe(plumber()).
pipe(isProd ? plugins.minifyHtml() : util.noop()).
pipe(isProd ? gulp.dest(base.dist) : gulp.dest(base.dev));
});
// elements
gulp.task('elements', () => {
const input = files.elements;
watchOpts.name = currentTaskName;
return gulp.src(input, {base: '.'}).
pipe(isWatch ? watch(input, watchOpts) : util.noop()).
pipe(plugins.eslint()).
pipe(plugins.eslint.formatEach()).
pipe(plugins.eslint.failAfterError()).
pipe(If('*.html', plugins.crisper(crisperOpts))).
pipe(gulp.dest(base.dev));
});
// styles
gulp.task('styles', () => {
const input = files.styles;
watchOpts.name = currentTaskName;
return gulp.src(input, {base: '.'}).
pipe(isWatch ? watch(input, watchOpts) : util.noop()).
pipe(plumber()).
pipe(If('*.css', isProd ? plugins.cleanCss() : util.noop())).
pipe(isProd ? gulp.dest(base.dist) : gulp.dest(base.dev));
});
// images
gulp.task('images', () => {
const input = files.images;
watchOpts.name = currentTaskName;
return gulp.src(input, {base: '.'}).
pipe(isWatch ? watch(input, watchOpts) : util.noop()).
pipe(plumber()).
pipe(plugins.imagemin({progressive: true, interlaced: true})).
pipe(isProd ? gulp.dest(base.dist) : gulp.dest(base.dev));
});
// assets
gulp.task('assets', () => {
const input = files.assets;
watchOpts.name = currentTaskName;
return gulp.src(input, {base: '.'}).
pipe(isWatch ? watch(input, watchOpts) : util.noop()).
pipe(plumber()).
pipe(isProd ? gulp.dest(base.dist) : gulp.dest(base.dev));
});
// lib
gulp.task('lib', () => {
const input = files.lib;
watchOpts.name = currentTaskName;
return gulp.src(input, {base: '.'}).
pipe(isWatch ? watch(input, watchOpts) : util.noop()).
pipe(plumber()).
pipe(isProd ? gulp.dest(base.dist) : gulp.dest(base.dev));
});
// locales
gulp.task('locales', () => {
const input = files.locales;
watchOpts.name = currentTaskName;
return gulp.src(input, {base: '.'}).
pipe(isWatch ? watch(input, watchOpts) : util.noop()).
pipe(plumber()).
pipe(isProd ? gulp.dest(base.dist) : gulp.dest(base.dev));
});
// vulcanize options_imports.html for production
gulp.task('vulcanize_options', () => {
return gulp.src(`${path.html}options_imports.html`, {base: '.'}).
pipe(plugins.vulcanize(vulcanizeOpts)).
pipe(plugins.crisper(crisperOpts)).
pipe(If('*.html', plugins.minifyInline())).
pipe(If('*.js', minify(minifyOpts).on('error', util.log))).
pipe(gulp.dest(base.dist));
});
// vulcanize background_imports.html for production
gulp.task('vulcanize_background', () => {
return gulp.src(`${path.html}background_imports.html`, {base: '.'}).
pipe(plugins.vulcanize(vulcanizeOpts)).
pipe(plugins.crisper(crisperOpts)).
pipe(If('*.html', plugins.minifyInline())).
pipe(If('*.js', minify(minifyOpts).on('error', util.log))).
pipe(gulp.dest(base.dist));
});
// vulcanize screensaver_imports.html for production
gulp.task('vulcanize_screensaver', () => {
return gulp.src(`${path.html}screensaver_imports.html`, {base: '.'}).
pipe(plugins.vulcanize(vulcanizeOpts)).
pipe(plugins.crisper(crisperOpts)).
pipe(If('*.html', plugins.minifyInline())).
pipe(If('*.js', minify(minifyOpts).on('error', util.log))).
pipe(gulp.dest(base.dist));
});
// delete unneeded files
gulp.task('prod_delete', () => {
return del(files.prodDelete);
});
// compress for the Chrome Web Store
gulp.task('zip', () => {
return gulp.src(`${base.dist}${base.src}**`).
pipe(!isProdTest ? plugins.zip('store.zip') : plugins.zip(
'store-test.zip')).
pipe(!isProdTest ? gulp.dest(base.store) : gulp.dest(base.dist));
});