-
Notifications
You must be signed in to change notification settings - Fork 7
/
gulpfile.js
74 lines (60 loc) · 1.93 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
'use strict'
const babelify = require('babelify')
const browserify = require('browserify')
const del = require('del')
const gulp = require('gulp')
const merge2 = require('merge2')
const uglify = require('gulp-uglify')
const path = require('path')
const source = require('vinyl-source-stream')
const buffer = require('vinyl-buffer')
const fs = require('fs')
function bundle (indexFile, dir, deps, useUglify, cb) {
let chain = browserify(indexFile, { debug: true })
.transform(babelify)
.bundle()
.pipe(source('bundle.js'))
.pipe(buffer())
if (useUglify) {
chain = chain.pipe(uglify())
}
chain = chain.pipe(gulp.dest(dir))
let stream = merge2(chain)
for (let key in deps) {
stream.add(
gulp.src(key)
.pipe(gulp.dest(path.join(dir, deps[key])))
)
}
stream.on('queueDrain', cb)
}
function createTasks (name, uglify = true) {
gulp.task(`clean-${name}`, (done) => {
return del(`./build/${name}`, { force: true })
})
gulp.task(`build-${name}`, (cb) => {
bundle('./src/shared/index.js', `./build/${name}`, {
[`./src/${name}/manifest.json`]: '',
'./src/shared/images/*': 'images',
'./src/shared/popup/*': 'popup'
}, uglify, cb)
})
gulp.task(`manifest-${name}`, (done) => {
let manifestFile = `./build/${name}/manifest.json`
let manifest = require(manifestFile)
let sources = require('./src/shared/sources.json')
let permissions = manifest['permissions']
for (let x in sources['sources']) {
let urls = sources['sources'][x]['urls']
for (let y in urls) {
permissions.push(urls[y])
}
}
let jsonOutput = JSON.stringify(manifest, null, 2)
fs.writeFileSync(manifestFile, jsonOutput, 'utf8')
done()
})
gulp.task(name, gulp.series(`clean-${name}`, `build-${name}`, `manifest-${name}`))
}
createTasks('firefox')
createTasks('chrome', false)