-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
86 lines (78 loc) · 2.04 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
const babelify = require('babelify')
const browserify = require('browserify')
const change = require('gulp-change')
const gulp = require('gulp')
const babel = require('gulp-babel')
const source = require('vinyl-source-stream')
exports.default = gulp.series(
compile,
bundle,
fixAsync
)
function compile() {
return gulp
.src('./src/**/*.js')
.pipe(change((content) => {
let result = ''
let withinComment = false
for (let i = 0; i < content.length; i++) {
let char = content.charAt(i)
if (withinComment) {
result += char
if ((char === '*') && (content.charAt(i + 1) === '/')) {
i++
withinComment = false
result += '/'
}
continue
}
switch (char) {
case '/':
if (content.charAt(i + 1) === '*') {
withinComment = true
}
break
case 'a':
if (['sync ', 'wait '].includes(content.substring(i + 1, i + 6))) {
result += `/*a${content.substring(i + 1, i + 5)}*/`
i += 4
continue
}
break
}
result += char
}
return result
}))
.pipe(babel())
.pipe(gulp.dest('./tmp'))
}
function bundle(done) {
const FILES = [
{ in: 'tmp/background/bootstrap.js', out: 'background.js' },
{ in: 'tmp/options/bootstrap.js', out: 'options.js' }
]
let subTasks = []
for (let file of FILES) {
subTasks.push(() =>
browserify({
entries: file.in,
transform: [babelify]
})
.bundle()
.pipe(source(file.out))
.pipe(gulp.dest('./dist'))
)
}
gulp.series(
gulp.parallel(...subTasks),
(done2) => { done(); done2() }
)()
}
function fixAsync() {
return gulp.src('./dist/*.js')
.pipe(change((content) => {
return content.replace(/\/\*a(sync|wait)\*\//g, 'a$1 ')
}))
.pipe(gulp.dest('./dist'))
}