-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
134 lines (115 loc) · 4.49 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
// require dependencies
const browserify = require('browserify');
const watchify = require('watchify');
const errorify = require('errorify');
const gulp = require('gulp');
const rename = require('gulp-rename');
const source = require('vinyl-source-stream');
const uglify = require('gulp-uglify');
const buffer = require('vinyl-buffer');
const size = require('gulp-size');
const mergeStream = require('merge-stream');
const gutil = require('gulp-util');
const prettyBytes = require('pretty-bytes');
const del = require('del');
// setup project parameters
const sourceFolder = './src/';
const sourceFile = 'main.js';
const destFolder = './build/';
const destFile = 'regular/build.js';
const destFileMinified = 'min/build.js';
const allSourceFiles = ['./src/**/!(gui).js']; // include all .js file except gui scripts
const manifestTemplate = './manifest.json';
// get browserify stream with common settings
function getBrowserify() {
return browserify({
debug: true,
entries: [sourceFolder + sourceFile]
})
.transform('rollupify', {config: {}})
.transform('babelify', {presets: ['es2015']});
}
// one time compilation of ECMA files into pure js
gulp.task('browserify', function () {
return getBrowserify()
.bundle()
.pipe(source(destFile))
.pipe(gulp.dest(destFolder));
});
// additional minification of built javascript files
gulp.task('uglify', ['browserify'], function () {
return gulp.src(destFolder + destFile)
.pipe(rename(destFileMinified))
.pipe(uglify({mangle: { keep_fnames: true} }))
.pipe(gulp.dest(destFolder));
});
// additional minification of built javascript files
gulp.task('buildManifest', function () {
return gulp.src(manifestTemplate)
.pipe(rename('manifest.json'))
.pipe(gulp.dest(destFolder + 'regular'))
.pipe(gulp.dest(destFolder + 'min'));
});
gulp.task('buildGui', function () {
return gulp.src(sourceFolder + 'gui/**/*', { base: sourceFolder })
.pipe(gulp.dest(destFolder + 'regular'))
.pipe(gulp.dest(destFolder + 'min'));
});
//
gulp.task('buildStats', function () {
// uncomment showFiles option to see all file sizes separately
const sizeClean = size(/*{showFiles: true}*/);
const sizeBuild = size(/*{showFiles: true}*/);
const sizeBuildMinified = size(/*{showFiles: true}*/);
var cleanFilesCount = 0;
const build = gulp.src(destFolder + destFile)
.pipe(sizeBuild);
const buildMinified = gulp.src(destFolder + destFileMinified)
.pipe(sizeBuildMinified);
const cleanProject = gulp.src(allSourceFiles)
.on('data', function() {
cleanFilesCount++;
})
.pipe(sizeClean)
// wait for streams to finish and print some useful information
return mergeStream(cleanProject, build, buildMinified).on('end', function () {
gutil.log('Clean project has ' + cleanFilesCount + ' files with total size ' + sizeClean.prettySize);
if (sizeBuild.size) {
gutil.log('Build size is ' + sizeBuild.prettySize + ' (' + parseInt(sizeBuild.size / sizeClean.size * 100) + '% of original size)');
} else {
gutil.log('Build not yet done');
}
if (sizeBuildMinified.size) {
gutil.log('Build size after minification is ' + sizeBuildMinified.prettySize + ' (' + parseInt(sizeBuildMinified.size / sizeClean.size * 100) + '% of original size and ' + parseInt(sizeBuildMinified.size / sizeBuild.size * 100) + '% of build size)');
} else {
gutil.log('Build minification not yet done');
}
});
});
// recompile ECMA to js after some file changed (main task during development)
gulp.task('browserifyWatch', function () {
const bundler = watchify(getBrowserify());
bundler.plugin(errorify, {/* replacer: function (error) { return 'custom error handler: ' + error; } */});
bundler.on('update', rebundle);
function rebundle() {
gutil.log('browserifyWatch - rebundling');
return bundler.bundle()
.pipe(source(destFile))
.pipe(gulp.dest(destFolder));
/* usefull?
.on('end', function () {
gulp.start('buildManifest');
});
*/
}
return rebundle();
});
gulp.task('build', ['uglify', 'buildManifest', 'buildGui'], function () {
// after build is done, print stats
gulp.start('buildStats');
});
gulp.task('clean', function () {
del([destFolder + '**/*']);
});
// default task: create full build
gulp.task('default', ['build']);