-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
295 lines (247 loc) · 7.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
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
287
288
289
290
291
292
293
294
295
var gulp = require('gulp'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
browserify = require('browserify'),
watchify = require('watchify'),
babelify = require('babelify'),
parcelify = require('parcelify'),
brfs = require('brfs'),
gulpLoadPlugins = require('gulp-load-plugins');
glob = require('glob'),
rimraf = require("rimraf"),
exec = require('child_process').exec;
// Automatically load any gulp plugins in your package.json
var $ = gulpLoadPlugins();
// External dependencies you do not want to rebundle while developing,
// but include in your application deployment
var dependencies = [
'@panorama/toolkit',
'cartodb-client',
'd3',
'flux',
'leaflet',
'lodash',
'queue-async',
'react',
'react-dom',
'react-leaflet',
'react-tabs'
];
var WEB_SERVER_PORT = 8888;
function browserifyTask (options) {
// Bundle the application with browserify
var appBundler = browserify({
entries: [options.src], // Application entry point; browserify finds and bundles all dependencies from there
transform: [babelify/*, brfs*/], // Convert React .jsx -> vanilla .js and enable ES6
// Need brfs to inline fs.realpathSync calls in `cartodb-api`, but due to
// https://github.com/substack/brfs/issues/39 it doesn't work. Therefore,
// we have to manually rewrite those lines in `cartodb-api` to hardcode the `require` paths.
debug: options.development, // Gives us sourcemapping
cache: {}, packageCache: {}, fullPaths: options.development // watchify requirements
});
// Use parcelify to pull in CSS from any node_module that specifies "style" and "transform" keys in package.json.
// @panorama components specify these keys.
parcelify(appBundler, {
bundles: {
style: './build/modules.css'
}
});
// We set our dependencies as externals on our app bundler when developing.
// You might consider doing this for production also and load two javascript
// files (main.js and vendors.js), as vendors.js will probably not change and
// takes full advantage of caching
appBundler.external(options.development ? dependencies : []);
// The bundling process
function createBundle() {
var start = Date.now();
console.log('Building APP bundle');
if (options.development) {
lintTask(options);
appBundler.bundle()
.on('error', $.util.log)
.pipe(source('main.js'))
.pipe(gulp.dest(options.dest))
.pipe($.livereload())
.pipe($.notify({
'onLast': true,
'message': function () { return 'APP bundle built in ' + (Date.now() - start) + 'ms'; }
}));
} else {
appBundler.bundle()
.on('error', $.util.log)
.pipe(source('main.js'))
.pipe(buffer())
.pipe($.uglify())
.pipe(gulp.dest(options.dest))
.pipe($.notify({
'onLast': true,
'message': function () { return 'APP bundle built in ' + (Date.now() - start) + 'ms'; }
}));
}
};
// Fire up Watchify when developing
if (options.development) {
appBundler = watchify(appBundler);
appBundler.on('update', createBundle);
}
createBundle();
// We create a separate bundle for our dependencies as they
// should not rebundle on file changes. This only happens when
// we develop. When deploying the dependencies will be included
// in the application bundle
if (options.development) {
var vendorsBundler = browserify({
debug: true,
require: dependencies
});
// Run the vendor bundle
var start = new Date();
console.log('Building VENDORS bundle');
vendorsBundler.bundle()
.on('error', $.util.log)
.pipe(source('vendors.js'))
.pipe(gulp.dest(options.dest))
.pipe($.notify({
'onLast': true,
'title': 'VENDORS bundle',
'message': function () { return 'built in ' + (Date.now() - start) + 'ms'; },
'notifier': function () {}
}));
} else {
// Distro bundles up a dummy vendors.js with nothing in it,
// so that loading vendors.js from index.html does not fail.
browserify({ require: '' })
.bundle()
.pipe(source('vendors.js'))
.pipe(gulp.dest(options.dest));
}
}
function cssTask(options) {
if (options.development) {
var run = function () {
var start = new Date();
console.log('Building CSS bundle');
gulp.src(options.src)
.pipe($.sass())
.pipe(gulp.dest(options.dest))
.pipe($.notify({
'onLast': true,
'title': 'CSS bundle',
'message': function () { return 'built in ' + (Date.now() - start) + 'ms'; },
'notifier': function () {}
}));
};
run();
gulp.watch(options.watchfiles, run);
} else {
gulp.src(options.src)
.pipe($.sass())
.pipe($.cssmin())
.pipe(gulp.dest(options.dest));
}
}
function copyTask(options) {
return gulp.src(options.src)
.pipe($.copy(options.dest, {
"prefix": options.pathDepth || 1
}));
}
function lintTask(options) {
console.log('ESLinting...');
return gulp.src(options.lintsrc)
.pipe($.eslint())
.pipe($.eslint.format())
.pipe($.eslint.failAfterError()) // Exit on lint error with code (1).
.pipe($.notify({
'onLast': true,
'title': 'Lint task',
'message': function () { return 'Linted.'; },
'notifier': function () {}
}));
}
function webserverTask(options) {
options = options || {}
var port = options.port || WEB_SERVER_PORT;
return $.connect.server({
root: './build/',
port: port,
livereload: false
});
}
function basemapsTask() {
console.log('Building basemaps...');
exec('npm run build:basemaps', function (err, stdout, stderr) {
if (err) {
console.log(stderr);
} else {
console.log(stdout.trim());
}
console.log('Basemaps build complete.');
});
}
function staticFolder() {
return gulp.src("static/**")
.pipe($.copy("build/"));
}
function staticDistFolder() {
return gulp.src("static/**")
.pipe($.copy("./dist"));
}
// Local development workflow:
// build component and test on local server (localhost:8888)
// with watcher to pick up changes and rebuild
gulp.task('default', function () {
rimraf("./build/**", function() {
copyTask({
"src" : "./src/*.html",
"dest" : "./build"
});
copyTask({
// "src" : "../panorama/dist/*.css*",
"src" : "./node_modules/@panorama/toolkit/dist/*.css*",
"dest" : "./build",
"pathDepth" : 4
});
basemapsTask();
browserifyTask({
"development" : true,
"lintsrc" : './src/**/*.js*',
"src" : './src/main.jsx',
"dest" : './build'
});
cssTask({
"development" : true,
"src" : './scss/*.scss',
"watchfiles" : './scss/**/*.scss',
"dest" : './build'
});
webserverTask();
staticFolder();
});
});
gulp.task('dist', function () {
rimraf("./dist/**", function() {
copyTask({
"src" : "./src/*.html",
"dest" : "./dist"
});
copyTask({
// "src" : "../panorama/dist/*.css*",
"src" : "./node_modules/@panorama/toolkit/dist/*.css*",
"dest" : "./dist",
"pathDepth" : 4
});
basemapsTask();
browserifyTask({
"development" : false,
"src" : './src/main.jsx',
"dest" : './dist'
});
cssTask({
"development" : false,
"src" : './scss/*.scss',
"dest" : './dist'
});
staticDistFolder()
});
});