-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
118 lines (101 loc) · 3.52 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
/**
* Created by s.neidig on 11/07/17.
*/
const gulp = require('gulp');
const fs = require('fs');
const path = require('path');
const opn = require('opn');
const minify = require('gulp-minify');
const babel = require('gulp-babel');
const getFolderSize = require('get-folder-size');
const imagemin = require('gulp-imagemin');
gulp.task('minifyAssets', () =>
gulp.src('assets/images/*')
.pipe(imagemin())
.pipe(gulp.dest('assets2'))
);
gulp.task('copy', ['minify', 'transpile', 'linkPhaser', 'calculateSize'], () => {
const destination = '/Applications/MAMP/htdocs/dasheck-hackathon-phaser';
const sources = ['dist', 'assets'].reverse();
sources.forEach(source => copyFolderRecursiveSync(path.join(process.cwd(), source), destination));
copyFileSync(path.join(process.cwd(), 'index.html'), destination);
});
gulp.task('open', () => {
opn('http://localhost:8888/dasheck-hackathon-phaser');
});
gulp.task('minify', ['transpile'], (done) => {
gulp.src('dist/**/*.js')
.pipe(minify({
ext: {
min: '.js'
},
noSource: true,
preserveComments: () => false,
ignoreFiles: ['phaser.js']
}))
.pipe(gulp.dest('dist'))
.on('end', done);
});
gulp.task('calculateSize', ['minify', 'transpile'], (done) => {
getFolderSize(path.join(process.cwd(), 'dist'), /phaser.js/g, (error, distSize) => {
getFolderSize(path.join(process.cwd(), 'assets'), (error, assetsSize) => {
console.log('Size:', ((distSize + assetsSize) / 1024.0).toFixed(2), 'KB');
done();
});
});
});
gulp.task('transpile', () => {
deleteFolderRecursive(path.join(process.cwd(), 'dist'));
return gulp.src('js/**/*.js')
.pipe(babel({
presets: ['es2015']
}))
.pipe(gulp.dest('dist'));
});
gulp.task('linkPhaser', ['minify'], () => {
fs.mkdirSync(path.join(process.cwd(), 'dist/libs'));
copyFileSync(path.join(process.cwd(), 'libs/phaser.js'), path.join(process.cwd(), 'dist/libs/phaser.js'));
});
function copyFileSync(source, target) {
var targetFile = target;
//if target is a directory a new file with the same name will be created
if (fs.existsSync(target)) {
if (fs.lstatSync(target).isDirectory()) {
targetFile = path.join(target, path.basename(source));
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
function copyFolderRecursiveSync(source, target) {
var files = [];
//check if folder needs to be created or integrated
var targetFolder = path.join(target, path.basename(source));
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
//copy
if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach(function (file) {
var curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderRecursiveSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
}
function deleteFolderRecursive(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};