-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
286 lines (239 loc) · 7.91 KB
/
gulpfile.js
File metadata and controls
286 lines (239 loc) · 7.91 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const gulp = require('gulp');
const sourcemaps = require('gulp-sourcemaps');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const browserify = require('browserify');
const watchify = require('watchify');
const babelify = require('babelify');
const browserSync = require('browser-sync');
const plumber = require('gulp-plumber');
const del = require('del');
const gutil = require('gulp-util');
const preprocess = require('gulp-preprocess');
const preprocessify = require('preprocessify');
const compilerPackage = require('google-closure-compiler');
const closureCompiler = compilerPackage.gulp(/* options */);
const runSequence = require('run-sequence').use(gulp);
const stripDebug = require('./__scripts__/strip-debug');
const fs = require('fs');
const replace = require('gulp-replace');
require('babel-polyfill');
const cfgBrowserify = {
cache: {},
packageCache: {},
debug: true,
fullPaths: false
};
const cfgTransform = {
'global': true,
'only': /^(?:.*\/node_modules\/black\/|(?!.*\/node_modules\/)).*$/,
'presets': ['es2015', 'stage-0'],
'plugins': [
['babel-root-slash-import', {
'rootPathSuffix': 'js'
}],
['transform-runtime', {
'polyfill': false,
'regenerator': true
}]
]
};
let bundler = browserify('./js/main.js', cfgBrowserify).transform(babelify, cfgTransform);
const preprocessCfg = {
includeExtensions: ['.js'],
context: { DEBUG: true }
};
bundler = bundler.transform(preprocessify, preprocessCfg);
const defPath = './node_modules/black/package.json';
const originalPath = './node_modules/black/package-original.json';
const devPath = './node_modules/black/package-development.json';
const prodPath = './node_modules/black/package-production.json';
function preparePackages() {
fs.copyFileSync(defPath, prodPath);
fs.copyFileSync(defPath, devPath);
fs.copyFileSync(defPath, originalPath);
const dev = JSON.parse(fs.readFileSync(devPath));
dev.env = 'development';
const prod = JSON.parse(fs.readFileSync(prodPath));
prod.env = 'production';
prod.main = 'dist/gcc/black-es6-module.js';
fs.writeFileSync(devPath, JSON.stringify(dev, null, 2));
fs.writeFileSync(prodPath, JSON.stringify(prod, null, 2));
}
gulp.task('sheets', function () {
return gulp.src(['./sheets/*.{png,jpg,json}'])
.pipe(plumber())
.pipe(gulp.dest('./dist/assets'))
.pipe(browserSync.stream());
});
gulp.task('textures', function () {
return gulp.src(['./textures/*.*'])
.pipe(plumber())
.pipe(gulp.dest('./dist/assets'))
.pipe(browserSync.stream());
});
gulp.task('spine', function () {
return gulp.src(['./spine/*.*'])
.pipe(plumber())
.pipe(gulp.dest('./dist/assets'))
.pipe(browserSync.stream());
});
gulp.task('fonts', function () {
return gulp.src(['./fonts/*.*'])
.pipe(plumber())
.pipe(gulp.dest('./dist/assets'))
.pipe(browserSync.stream());
});
gulp.task('audio', function () {
return gulp.src(['./audio/*.*'])
.pipe(plumber())
.pipe(gulp.dest('./dist/assets'))
.pipe(browserSync.stream());
});
gulp.task('index', ['sheets'], function () {
return gulp.src(['./html/index.html'])
.pipe(plumber())
.pipe(gulp.dest('./dist'))
.pipe(browserSync.stream());
});
gulp.task('clean', function () {
del.sync('./dist/**/*.*');
});
gulp.task('development-package', function (done) {
if (fs.existsSync(originalPath) === false)
preparePackages();
fs.copyFileSync(devPath, defPath);
done();
});
gulp.task('production-package', function (done) {
if (fs.existsSync(originalPath) === false)
preparePackages();
fs.copyFileSync(prodPath, defPath);
done();
});
gulp.task('restore-package', function (done) {
if (fs.existsSync(originalPath) === false)
return;
fs.copyFileSync(originalPath, defPath);
done();
});
gulp.task('strip-debug', function () {
return gulp.src(['./node_modules/black/dist/black-es6-module.js'], {})
.pipe(preprocess({
context: { HIDE_SPLASH_SCREEN: true }
}))
.pipe(stripDebug())
.pipe(gulp.dest('./node_modules/black/dist/gcc/'));
});
gulp.task('trim-gcc', function () {
gulp.src(['./dist/code.js'])
.pipe(replace(/configurable:!0,/g, ' '))
.pipe(replace(/enumerable:!0,/g, ' '))
.pipe(replace(/writable:!0,/g, ' '))
.pipe(gulp.dest('./dist/'));
});
gulp.task('compile-gcc', function () {
return gulp.src(['./js/**/*.js'], {})
.pipe(preprocess())
.pipe(stripDebug())
.pipe(closureCompiler({
externs: './externs/w3c_audio.js',
entry_point: 'main.js',
compilation_level: 'ADVANCED',
rewrite_polyfills: false,
language_in: 'ECMASCRIPT6',
language_out: 'ECMASCRIPT5_STRICT',
output_wrapper: '(function(){\n%output%\n}).call(this);',
js_output_file: 'code.js',
module_resolution: 'NODE',
dependency_mode: 'STRICT',
extra_annotation_name: 'cat',
jscomp_warning: 'newCheckTypes',
use_types_for_optimization: true,
process_common_js_modules: true,
generate_exports: true,
export_local_property_definitions: true,
js: ['./__scripts__/base.js', 'node_modules/black/dist/gcc/black-es6-module.js', 'node_modules/black/package.json']
}))
.pipe(buffer())
.pipe(gulp.dest('./dist'));
});
function compile_babel() {
let stream = null;
stream = bundler.bundle()
.on('error', function (err) {
console.log(err.message);
browserSync.notify(err.message, 3000);
this.emit('end');
})
.on('end', function () { })
.pipe(plumber())
.pipe(source('code.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist'));
stream.on('end', x => {
browserSync.reload({ stream: false })
});
return stream;
}
function bs() {
let cfg = {
server: { baseDir: './dist' },
reloadDelay: 50,
open: true,
port: 4245
};
return browserSync(cfg);
}
gulp.task('watchify', function () {
let cfg = {
debug: true,
//ignoreWatch: ['**/node_modules/**'],
poll: true,
awaitWriteFinish: true
};
bundler = watchify(bundler, cfg);
bundler.on('update', compile_babel);
bundler.on('time', time => {
gutil.log(gutil.colors.yellow(`Compiled in ${time / 1000}s`));
});
bundler.on('log', function (msg) {
gutil.log(msg);
});
return compile_babel();
});
gulp.task('build:dev', function () {
return runSequence('clean', ['sheets', 'textures', 'spine', 'index', 'fonts', 'audio'], 'development-package', 'watchify', 'watch-assets', bs);
});
gulp.task('build:prod', function () {
return runSequence('clean', ['sheets', 'textures', 'spine', 'index', 'fonts', 'audio'], 'production-package', 'strip-debug', 'compile-gcc', 'restore-package', 'trim-gcc');
});
gulp.task('watch-assets', function () {
var watcher1 = gulp.watch('./sheets/*.*', ['sheets']);
var watcher2 = gulp.watch('./fonts/*.*', ['fonts']);
var watcher3 = gulp.watch('./spine/*.*', ['spine']);
var watcher4 = gulp.watch('./html/*.*', ['index']);
var watcher5 = gulp.watch('./audio/*.*', ['audio']);
var watcher6 = gulp.watch('./textures/*.*', ['textures']);
watcher1.on('change', function (event) {
console.log('Texture Atlas file ' + event.path + ' was ' + event.type + ', running tasks...');
});
watcher2.on('change', function (event) {
console.log('Font file ' + event.path + ' was ' + event.type + ', running tasks...');
});
watcher3.on('change', function (event) {
console.log('Spine file ' + event.path + ' was ' + event.type + ', running tasks...');
});
watcher4.on('change', function (event) {
console.log('HTML file ' + event.path + ' was ' + event.type + ', running tasks...');
});
watcher5.on('change', function (event) {
console.log('Audio file ' + event.path + ' was ' + event.type + ', running tasks...');
});
watcher6.on('change', function (event) {
console.log('Texture file ' + event.path + ' was ' + event.type + ', running tasks...');
});
});
gulp.task('default', ['build:dev']);