This repository has been archived by the owner on Aug 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgulpfile.js
515 lines (442 loc) · 15.5 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
'use strict';
const gulp = require('gulp');
// system for lazy loading modules, because the gulp starting time has become quite slow with all the modules
class Modules {
constructor() {
this.loaded = {};
this.moduleNames = new Map();
this.get = module => {
if (!(module in this.loaded)) {
const packageName = this.moduleNames.get(module);
if (!packageName) {
console.error(`Module ${module} is not registered in modules.`);
} else {
try {
this.loaded[module] = require(packageName);
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
console.error(
`Package ${packageName} for module ${module} has not been found. Is it installed?`
);
} else {
throw error;
}
}
}
}
return this.loaded[module];
};
}
addModule(moduleName, packageName) {
this.moduleNames.set(moduleName, packageName);
}
addModules(obj) {
for (const module in obj) {
this.addModule(module, obj[module]);
}
}
}
let modules = new Modules();
modules.addModules({
autoprefixer: 'gulp-autoprefixer',
uglify: 'gulp-uglify',
rename: 'gulp-rename',
del: 'del',
file: 'gulp-file',
minifier: 'gulp-minifier',
watch: 'gulp-watch',
imagemin: 'gulp-imagemin',
zip: 'gulp-zip',
tar: 'gulp-tar',
gzip: 'gulp-gzip',
markdown: 'gulp-markdown',
template: 'gulp-template-html',
insert: 'gulp-insert',
replace: 'gulp-replace',
changed: 'gulp-changed',
sourcemaps: 'gulp-sourcemaps',
source: 'vinyl-source-stream',
buffer: 'vinyl-buffer',
browserify: 'browserify',
babel: 'babelify',
gulpif: 'gulp-if',
jsdoc: 'gulp-jsdoc3',
sass: 'gulp-sass',
jsoneditor: 'gulp-json-editor',
tap: 'gulp-tap',
eslint: 'gulp-eslint',
eventStream: 'event-stream'
});
const packageData = require('./package.json');
const config = packageData.config;
const out = 'dist',
outCss = out + '/css',
outJs = out + '/js',
outImg = out + '/img',
outDocs = out + '/docs',
outFonts = out + '/fonts',
outJsDocRelative = 'gen',
outJsDoc = outDocs + '/' + outJsDocRelative,
packaged = out + '/archives',
src = 'src',
srcHtml = src + '/html',
srcImg = src + '/img',
srcCss = src + '/scss',
srcJs = src + '/es6',
srcDocs = src + '/help',
srcMd = srcDocs + '/md',
srcFonts = src + '/fonts',
docs = 'docs',
docsOut = out + '/docs',
docsCss = docsOut + '/css',
srcLibrary = 'library',
outLibrary = out + '/library',
ghPages = 'gh-pages';
let production = false;
gulp.task('production', done => {
production = true;
done(); // required to signal async completion
});
// compile and minimize sass
gulp.task('styles', () => {
const sass = modules.get('sass'),
autoprefixer = modules.get('autoprefixer'),
gulpif = modules.get('gulpif'),
rename = modules.get('rename'),
minifier = modules.get('minifier');
return gulp
.src(srcCss + '/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer('last 2 version'))
.pipe(gulpif(production, rename({ suffix: '.min' })))
.pipe(gulpif(production, minifier({
minify: true,
minifyCSS: true
})))
.pipe(gulp.dest(outCss));
});
gulp.task('scripts:lint', () => {
const eslint = modules.get('eslint');
return gulp
.src(srcJs + '/**/*.js')
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
// compile and minimize es6
gulp.task('scripts:build', done => {
const browserify = modules.get('browserify'),
babel = modules.get('babel'),
source = modules.get('source'),
buffer = modules.get('buffer'),
sourcemaps = modules.get('sourcemaps'),
gulpif = modules.get('gulpif'),
rename = modules.get('rename'),
uglify = modules.get('uglify'),
eventStream = modules.get('eventStream'),
replace = modules.get('replace');
const startpoints = ['main.js', 'routeWorker.js'];
let tasks = startpoints.map(startpoint => {
return browserify(srcJs + '/' + startpoint, { debug: true })
.transform(
babel.configure({
presets: ['@babel/preset-env'].map(require.resolve)
})
)
.bundle()
.on('error', err => {
console.error(err);
this.emit('end');
})
.pipe(source(startpoint))
.pipe(buffer())
.pipe(gulpif(production, replace('[routeWorkerFileName]', 'routeWorker.min.js')))
.pipe(gulpif(!production, replace('[routeWorkerFileName]', 'routeWorker.js')))
.pipe(gulpif(!production, sourcemaps.init({ loadMaps: true })))
.pipe(gulpif(!production, sourcemaps.write('./')))
.pipe(gulpif(production, rename({ suffix: '.min' })))
.pipe(gulpif(production, uglify()))
.pipe(gulp.dest(outJs));
});
// create a merged stream
return eventStream.merge.apply(null, tasks).on('end', done);
});
gulp.task('scripts', gulp.series('scripts:lint', 'scripts:build'));
gulp.task('lib-lity-js', () => {
const changed = modules.get('changed');
const outLoc = outJs + '/lib';
const src = production ? 'lity.min.js' : 'lity.js';
return gulp
.src(`./node_modules/lity/dist/${src}`)
.pipe(changed(outLoc))
.pipe(gulp.dest(outLoc));
});
gulp.task('lib-lity-css', () => {
const changed = modules.get('changed');
const outLoc = outCss + '/lib';
const src = production ? 'lity.min.css' : 'lity.css';
return gulp
.src(`./node_modules/lity/dist/${src}`)
.pipe(changed(outLoc))
.pipe(gulp.dest(outLoc));
});
gulp.task('lib-lity', gulp.parallel('lib-lity-js', 'lib-lity-css'));
gulp.task('lib-jquery', () => {
const changed = modules.get('changed');
const outLoc = outJs + '/lib';
const src = production ? 'jquery.min.js' : 'jquery.js';
return gulp
.src(`./node_modules/jquery/dist/${src}`)
.pipe(changed(outLoc))
.pipe(gulp.dest(outLoc));
});
// copies all libraries
gulp.task('libraries', gulp.parallel('lib-lity', 'lib-jquery'));
// copies the font directory
gulp.task('fonts', () => {
return gulp.src(srcFonts + '/**/*').pipe(gulp.dest(outFonts));
});
// generates the html file
gulp.task('html', () => {
const file = modules.get('file'),
insert = modules.get('insert'),
gulpif = modules.get('gulpif'),
template = modules.get('template'),
minifier = modules.get('minifier');
const p = production ? '.min' : '';
return file('index.html', '', { src: true })
.pipe(insert.append('<!-- build:title -->'))
.pipe(insert.append(`${config.title} (v${packageData.version})`))
.pipe(insert.append('<!-- /build:title -->'))
.pipe(insert.append('<!-- build:styles -->'))
.pipe(insert.append(`<link href="css/lib/lity${p}.css" rel="stylesheet">`))
.pipe(insert.append(`<link href="css/style${p}.css" rel="stylesheet">`))
.pipe(insert.append('<!-- /build:styles -->'))
.pipe(insert.append('<!-- build:scripts -->'))
.pipe(insert.append(`<script src="js/lib/jquery${p}.js"></script>`))
.pipe(insert.append(`<script src="js/lib/lity${p}.js"></script>`))
.pipe(insert.append(`<script src="js/main${p}.js"></script>`))
.pipe(insert.append('<!-- /build:scripts -->'))
.pipe(template(srcHtml + '/index.html'))
.pipe(gulpif(production, minifier({
minify: true,
minifyHTML: {
collapseWhitespace: true,
conservativeCollapse: true,
}
})))
.pipe(gulp.dest(out));
});
// copies images
gulp.task('images', () => {
const changed = modules.get('changed'),
imagemin = modules.get('imagemin');
return gulp
.src(srcImg + '/**/*.svg')
.pipe(changed(outImg))
.pipe(
imagemin([
imagemin.svgo({
plugins: [{ removeViewBox: true }, { cleanupIDs: false }]
})
])
)
.pipe(gulp.dest(outImg));
});
// removes the deploy directory
gulp.task('clean', () => {
const del = modules.get('del');
return del(out);
});
// compile the html pages for docs from the md files
gulp.task('help', () => {
const markdown = modules.get('markdown'),
rename = modules.get('rename'),
replace = modules.get('replace'),
insert = modules.get('insert'),
template = modules.get('template'),
gulpif = modules.get('gulpif'),
minifier = modules.get('minifier');
let styleSheet = 'docs.css';
if (production) {
styleSheet = 'docs.min.css';
}
const snippets = {
styleSheet: `<link href="../css/${styleSheet}" rel="stylesheet">`
};
return (
gulp
.src(srcDocs + '/*.md')
.pipe(markdown())
.pipe(
rename(path => {
path.extname = '.html';
})
)
.pipe(replace('.md', '.html'))
// wrap the generated html between <!-- build:md --> tags to mark it for the template plugin
.pipe(insert.prepend('<!-- build:md -->'))
.pipe(
insert.append(
`<p>For technical documentation please visit <a href="./${outJsDocRelative}/index.html" target="_blank">the docs</a>.</p>`
)
)
.pipe(insert.append('<!-- /build:md -->'))
// add color examples after the colors described in the markdown
.pipe(
replace(/<!-- color (.*) -->/g, match => {
const colorName = match.replace(/<!-- color | -->/g, '');
return `<i class="color ${colorName}"></i>`;
})
)
// add links to the stylesheets
.pipe(insert.append('<!-- build:styleSheet -->'))
.pipe(insert.append(snippets.styleSheet))
.pipe(insert.append('<!-- /build:styleSheet -->'))
// put the generated file into a predefined template
.pipe(template(srcHtml + '/help.html'))
.pipe(gulpif(production, minifier({
minify: true,
minifyHTML: {
collapseWhitespace: true,
conservativeCollapse: true,
}
})))
.pipe(gulp.dest(outDocs))
);
});
gulp.task('jsdoc:generate', done => {
const jsdoc = modules.get('jsdoc');
const customCss = production ? 'jsdoc.min.css' : 'jsdoc.css';
const jsdocConfig = {
opts: {
destination: outJsDoc,
encoding: 'utf8',
private: true,
recurse: true,
template: 'node_modules/tui-jsdoc-template'
},
templates: {
name: 'Hradla',
footerText: `${config.title} (v${packageData.version})`,
logo: {
url: '../../img/svg/gate/xor.svg',
width: '40px',
height: '20px'
// link: "../../"
},
css: [`../../css/${customCss}`]
},
plugins: ['plugins/markdown']
};
return gulp
.src(['README.md', './' + srcJs + '/**/*.js'], { read: false })
.pipe(jsdoc(jsdocConfig, done));
});
gulp.task('jsdoc:clean', () => {
const del = modules.get('del');
return del(outJsDoc);
});
gulp.task('jsdoc', gulp.series('jsdoc:clean', 'jsdoc:generate'));
gulp.task('docs', gulp.parallel('help', 'jsdoc'));
// create network library
// Copy all files to outJson but get their file names and 'name' fields
// and save them to the 'networks' array. Then serialize this field and save is as JSON
gulp.task('library', () => {
const jsoneditor = modules.get('jsoneditor'),
file = modules.get('file'),
insert = modules.get('insert'),
tap = modules.get('tap');
let currentFileName;
let networks = [];
return gulp
.src(srcLibrary + '/*.json')
.pipe(
tap(function(file, t) {
// get the file name
currentFileName = file.path
.split('/')
.pop()
.replace('.json', '');
})
)
.pipe(
jsoneditor(json => {
// add info about this network to the networks array
networks.push({
name: json.name, // name of the network parsed from the network JSON file
file: currentFileName, // file name acquired using tap
hasNetwork: json.boxes !== undefined, // true if the network has a gate layout defined
hasTable: json.blackbox !== undefined // true if the network has a truth table defined
});
return json; // pass the network JSON through without changes
})
)
.pipe(gulp.dest(outLibrary)) // save the networks in the output directory without changes
.on('end', () => {
// create the network list file from the networks array and save it
return file('networkList.json', '', { src: true })
.pipe(insert.append(JSON.stringify({ networks })))
.pipe(gulp.dest(outLibrary));
});
});
///// create archives
// create a zip archive
gulp.task('zip', () => {
const changed = modules.get('changed'),
zip = modules.get('zip');
return gulp
.src(out + '/**/*')
.pipe(changed(packaged))
.pipe(zip('hradla-' + packageData.version + '.zip'))
.pipe(gulp.dest(packaged));
});
// create a tarball
gulp.task('tarball', () => {
const changed = modules.get('changed'),
tar = modules.get('tar'),
gzip = modules.get('gzip');
return gulp
.src(out + '/**/*')
.pipe(changed(packaged))
.pipe(tar('hradla-' + packageData.version + '.tar'))
.pipe(gzip())
.pipe(gulp.dest(packaged));
});
gulp.task('gh-pages-copy', () => {
return gulp.src(out + '/**/*').pipe(gulp.dest(ghPages));
});
// create the zip archive and the tarball
gulp.task('package', gulp.parallel('zip', 'tarball'));
///// main scripts
// build the whole project
gulp.task(
'build-all',
gulp.series(
'clean',
gulp.parallel(
'scripts',
'styles',
'library',
'libraries',
'html',
'images',
'fonts',
'docs'
)
)
);
gulp.task('build-prod', gulp.series('production', 'build-all', 'package'));
gulp.task('gh-pages', gulp.series('production', 'build-all', 'gh-pages-copy'));
gulp.task('build-dev', gulp.series('build-all'));
gulp.task('default', gulp.series('build-prod'));
///// watches
gulp.task('watch-scripts', () => {
const watch = modules.get('watch');
return watch(srcJs + '/**', gulp.series('scripts'));
});
gulp.task('watch-styles', () => {
const watch = modules.get('watch');
return watch(srcCss + '/**', gulp.series('styles'));
});
gulp.task('watch', gulp.parallel('watch-scripts', 'watch-styles'));