forked from abernier/mosaico-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
646 lines (577 loc) · 16.3 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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
'use strict'
const gulp = require('gulp')
const path = require('path')
const $ = require('gulp-load-plugins')()
const browserSync = require('browser-sync').create()
const { reload } = browserSync
const lazypipe = require('lazypipe')
const del = require('del')
const merge = require('merge-stream')
const args = require('yargs').argv
const _ = require('lodash')
const { cyan, magenta } = require('chalk')
const Vinyl = require(`vinyl`)
const isDev = args.dev === true
const isBuild = !isDev
const env = isDev ? 'development' : 'build'
const buildDir = 'dist'
function onError(err) {
if (err.annotated) {
console.log(err.annotated)
} else if (err.message) {
console.log(err.message)
} else {
console.log(err)
}
return this.emit('end')
}
console.log('environment is', magenta(env))
function bump() {
return gulp
.src(`package.json`)
.pipe(
$.bump({
version: args.pkg,
})
)
.pipe(gulp.dest('./'))
}
bump.description = `Bump versions on package.json. Used only in release script`
const unDevName = lazypipe().pipe(
$.rename,
filePath => (filePath.basename = filePath.basename.replace('-dev', ''))
)
////////
// CSS
////////
const autoprefixer = require('autoprefixer')
const csswring = require('csswring')
const cssProd = lazypipe()
.pipe(unDevName)
.pipe($.purgeSourcemaps)
.pipe(
$.postcss,
[csswring({ removeAllComments: true })]
)
function cleanCss() {
return del([buildDir + '/*.css', buildDir + '/*.css.map'])
}
function cssEditor() {
return gulp
.src('src/css/custom-editor.less')
.pipe($.plumber(onError))
.pipe($.sourcemaps.init())
.pipe($.less())
.pipe($.postcss([autoprefixer({ browsers: ['ie 10', 'last 2 versions'] })]))
.pipe($.sourcemaps.write())
.pipe($.rename('editor-dev.css'))
.pipe(gulp.dest(buildDir))
.pipe(reload({ stream: true }))
.pipe($.if(isBuild, cssProd()))
.pipe($.if(isBuild, gulp.dest(buildDir)))
}
cssEditor.description = `build CSS for mosaico editor`
exports[`css:mosaico`] = cssEditor
function cssApp() {
return gulp
.src('src/css-backend/index.styl')
.pipe($.plumber(onError))
.pipe($.sourcemaps.init())
.pipe(
$.stylus({
'include css': true,
})
)
.pipe($.postcss([autoprefixer({ browsers: ['ie 10', 'last 2 versions'] })]))
.pipe(
$.replace(
'rgb(255,152,0)',
'var(--color-primary, var(--default-color-primary))'
)
)
.pipe(
$.replace(
'rgb(68,138,255)',
'var(--color-accent, var(--default-color-accent))'
)
)
.pipe($.sourcemaps.write())
.pipe($.rename('app-dev.css'))
.pipe(gulp.dest(buildDir))
.pipe(reload({ stream: true }))
.pipe($.if(isBuild, cssProd()))
.pipe($.if(isBuild, gulp.dest(buildDir)))
}
cssApp.description = `build CSS for the backend`
exports[`css:backend`] = cssApp
const css = gulp.series(cleanCss, gulp.parallel(cssEditor, cssApp))
css.description = `Build CSS for the mosaico editor and the app`
////////
// JS
////////
//----- LIBRARIES
function cleanLib() {
return del(buildDir, '/**/*.js')
}
function mosaicoLib() {
return gulp
.src([
`node_modules/jquery/dist/jquery.js`,
// NOTE: use minimized version because non-min uses console to write migration warnings
`node_modules/jquery-migrate/dist/jquery-migrate.min.js`,
`node_modules/knockout/build/output/knockout-latest.js`,
`node_modules/jquery-ui-package/jquery-ui.js`,
`node_modules/jquery-ui-touch-punch/jquery.ui.touch-punch.js`,
`node_modules/default-passive-events/dist/index.js`,
// NOTE: include these 2 BEFORE the fileupload libs
// using npm5 we can get sub-dependencies from nested paths, but npm3 does flatten them, so let's depend on them explicitly.
// 'node_modules/blueimp-file-upload/node_modules/blueimp-canvas-to-blob/js/canvas-to-blob.js',
// 'node_modules/blueimp-file-upload/node_modules/blueimp-load-image/js/load-image.all.min.js',
`node_modules/blueimp-canvas-to-blob/js/canvas-to-blob.js`,
`node_modules/blueimp-load-image/js/load-image.all.min.js`,
// 'node_modules/blueimp-file-upload/js/jquery.iframe-transport.js',
`node_modules/blueimp-file-upload/js/jquery.fileupload.js`,
`node_modules/blueimp-file-upload/js/jquery.fileupload-process.js`,
`node_modules/blueimp-file-upload/js/jquery.fileupload-image.js`,
`node_modules/blueimp-file-upload/js/jquery.fileupload-validate.js`,
`node_modules/knockout-jqueryui/dist/knockout-jqueryui.js`,
`node_modules/tinymce/tinymce.js`,
])
.pipe($.concat('lib-editor-dev.js'))
.pipe(gulp.dest(buildDir + '/lib'))
.pipe($.if(isBuild, unDevName()))
.pipe($.if(isBuild, $.uglify()))
.pipe(gulp.dest(buildDir + '/lib'))
}
mosaicoLib.description = `copy all related tinymce files to the right place`
exports[`js:mosaico-lib`] = mosaicoLib
function copyTinymceFiles() {
return gulp
.src(
[
'node_modules/tinymce/themes/modern/theme.js',
'node_modules/tinymce/themes/modern/theme.min.js',
'node_modules/tinymce/plugins/paste/plugin.js',
'node_modules/tinymce/plugins/paste/plugin.min.js',
'node_modules/tinymce/plugins/link/plugin.js',
'node_modules/tinymce/plugins/link/plugin.min.js',
'node_modules/tinymce/plugins/hr/plugin.js',
'node_modules/tinymce/plugins/hr/plugin.min.js',
'node_modules/tinymce/plugins/lists/plugin.js',
'node_modules/tinymce/plugins/lists/plugin.min.js',
'node_modules/tinymce/plugins/textcolor/plugin.js',
'node_modules/tinymce/plugins/textcolor/plugin.min.js',
'node_modules/tinymce/plugins/colorpicker/plugin.js',
'node_modules/tinymce/plugins/colorpicker/plugin.min.js',
'node_modules/tinymce/plugins/code/plugin.js',
'node_modules/tinymce/plugins/code/plugin.min.js',
],
{ base: 'node_modules/tinymce' }
)
.pipe(gulp.dest(buildDir + '/lib'))
}
copyTinymceFiles.description = `copy all related tinymce files to the right place`
exports[`js:tinymce`] = copyTinymceFiles
// Bundling mosaico libs is just a concat…
const editorLib = gulp.series(
cleanLib,
gulp.parallel(mosaicoLib, copyTinymceFiles)
)
editorLib.description = `build JS for the mosaico editor and the app`
//----- MOSAICO APPLICATION
const browserify = require('browserify')
const source = require('vinyl-source-stream')
const vinylBuffer = require('vinyl-buffer')
const aliasify = require('aliasify')
const shim = require('browserify-shim')
const babelify = require('babelify')
const envify = require('envify/custom')
const watchify = require('watchify')
function mosaicoEditor(debug = false) {
return browserify({
cache: {},
packageCache: {},
debug: debug,
entries: [`./src/js/app.js`, `./build/templates.js`],
standalone: `Mosaico`,
})
.transform(aliasify)
.transform(shim, { global: true })
.transform(
// babelify only apply to our own custum additions
babelify.configure({
presets: [`env`],
only: /custom-/,
})
)
.transform(
envify({
_: 'purge',
NODE_ENV: debug,
CUSTOM: true,
MOSAICO: false,
})
)
}
function jsMosaicoDev() {
let b = mosaicoEditor(true)
if (isDev) {
b = watchify(b)
b.on('update', function() {
console.log(`bundle ${magenta('editor')} app`)
bundleShareDev(b)
})
}
return bundleShareDev(b)
}
function bundleShareDev(b) {
return b
.bundle()
.on('error', onError)
.pipe(source('editor-dev.js'))
.pipe(vinylBuffer())
.pipe(gulp.dest(buildDir))
}
function jsMosaicoProd() {
return mosaicoEditor()
.bundle()
.on('error', onError)
.pipe(source('editor.js'))
.pipe(vinylBuffer())
.pipe($.stripDebug())
.pipe($.uglify())
.pipe(gulp.dest(buildDir))
}
const jsEditor = gulp.series(
templates,
isBuild ? gulp.parallel(jsMosaicoDev, jsMosaicoProd) : jsMosaicoDev
)
jsEditor.description = `Bundle mosaico app, without libraries`
exports[`js:mosaico-editor`] = jsEditor
exports[`js:mosaico`] = gulp.parallel(editorLib, jsEditor)
//----- MOSAICO'S KNOCKOUT TEMPLATES: see -> combineKOTemplates.js
const through = require('through2')
const StringDecoder = require('string_decoder').StringDecoder
const decoder = new StringDecoder('utf8')
function templates() {
const templates = []
function passThrough(file, encoding, cb) {
var name = path.basename(file.path)
var name = /^([^\.]*)/.exec(name)[1]
var content = decoder.write(file.contents)
content = content.replace(/"/g, '\\x22')
content = content.replace(/(\r\n|\n|\r)/gm, '')
content = ` templateSystem.addTemplate("${name}", "${content}");`
// content = " templateSystem.addTemplate(\"" + name + "\", \"" + content + "\");";
templates.push(content)
return cb(null)
}
function flush(cb) {
var result =
"var templateSystem = require('../src/js/bindings/choose-template.js');\n"
result =
result +
"document.addEventListener('DOMContentLoaded', function(event) {\n"
result = result + templates.join('\n') + '\n'
result = result + '});\n'
this.push(
new Vinyl({
cwd: './',
base: './',
path: 'templates.js',
contents: new Buffer(result),
})
)
return cb()
}
return (
gulp
.src([
'src/tmpl/*.html',
// replace some original templates by custom ones
'src/tmpl-custom/*.html',
'!src/tmpl/gallery-images.tmpl.html',
])
.pipe(through.obj(passThrough, flush))
// templates has to be build on “build” folder
// they will be require by editor app application
.pipe(gulp.dest('build'))
)
}
//----- HOME JS (rename for now)
const pugify = require('pugify')
function jsUser(debug = false) {
return browserify({
cache: {},
packageCache: {},
debug: debug,
entries: ['./src/js-user-backend/index.js'],
})
.transform(babelify, {
presets: ['env'],
})
.transform(
pugify.pug({
pretty: debug,
compileDebug: debug,
})
)
.transform(
envify({
_: 'purge',
NODE_ENV: debug ? 'development' : 'production',
LOG: debug,
})
)
}
function jsUserDev() {
let b = jsUser(true)
if (isDev) {
b = watchify(b)
b.on('update', function() {
console.log(`bundle ${magenta('user')} app`)
bundleUserDev(b)
})
}
return bundleUserDev(b)
}
function bundleUserDev(b) {
return b
.bundle()
.on('error', onError)
.pipe(source('user-dev.js'))
.pipe(vinylBuffer())
.pipe(gulp.dest(buildDir))
}
function jsUserProd() {
return jsUser()
.bundle()
.on('error', onError)
.pipe(source('user.js'))
.pipe(vinylBuffer())
.pipe($.uglify())
.pipe(gulp.dest(buildDir))
}
gulp.task('js:user', isBuild ? gulp.parallel(jsUserDev, jsUserProd) : jsUserDev)
//----- ADMIN JS
function jsAdmin(debug = false) {
return browserify({
cache: {},
packageCache: {},
debug: debug,
entries: ['./src/js-admin-backend/index.js'],
})
.transform(babelify, {
presets: ['env'],
})
.transform(
envify({
_: 'purge',
NODE_ENV: debug ? 'development' : 'production',
LOG: debug,
})
)
}
function jsAdminDev() {
let b = jsAdmin(true)
if (isDev) {
b = watchify(b)
b.on('update', function() {
console.log(`bundle ${magenta('admin')} app`)
bundleAdminDev(b)
})
}
return bundleAdminDev(b)
}
function bundleAdminDev(b) {
return b
.bundle()
.on('error', onError)
.pipe(source('admin-dev.js'))
.pipe(vinylBuffer())
.pipe(gulp.dest(buildDir))
}
function jsAdminProd() {
return jsAdmin()
.bundle()
.on('error', onError)
.pipe(source('admin.js'))
.pipe(vinylBuffer())
.pipe($.uglify())
.pipe(gulp.dest(buildDir))
}
gulp.task(
'js:admin',
isBuild ? gulp.parallel(jsAdminDev, jsAdminProd) : jsAdminDev
)
const js = gulp.parallel(mosaicoLib, jsEditor, 'js:user', 'js:admin')
js.description = `build js for mosaico app and the for the rests of the application`
////////
// ASSETS
////////
//----- FONTS
function cleanFonts() {
return del('res/fa/fonts')
}
function fonts() {
return gulp
.src('node_modules/@fortawesome/fontawesome-free/webfonts/*')
.pipe(gulp.dest('res/fa/fonts'))
}
const assets = gulp.series(cleanFonts, fonts)
assets.description = `Copy font-awesome in the right place`
//----- MAINTENANCE
const maintenanceFolder = 'server/views/maintenance-pages'
const cleanMaintenance = cb => del([`${maintenanceFolder}/*.html`], cb)
function maintenance() {
return gulp
.src([`${maintenanceFolder}/*.pug`, `!${maintenanceFolder}/_*.pug`])
.pipe($.pug())
.pipe(gulp.dest(maintenanceFolder))
}
//----- REVS
const crypto = require('crypto')
function rev() {
let revs = []
function sortByName(a, b) {
const nameA = a.name.toUpperCase()
const nameB = b.name.toUpperCase()
if (nameA < nameB) return -1
if (nameA > nameB) return 1
return 0
}
function passThrough(file, enc, callback) {
const key = path.relative(file.base, file.path)
const md5 = crypto.createHash('md5')
if (!file.contents) return callback(null)
const hash = md5.update(file.contents.toString()).digest('hex')
revs.push({ name: '/' + key, hash })
callback(null)
}
function flush(cb) {
const md5Object = {}
// keep the json in alphabetical order
revs.sort(sortByName).forEach(r => {
md5Object[r.name] = r.hash
})
let file = new Vinyl({
path: 'md5public.json',
contents: new Buffer(JSON.stringify(md5Object, null, ' ')),
})
this.push(file)
cb()
}
return gulp
.src([
'dist/**/*.*',
'!dist/**/*-dev.*',
'res/**/*.*',
'!res/lang/*.*',
'node_modules/material-design-lite/*.js',
'node_modules/material-design-icons-iconfont/dist/**/*.*',
'!node_modules/material-design-icons-iconfont/dist/**/*.scss',
])
.pipe(through.obj(passThrough, flush))
.pipe(gulp.dest('server'))
}
////////
// DEV
////////
const cleanTmp = cb => del(['tmp/upload_*'], cb)
function toc() {
return gulp
.src('./README.md')
.pipe(
$.doctoc({
mode: 'github.com',
})
)
.pipe(gulp.dest('./'))
}
toc.description = `Regenerate TOC for BACKEND.md`
const cleanAll = cb => del([buildDir, 'build'], cb)
const build = gulp.series(
cleanAll,
gulp.parallel(editorLib, js, css, assets),
rev
)
build.description = `rebuild all assets`
const nodemonOptions = {
script: 'server/workers.js',
ext: 'js json',
watch: [
'server/**/*.js',
// '!server/locales/*.js',
'.backendrc',
'index.js',
'res/lang/*.js',
'shared/*.js',
],
}
let init = true
function nodemon(cb) {
return $.nodemon(
_.merge(
{
env: {
NODE_ENV: 'development',
VIPS_WARNING: false,
// 'DEBUG': 'nightmare*',
},
// nodeArgs: ['--inspect'],
},
nodemonOptions
)
).on('start', () => {
// https://gist.github.com/sogko/b53d33d4f3b40d3b4b2e#comment-1457582
if (init) {
init = false
cb()
}
})
}
function launchBrowserSync(cb) {
browserSync.init(
{
proxy: 'http://localhost:3000',
open: false,
port: 7000,
ghostMode: false,
},
cb
)
}
const bsAndWatch = gulp.series(launchBrowserSync, watchFiles)
let initProd = true
function nodemonProd(cb) {
return $.nodemon(
_.merge({ env: { NODE_ENV: 'production' } }, nodemonOptions)
).on('start', () => {
if (initProd) {
initProd = false
cb()
}
})
}
function watchFiles(cb) {
gulp.watch(['server/views/*.jade', 'dist/*.js']).on('change', reload)
gulp.watch('src/css/**/*.less', cssEditor)
gulp.watch('src/css-backend/**/*.styl', cssApp)
gulp.watch(['src/tmpl/*.html', 'src/tmpl-custom/*.html'], templates)
cb()
}
gulp.task('css', css)
gulp.task('css:editor', cssEditor)
gulp.task('css:app', cssApp)
gulp.task('js', js)
gulp.task('js:editor', jsEditor)
gulp.task('js:editor-libs', mosaicoLib)
gulp.task('assets', assets)
gulp.task('rev', rev)
gulp.task('templates', templates)
gulp.task('build', build)
gulp.task('maintenance', gulp.series(cleanMaintenance, maintenance))
gulp.task('dev', gulp.series(build, nodemon, bsAndWatch))
gulp.task('prod', gulp.parallel(js, nodemonProd), watchFiles)
gulp.task('bump', bump)
gulp.task('toc', toc)