This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
210 lines (189 loc) · 7.57 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
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
var gulp = require('gulp'),
nodemon = require('nodemon'),
// Generic imports
fs = require('fs'),
Stream = require('stream'),
gutil = require('gulp-util'),
path = require('path'),
clean = require('rimraf'),
plumber = require('gulp-plumber'),
replace = require('gulp-replace'),
nodemon = require('gulp-nodemon'),
// Browserify-related imports
browserify = require('browserify'),
source = require('vinyl-source-stream'),
watchify = require('watchify'),
babelify = require('babelify'),
uglify = require('gulp-uglify'),
buffer = require('vinyl-buffer')
// LESS-related imports
less = require('gulp-less'),
sourcemaps = require('gulp-sourcemaps'),
// HTML-related imports
minify = require('gulp-minify-html'),
// Dev utility imports
livereload = require('gulp-livereload');
/**************************************** CONSTANTS ****************************************/
var PUBLIC_FOLDER_NAME = 'public',
FRONTEND_FOLDER_NAME = 'frontend',
FRONTEND_JS_FOLDER_NAME = 'js',
FRONTEND_LESS_FOLDER_NAME = 'less',
FRONTEND_IMG_FOLDER_NAME = 'img',
FRONTEND_VENDOR_FOLDER_NAME = 'vendor',
FRONTEND_HTML_FOLDER_NAME = 'pages',
BACKEND_FOLDER_NAME = 'backend',
ENV_FOLDER_NAME = 'env',
FRONTEND_JS_ENTRY_POINT = 'main.js',
FRONTEND_LESS_ENTRY_POINT = 'main.less',
BACKEND_ENTRY_POINT = 'index.js',
ENV_FOLDER_PATH = path.join(__dirname, ENV_FOLDER_NAME),
PUBLIC_FOLDER_PATH = path.join(__dirname, PUBLIC_FOLDER_NAME),
FRONTEND_FOLDER_PATH = path.join(__dirname, FRONTEND_FOLDER_NAME),
FRONTEND_JS_FOLDER_PATH = path.join(FRONTEND_FOLDER_PATH, FRONTEND_JS_FOLDER_NAME),
FRONTEND_LESS_FOLDER_PATH = path.join(FRONTEND_FOLDER_PATH, FRONTEND_LESS_FOLDER_NAME),
FRONTEND_IMG_FOLDER_PATH = path.join(FRONTEND_FOLDER_PATH, FRONTEND_IMG_FOLDER_NAME),
FRONTEND_VENDOR_FOLDER_PATH = path.join(FRONTEND_FOLDER_PATH, FRONTEND_VENDOR_FOLDER_NAME),
FRONTEND_HTML_FOLDER_PATH = path.join(FRONTEND_FOLDER_PATH, FRONTEND_HTML_FOLDER_NAME),
BACKEND_FOLDER_PATH = path.join(__dirname, BACKEND_FOLDER_NAME),
PROJECT_NAME = __dirname.split(path.sep).pop();
/************************************* HELPER FUNCTIONS ************************************/
var helpers = {
rebundle: function(bundler, done) {
var time = (new Date()).getTime();
gutil.log('Started re-bundling client js');
bundler
.bundle(function(err) {
if (!err) {
gutil.log('Finished re-bundling client js after ' + (((new Date()).getTime() - time) / 1000) + ' s');
if (done) done();
} else {
gutil.log('Failed to re-bundle client js');
console.log(err);
if (done) done(err);
}
})
.pipe(plumber())
.pipe(source(FRONTEND_JS_ENTRY_POINT))
.pipe(buffer())
// .pipe(uglify()) // Commented for sanity
.pipe(gulp.dest(PUBLIC_FOLDER_PATH));
},
delay: function(callback) {
// Waits a second before executing a function
return function() {
setTimeout(callback, 1000);
};
},
copyAssets: function(folderName) {
return function() {
gulp.src(path.join(FRONTEND_FOLDER_PATH, folderName, '**', '*'))
.pipe(plumber())
.pipe(gulp.dest(path.join(PUBLIC_FOLDER_PATH, folderName)));
};
}
};
/**************************************** FRONTEND ****************************************/
// Compiles the client js
gulp.task('browserify', function(cb) {
var bundler = browserify({
cache: {},
packageCache: {},
fullPaths: true
});
// ES6 Compatibility
bundler.transform(babelify);
// Add the entry point
bundler.add(path.join(FRONTEND_JS_FOLDER_PATH, FRONTEND_JS_ENTRY_POINT));
// Perform initial rebundle
return helpers.rebundle(bundler, cb);
});
// Watches and recompiles client js
gulp.task('watchify', function(cb) {
var bundler = browserify({
cache: {},
packageCache: {},
fullPaths: true,
debug: true
});
// Pass the browserify bundler to watchify
bundler = watchify(bundler);
// ES6 Compatibility
bundler.transform(babelify);
// Bundlize on updates
bundler.on('update', function() {
helpers.rebundle(bundler);
});
// Add the entry point
bundler.add(path.join(FRONTEND_JS_FOLDER_PATH, FRONTEND_JS_ENTRY_POINT));
// Perform initial rebundle
return helpers.rebundle(bundler, cb);
});
// Compiles the client less
gulp.task('less', function() {
gulp.src(path.join(FRONTEND_LESS_FOLDER_PATH, FRONTEND_LESS_ENTRY_POINT))
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(less())
.pipe(sourcemaps.write())
.pipe(gulp.dest(PUBLIC_FOLDER_PATH));
});
gulp.task('less-dev', function() {
gulp.src(path.join(FRONTEND_LESS_FOLDER_PATH, FRONTEND_LESS_ENTRY_POINT))
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(less())
.pipe(sourcemaps.write())
.pipe(gulp.dest(PUBLIC_FOLDER_PATH))
.pipe(livereload());
});
// Condenses the pages
gulp.task('html', function() {
gulp.src(path.join(FRONTEND_HTML_FOLDER_PATH, '**', '*'))
.pipe(plumber())
.pipe(minify({
empty: true,
spare: true
}))
.pipe(gulp.dest(path.join(PUBLIC_FOLDER_PATH, FRONTEND_HTML_FOLDER_NAME)));
});
gulp.task('html-dev', function() {
gulp.src(path.join(FRONTEND_HTML_FOLDER_PATH, '**', '*'))
.pipe(plumber())
.pipe(minify({
empty: true,
spare: true
}))
.pipe(replace('</body>', '<script src="http://localhost:35729/livereload.js"></script></body>'))
.pipe(gulp.dest(path.join(PUBLIC_FOLDER_PATH, FRONTEND_HTML_FOLDER_NAME)))
.pipe(livereload());
});
// Moves images
gulp.task('images', helpers.copyAssets(FRONTEND_IMG_FOLDER_NAME));
gulp.task('images-delayed', helpers.delay(helpers.copyAssets(FRONTEND_IMG_FOLDER_NAME)));
// Moves vendor files
gulp.task('vendor', helpers.copyAssets(FRONTEND_VENDOR_FOLDER_NAME));
gulp.task('vendor-delayed', helpers.delay(helpers.copyAssets(FRONTEND_VENDOR_FOLDER_NAME)));
// Clears all compiled client code
gulp.task('clean', function() {
clean.sync(PUBLIC_FOLDER_PATH);
});
/**************************************** BACKEND ****************************************/
gulp.task('server', function() {
nodemon({
script: path.join(BACKEND_FOLDER_PATH, BACKEND_ENTRY_POINT),
env: require('./env/dev.json'),
ignore: ['frontend/*', 'gulpfile.js']
});
});
/**************************************** GENERIC ****************************************/
// Watches changes to the client code
gulp.task('watch', ['clean', 'less-dev', 'html-dev', 'images', 'vendor'], function() {
gulp.watch(path.join(FRONTEND_HTML_FOLDER_PATH, '**', '*'), ['html-dev']);
gulp.watch(path.join(FRONTEND_LESS_FOLDER_PATH, '**', '*'), ['less-dev']);
gulp.watch(path.join(FRONTEND_IMG_FOLDER_PATH, '**', '*'), ['images-delayed']);
gulp.watch(path.join(FRONTEND_VENDOR_FOLDER_PATH, '**', '*'), ['vendor-delayed']);
// Start the livereload server
livereload.listen();
});
// Run all compilation tasks
gulp.task('default', ['watch', 'watchify', 'server']);