-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
119 lines (102 loc) · 3.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
var program = require('commander');
var browserify = require('browserify');
var chalk = require('chalk');
var express = require('express');
var path = require('path');
var rimraf = require('rimraf');
var gulp = require('gulp');
var gutil = require('gulp-util');
var gulpif = require('gulp-if');
var buffer = require('gulp-buffer');
var concat = require('gulp-concat');
var cssmin = require('gulp-cssmin');
var eslint = require('gulp-eslint');
var htmlmin = require('gulp-htmlmin');
var less = require('gulp-less');
var micro = require('gulp-micro');
var size = require('gulp-size');
var uglify = require('gulp-uglify');
var zip = require('gulp-zip');
var source = require('vinyl-source-stream');
program.on('--help', function(){
console.log(' Tasks:');
console.log();
console.log(' build build the game');
console.log(' clean delete generated files');
console.log(' dist generate archive');
console.log(' serve launch development server');
console.log(' watch watch for file changes and rebuild automatically');
console.log();
});
program
.usage('<task> [options]')
.option('-P, --prod', 'generate production assets')
.parse(process.argv);
var prod = !!program.prod;
gulp.task('default', ['build']);
gulp.task('build', ['build_source', 'build_index', 'build_styles']);
gulp.task('build_source', function() {
var bundler = browserify('./src/main', {debug: !prod});
if (prod) {
bundler.plugin(require('bundle-collapser/plugin'));
}
return bundler
.bundle()
.on('error', browserifyError)
.pipe(source('build.js'))
.pipe(buffer())
.pipe(gulpif(prod, uglify()))
.pipe(gulp.dest('build'));
});
gulp.task('build_index', function() {
return gulp.src('src/index.html')
.pipe(gulpif(prod, htmlmin({
collapseWhitespace: true,
removeAttributeQuotes: true,
removeComments: true,
})))
.pipe(gulp.dest('build'));
});
gulp.task('build_styles', function() {
return gulp.src('src/*.css')
.pipe(concat('build.css'))
.pipe(gulpif(prod, cssmin()))
.pipe(gulp.dest('build'));
});
gulp.task('clean', function() {
rimraf.sync('build');
rimraf.sync('dist');
});
gulp.task('lint', function() {
return gulp.src(['*.js', 'src/**/*.js'])
.pipe(eslint())
.pipe(eslint.format());
});
gulp.task('dist', ['build'], function() {
if (!prod) {
gutil.log(chalk.yellow('WARNING'), chalk.gray('Missing flag --prod'));
gutil.log(chalk.yellow('WARNING'), chalk.gray('You should generate production assets to lower the archive size'));
}
return gulp.src('build/*')
.pipe(zip('archive.zip'))
.pipe(size())
.pipe(micro({limit: 13 * 1024}))
.pipe(gulp.dest('dist'));
});
gulp.task('watch', function() {
gulp.watch('src/**/*.js', [/*'lint', */'build_source']);
gulp.watch('src/*.css', ['build_styles']);
gulp.watch('src/index.html', ['build_index']);
});
gulp.task('serve', ['build'], function() {
var htdocs = path.resolve(__dirname, 'build');
var app = express();
app.use(express.static(htdocs));
app.listen(3000, function() {
gutil.log("Server started on '" + chalk.green('http://localhost:3000') + "'");
});
});
function browserifyError(err) {
gutil.log(chalk.red('ERROR'), chalk.gray(err.message));
this.emit('end');
}