-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
206 lines (180 loc) · 4.95 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
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
'use strict';
import del from 'del'
import fs from 'fs'
import gulp from 'gulp'
import through from 'through2'
import replaceStream from 'replacestream'
import rename from 'gulp-rename'
import { rollup } from 'rollup'
import _ from 'lodash'
import { create } from 'browser-sync'
import runSequence from 'run-sequence'
import * as workboxBuild from 'workbox-build'
import { version } from './package.json'
import { generate } from './rollup.config.raw'
import { template } from './src/lib/doT'
const state = {
version,
passLenRange: new Array(123).fill(0).map((v, k) => k + 6),
passLen: 16,
iteration: 100,
salt: '9rjixtK35p091K2glFZWDgueRFqmSNfX'
}
const browserSync = create()
function dotPlugin(model) {
return through.obj(function (file, encoding, cb) {
if (file.isNull()) {
// nothing to do
return cb(null, file)
}
if (file.isStream()) {
this.emit('error', new Error('Streams not supported!'))
} else if (file.isBuffer()) {
const content = template(file.contents.toString(encoding))(model)
// console.log(content)
file.contents = new Buffer(content)
cb(null, file)
}
})
}
function replaceScript() {
return through.obj(function (file, encoding, cb) {
if (file.isNull()) {
// nothing to do
return cb(null, file)
}
if (file.isStream()) {
this.emit('error', new Error('Streams not supported!'))
cb(null, file)
} else if (file.isBuffer()) {
const template$ = fs.createReadStream('./src/mobile/mobile.html')
.pipe(replaceStream('$_SCRIPT_$', file.contents.toString(encoding)))
file.contents = template$
cb(null, file)
}
})
}
gulp.task('build-install-tpl', function () {
return gulp.src('./src/install/install.html')
.pipe(dotPlugin(state))
.pipe(rename({
basename: 'index',
extname: '.html'
}))
.pipe(gulp.dest('./dist/'))
})
gulp.task('build-install-js', function () {
const cloneConfig = _.merge({}, generate(), {
input: './src/install/install.js'
})
return rollup(cloneConfig).then(function (bundle) {
return bundle.write({
format: 'iife',
file: 'dist/install.js'
})
})
})
gulp.task('install', [], function (callback) {
runSequence('build-install-tpl', 'build-install-js', callback)
})
gulp.task('mobile', function () {
const cloneConfig = _.merge({}, generate(), {
input: './src/mobile/mobile.js'
})
return rollup(cloneConfig).then(function (bundle) {
return bundle.write({
format: 'iife',
file: 'dist/mobile.js'
})
})
})
gulp.task('mobile:pwa:index', function () {
return gulp.src('./src/pwa/index.html')
.pipe(rename({
basename: 'pwa'
}))
.pipe(gulp.dest('./dist'))
})
gulp.task('mobile:pwa:deps', function () {
return gulp.src('./node_modules/workbox-sw/build/workbox-sw.js')
.pipe(gulp.dest('./dist/libs/workbox-sw/build'))
})
gulp.task('mobile:pwa', ['mobile:pwa:index', 'mobile:pwa:deps'], function () {
const cloneConfig = _.merge({}, generate(), {
input: './src/pwa/app.js'
})
return rollup(cloneConfig).then(function (bundle) {
return bundle.write({
format: 'iife',
file: 'dist/app.js'
})
})
})
gulp.task('build-service-worker', function () {
return workboxBuild.injectManifest({
swSrc: 'src/pwa/sw.js',
swDest: 'dist/sw.js',
globDirectory: 'dist/',
globPatterns: [
'**/*.png',
'**/*.ico',
'app.js',
'pwa.html',
'*.json'
]
})
})
gulp.task('assets', function () {
return gulp.src(['./src/assets/**', '!*.ai'])
.pipe(gulp.dest('./dist/'))
})
gulp.task('mobile:all', ['mobile', 'mobile:pwa'], function () {
// inject the script into mobile.html and output seperatly
return gulp.src(['./dist/mobile.js'])
.pipe(replaceScript())
.pipe(rename({
extname: '.html'
}))
.pipe(gulp.dest('./dist/'))
})
gulp.task('env', function (cb) {
process.env.NODE_ENV = 'Production'
cb()
})
gulp.task('bundle', [], function () {
process.env.NODE_ENV = 'Production'
return rollup(generate()).then(function (bundle) {
process.env.NODE_ENV = 'Development'
return bundle.write({
format: 'iife',
file: './dist/bundle.js'
})
})
})
gulp.task('build', [], function (cb) {
runSequence('clean', ['install', 'mobile:all', 'assets'], 'build-service-worker', 'bundle', cb)
})
// create a task that ensures the `js` task is complete before
// reloading browsers
gulp.task('watch', ['build'], function (done) {
browserSync.reload()
done()
})
gulp.task('clean', function () {
return del('./dist')
})
// use default task to launch Browsersync and watch JS files
gulp.task('serve', ['build'], function () {
// Serve files from the root of this project
browserSync.init({
server: {
baseDir: './dist'
}
})
// add browserSync.reload to the tasks array to make
// all browsers reload after tasks are complete.
gulp.watch('src/**', ['watch'])
})
gulp.task('default', function (cb) {
runSequence('env', ['build'], cb)
})