This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
104 lines (91 loc) · 2.49 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
const gulp = require('gulp');
const {merge} = require('event-stream');
const map = require('map-stream');
const $ = require('gulp-load-plugins')();
const version = require('./package.json').version;
const replaceExt = require('replace-ext');
gulp.task('clean', () => {
return pipe(
'./tmp',
$.clean()
);
});
gulp.task('style', () => {
return pipe(
'./src/styles/mdisviewer.less',
$.plumber(),
$.less({relativeUrls: true}),
$.autoprefixer({cascade: true}),
'./tmp'
);
});
gulp.task('template', () => {
const LOTS_OF_SPACES = new Array(500).join(' ');
return pipe(
'./src/template.html',
$.replace('__SPACES__', LOTS_OF_SPACES),
html2js('const TEMPLATE = \'$$\''),
'./tmp'
);
});
gulp.task('js', gulp.series('template', () => {
const src = [
'./tmp/template.js',
'./src/constants.js',
'./src/adapters/adapter.js',
'./src/adapters/gistmd.js',
'./src/view.index.js',
'./src/view.options.js',
'./src/util.async.js',
'./src/util.storage.js'
].concat(['./src/config/overrides.js'])
.concat('./src/mdisviewer.js');
return pipe(
src,
$.concat('mdisviewer.js'),
'./tmp'
);
}));
gulp.task('devdist', gulp.series('js', (cb) => {
merge(
pipe('./icons/**/*', './tmp/app/icons'),
pipe(['./libs/**/*', './tmp/mdisviewer.*'], './tmp/app/'),
pipe('./src/config/background.js', './tmp/app/'),
pipe('./src/config/manifest.json', $.replace('$VERSION', version), './tmp/app/')
);
cb(); // Need this callback after Gulp4 because `return merge()` doesn't returns stream
}));
gulp.task('build', gulp.series(
'clean',
'style',
'template',
'js',
'devdist'
));
gulp.task('default', gulp.series('build', () => {
gulp.watch(['./libs/**/*', './src/**/*'], gulp.task('default'));
}));
// Helpers
function pipe(src, ...transforms) {
return transforms.reduce((stream, transform) => {
const isDest = typeof transform === 'string';
return stream.pipe(isDest ? gulp.dest(transform) : transform);
}, gulp.src(src));
}
function html2js(template) {
return map(escape);
function escape(file, cb) {
const path = replaceExt(file.path, '.js');
const content = file.contents.toString();
/* eslint-disable quotes */
const escaped = content
.replace(/\\/g, "\\\\")
.replace(/'/g, "\\'")
.replace(/\r?\n/g, "\\n' +\n '");
/* eslint-enable */
const body = template.replace('$$', escaped);
file.path = path;
file.contents = Buffer.from(body);
cb(null, file);
}
}