-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathgulpfile.js
More file actions
232 lines (206 loc) · 5.31 KB
/
gulpfile.js
File metadata and controls
232 lines (206 loc) · 5.31 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright (c) 2018 ALSENET SA
*
* Author(s):
*
* Luc Deschenaux <luc.deschenaux@freesurf.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
*/
var path = require('path');
var gulp = require('gulp');
var watchify = require('watchify');
var browserify = require('browserify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var minify = require('gulp-minify');
var sourcemaps = require('gulp-sourcemaps');
var debug=require('gulp-debug');
var log = require('gulplog');
var extend = require('extend');
var browserSync = require('browser-sync');
var es = require('event-stream');
var htmlreplace = require('gulp-html-replace');
var insert = require('gulp-insert');
var baseDir='build';
var dist=false;
if (process.env.ENV=='dist') {
distEnv();
}
function distEnv() {
dist=true;
baseDir='dist';
}
function copy(){
var arglist=Array.prototype.slice.call(arguments);
var tasks=[];
arglist.forEach(function(cp){
tasks.push(
gulp.src(cp.src)
.pipe(debug({title: ' writing', showCount: false}))
.pipe(gulp.dest(cp.dest))
.on('error', function(e){
throw e;
})
);
});
return es.merge(tasks)
.on('error',function(e){
throw e;
});
}
gulp.task('dist-env', function(callback){
distEnv();
callback();
});
gulp.task('static-fs', function(callback) {
var exec = require('child_process').exec;
var job=exec('node ./static-fs-make.js', function(err, stdout, stderr){
callback(err);
});
});
gulp.task('scripts', ['static-fs'], function(){
return processScripts({
watchify: false,
minify: dist,
dest: baseDir+'/js'
})
});
gulp.task('copy',function(){
return copy({
src: './src/index.html',
dest: './'+baseDir+'/'
});
});
gulp.task('wrap', ['scripts'], function(){
var inFile=path.join(baseDir,'js/mrz-worker.bundle'+(dist?'-min':'')+'.js');
return gulp.src(inFile)
.pipe(insert.wrap('function mrz_worker(){\n','\n}'))
.pipe(rename({
dirname: '',
extname: '-wrapped.js'
}))
.pipe(debug({title: ' writing', showCount: false}))
.pipe(gulp.dest(baseDir+'/js'))
.on('error',function(err){
log.info(err);
});
});
gulp.task('htmlreplace', ['copy'], function(){
return gulp.src(baseDir+'/index.html')
.pipe(htmlreplace({
js: {
src: null,
tpl: [
'<script src="js/mrz-worker.bundle'+(dist?'-min':'')+'-wrapped.js"></script>',
'<script src="js/demo.bundle'+(dist?'-min':'')+'.js"></script>'
].join('\n')
}
}))
.pipe(gulp.dest('./'+baseDir+'/'));
})
gulp.task('build', [
'scripts',
'wrap',
'copy',
'htmlreplace'
]);
gulp.task('dist',['dist-env'], function(){
return gulp.start('build');
});
gulp.task('watch',function(){
gulp.watch('./src/index.html',['htmlreplace'])
return processScripts({
justWatch: true,
minify: dist,
dest: './'+baseDir+'/js'
});
});
gulp.task('serve',function(){
browserSync.init([baseDir+'/**'], {
watchOptions: {
ignoreInitial: true
},
server: {
baseDir: baseDir
},
https: false
});
});
gulp.task('default', ['build', 'watch'], function(){
return gulp.start('serve');
});
function processScripts(options){
options=options||{};
var files=[
'src/demo.js',
'src/mrz-worker.js'
];
var _watchify_enabled=(options.watchify===true || options.justWatch==true);
var _watchify=(_watchify_enabled)?watchify:function(b){return b};
var tasks=files.map(function(filename){
var b=_watchify(browserify(extend(true,{},
watchify.args,
{
debug: true
}
)))
.add(filename)
.on('update', bundle)
.on('log', log.info)
function bundle() {
var stream=b.transform('babelify', {
presets: [["env",{
"targets": {
"browsers": ["last 2 versions", "safari >=7"]
}
}]],
sourceMaps: true,
plugins: [
["transform-runtime", {
"helpers": false,
"polyfill": false,
"regenerator": true,
"moduleName": "babel-runtime"
}]
]
})
.bundle()
.on('error', function(err){
console.error(err);
this.emit('end');
})
.on('log',log.info)
.pipe(source(filename))
.pipe(buffer())
.pipe(rename({
dirname: '',
extname: '.bundle.js'
}))
.pipe(sourcemaps.init({loadMaps: true}))
if (options.minify) {
stream=stream.pipe(minify())
.on('error',function(err){
console.error(err);
})
}
return stream.pipe(sourcemaps.write('./'))
.pipe(debug({title: ' writing', showCount: false}))
.pipe(gulp.dest(options.dest))
}
return options.justWatch?b.bundle():bundle();
});
return es.merge.apply(null,tasks);
}