forked from WebMemex/webmemex-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
113 lines (99 loc) · 3.14 KB
/
gulpfile.babel.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
import gulp from 'gulp'
import uglify from 'gulp-uglify'
import identity from 'gulp-identity'
import source from 'vinyl-source-stream'
import buffer from 'vinyl-buffer'
import browserify from 'browserify'
import watchify from 'watchify'
import babelify from 'babelify'
import envify from 'loose-envify/custom'
import uglifyify from 'uglifyify'
import path from 'path'
import cssModulesify from 'css-modulesify'
import cssnext from 'postcss-cssnext'
const staticFiles = {
'node_modules/webextension-polyfill/dist/browser-polyfill.js': 'extension/lib',
}
const sourceFiles = [
{
entries: ['./src/background.js'],
output: 'background.js',
destination: './extension',
},
{
entries: ['./src/content_script.js'],
output: 'content_script.js',
destination: './extension',
},
{
entries: ['./src/overview/main.jsx'],
output: 'overview.js',
destination: './extension/overview',
cssOutput: 'style.css',
},
{
entries: ['./src/options/main.jsx'],
output: 'options.js',
destination: './extension/options',
cssOutput: 'style.css',
},
]
const browserifySettings = {
debug: true,
extensions: ['.jsx', '.css'],
paths: ['.'],
}
function createBundle({entries, output, destination, cssOutput},
{watch = false, production = false}) {
let b = watch
? watchify(browserify({...watchify.args, ...browserifySettings, entries}))
.on('update', bundle)
: browserify({...browserifySettings, entries})
b.transform(babelify)
b.transform(envify({
NODE_ENV: production ? 'production' : 'development',
}), {global: true})
if (cssOutput) {
b.plugin(cssModulesify, {
global: true,
output: path.join(destination, cssOutput),
postcssBefore: [
cssnext,
],
})
}
if (production) {
b.transform(uglifyify, {global: true})
}
function bundle() {
let startTime = Date.now()
b.bundle()
.on('error', error => console.error(error.message))
.pipe(source(output))
.pipe(buffer())
.pipe(production ? uglify({output: {ascii_only: true}}) : identity())
.pipe(gulp.dest(destination))
.on('end', () => {
let time = (Date.now() - startTime) / 1000
console.log(`Bundled ${output} in ${time}s.`)
})
}
bundle()
}
gulp.task('copyStaticFiles', () => {
for (let filename in staticFiles) {
console.log(`Copying '${filename}' to '${staticFiles[filename]}'..`)
gulp.src(filename)
.pipe(gulp.dest(staticFiles[filename]))
}
})
gulp.task('build-prod', ['copyStaticFiles'], () => {
sourceFiles.forEach(bundle => createBundle(bundle, {watch: false, production: true}))
})
gulp.task('build', ['copyStaticFiles'], () => {
sourceFiles.forEach(bundle => createBundle(bundle, {watch: false}))
})
gulp.task('watch', ['copyStaticFiles'], () => {
sourceFiles.forEach(bundle => createBundle(bundle, {watch: true}))
})
gulp.task('default', ['watch'])