-
Notifications
You must be signed in to change notification settings - Fork 105
/
gulpfile.js
120 lines (100 loc) · 3.18 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
var gulp = require('gulp');
var clean = require('gulp-clean');
var gutil = require('gulp-util');
var source = require('vinyl-source-stream');
var buffer = require('gulp-buffer');
var uglify = require('gulp-uglify');
var gulpif = require('gulp-if');
var exorcist = require('exorcist');
var babelify = require('babelify');
var browserify = require('browserify');
var browserSync = require('browser-sync');
/**
* Using different folders/file names? Change these constants:
*/
var PHASER_PATH = './node_modules/phaser/build/';
var BUILD_PATH = './build';
var SCRIPTS_PATH = BUILD_PATH + '/scripts';
var SOURCE_PATH = './src';
var STATIC_PATH = './static';
var ENTRY_FILE = SOURCE_PATH + '/index.js';
var OUTPUT_FILE = 'game.js';
function isProduction() {
return (process.env.NODE_ENV === 'production') ? true : false;
}
/**
* Deletes all content inside the './build' folder.
*/
gulp.task('cleanBuild', function() {
return gulp.src(BUILD_PATH, {read: false})
.pipe(clean());
});
/**
* Copies the content of the './static' folder into the '/build' folder.
* Check out README.md for more info on the '/static' folder.
*/
gulp.task('copyStatic', ['cleanBuild'], function() {
return gulp.src(STATIC_PATH + '/**/*')
.pipe(gulp.dest(BUILD_PATH));
});
/**
* Copies required Phaser files from the './node_modules/Phaser' folder into the './build/scripts' folder.
* This way you can call 'npm update', get the lastest Phaser version and use it on your project with ease.
*/
gulp.task('copyPhaser', ['copyStatic'], function() {
var srcList = ['phaser.min.js'];
if (!isProduction()) {
srcList.push('phaser.map', 'phaser.js');
}
srcList = srcList.map(function(file) {
return PHASER_PATH + file;
});
return gulp.src(srcList)
.pipe(gulp.dest(SCRIPTS_PATH));
});
/**
* Transforms ES2015 code into ES5 code.
* Optionally: Creates a sourcemap file 'game.js.map' for debugging.
*/
gulp.task('build', ['copyPhaser'], function () {
var sourcemapPath = SCRIPTS_PATH + '/' + OUTPUT_FILE + '.map';
return browserify({
entries: ENTRY_FILE,
debug: true
})
.transform(babelify)
.bundle().on('error', function(error){
gutil.log(gutil.colors.red('[Build Error]', error.message));
this.emit('end');
})
.pipe(gulpif(!isProduction(), exorcist(sourcemapPath)))
.pipe(source(OUTPUT_FILE))
.pipe(buffer())
.pipe(gulpif(isProduction(), uglify()))
.pipe(gulp.dest(SCRIPTS_PATH));
});
/**
* Starts the Browsersync server.
* Watches for file changes in the 'src' folder.
*/
gulp.task('serve', ['build'], function() {
browserSync({
server: {
baseDir: BUILD_PATH
},
open: false
});
gulp.watch(SOURCE_PATH + '/**/*.js', ['watch-js']);
});
/**
* Rebuilds and reloads the project when executed.
*/
gulp.task('watch-js', ['build'], browserSync.reload);
/**
* The tasks are executed in the following order:
* 'cleanBuild' -> 'copyStatic' -> 'copyPhaser' -> 'build' -> 'serve'
*
* Read more about task dependencies in Gulp:
* https://medium.com/@dave_lunny/task-dependencies-in-gulp-b885c1ab48f0
*/
gulp.task('default', ['serve']);