-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgulpfile.js
139 lines (121 loc) · 4.24 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
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
#!/usr/bin/env node
'use strict';
/*
* Note:
* met an issue that using ES6 can cause trouble with uglify
* events.js:160
throw er; // Unhandled 'error' event
^
GulpUglifyError: unable to minify JavaScript
Need change ES6 to ES5 format, remove 'let' to 'var'
*
*/
var version = require('./lib/version.json');
var path = require('path');
var del = require('del');
var gulp = require('gulp');
var browserify = require('browserify');
var jshint = require('gulp-jshint');
var uglify = require('gulp-uglify');//Using this may cause an error when using ES6
var rename = require('gulp-rename');
var source = require('vinyl-source-stream');
var exorcist = require('exorcist');
var bower = require('bower');
var streamify = require('gulp-streamify');
var replace = require('gulp-replace');
//To debug any issues with gulp
var gutil = require('gulp-util');
var DEST = path.join(__dirname, 'dist/');
var src = 'index';
var dst = 'chain3';
var lightDst = 'chain3-light';
var browserifyOptions = {
debug: true,
insert_global_vars: false, // jshint ignore:line
detectGlobals: false,
bundleExternal: true
};
var ugliyOptions = {
compress: {
dead_code: true, // jshint ignore:line
drop_debugger: true, // jshint ignore:line
global_defs: { // jshint ignore:line
"DEBUG": false // matters for some libraries
}
}
};
gulp.task('version', function(){
gulp.src(['./package.json'])
.pipe(replace(/\"version\"\: \"([\.0-9]*)\"/, '"version": "'+ version.version + '"'))
.pipe(gulp.dest('./'));
gulp.src(['./bower.json'])
.pipe(replace(/\"version\"\: \"([\.0-9]*)\"/, '"version": "'+ version.version + '"'))
.pipe(gulp.dest('./'));
gulp.src(['./package.js'])
.pipe(replace(/version\: \'([\.0-9]*)\'/, "version: '"+ version.version + "'"))
.pipe(gulp.dest('./'));
});
gulp.task('bower', ['version'], function(cb){
bower.commands.install().on('end', function (installed){
console.log(installed);
cb();
});
});
gulp.task('lint', [], function(){
return gulp.src(['./*.js', './lib/*.js'])
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
gulp.task('clean', ['lint'], function(cb) {
del([ DEST ]).then(cb.bind(null, null));
});
//build the light version of the chain3 library used for browser
gulp.task('light', ['clean'], function () {
return browserify(browserifyOptions)
.require('./' + src + '.js', {expose: 'chain3'})
.ignore('bignumber.js')
.require('./lib/utils/browser-bn.js', {expose: 'bignumber.js'}) // fake bignumber.js
.add('./' + src + '.js')
.bundle()
.pipe(exorcist(path.join( DEST, lightDst + '.js.map')))
.pipe(source(lightDst + '.js'))
.pipe(gulp.dest( DEST ))
.pipe(streamify(uglify()))
.pipe(rename(lightDst + '.min.js'))
.pipe(gulp.dest( DEST ));
});
/*build the chain3.js under dist using files listed in the [lint] option*/
gulp.task('standalone', ['clean'], function () {
return browserify(browserifyOptions)
.require('./' + src + '.js', {expose: 'chain3'})
.require('bignumber.js') // expose it to dapp users
.add('./' + src + '.js')
.ignore('crypto')
.bundle()
.pipe(exorcist(path.join( DEST, dst + '.js.map')))
.pipe(source(dst + '.js'))
.pipe(gulp.dest( DEST ))
.pipe(streamify(uglify()))
.on('error', function (err) {gutil.log(gutil.colors.red('[Error]'),err.toString());})
.pipe(rename(dst + '.min.js'))
.pipe(gulp.dest( DEST ));
});
gulp.task('chain3', ['clean'], function () {
return browserify(browserifyOptions)
.require('./' + src + '.js', {expose: 'chain3'})
.require('bignumber.js') // expose it to dapp users
.add('./' + src + '.js')
.ignore('crypto')
.bundle()
.pipe(exorcist(path.join( DEST, dst + '.js.map')))
.pipe(source(dst + '.js'))
.pipe(gulp.dest( DEST ))
.pipe(streamify(uglify(ugliyOptions)))
.pipe(rename(dst + '.min.js'))
.pipe(gulp.dest( DEST ));
});
gulp.task('watch', function() {
gulp.watch(['./lib/*.js'], ['lint', 'build']);
});
/*Default build process*/
gulp.task('default', ['version', 'lint', 'clean', 'light', 'standalone']);