-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
217 lines (191 loc) · 6.07 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
'use strict';
const gulp = require('gulp');
const packageJson = require('./package.json');
const watch = require('gulp-watch');
const es = require('event-stream');
const debug = require('gulp-debug');
const preprocess = require('gulp-preprocess');
const rename = require('gulp-rename');
const del = require('del');
const fs = require('fs');
const ms = require('ms');
const browserify = require('browserify');
const watchify = require('watchify');
const source = require('vinyl-source-stream');
const ngHtml2Js = require('browserify-ng-html2js');
const transform = require('vinyl-transform');
const uglify = require('gulp-uglify');
const buffer = require('vinyl-buffer');
const sourcemaps = require('gulp-sourcemaps');
const babelify = require('babelify');
const onceConcat = require('gulp-concat');
const continuousConcat = require('gulp-continuous-concat');
let concat = onceConcat;
const configFile = require('./server/config.js');
const config = {
DEBUG: true,
EXPERIMENTAL: false,
CLIENT_VERSION: packageJson.version,
SECURE: configFile.secure,
HOST: configFile.hostname,
PORT: configFile.port,
CONFIGURATION: 'debug',
SUBDIR: configFile.subdir,
FORMS_SUBDIR: configFile.formsSubdir,
get buildDir() { return './build'; },
get outDir() { return this.buildDir + '/www'; },
get URL_BASE_PATH() { return (this.SECURE ? 'https' : 'http') + '://' + this.HOST + ':' + this.PORT + this.SUBDIR },
get FORMS_URL_BASE_PATH() { return (this.SECURE ? 'https' : 'http') + '://' + this.HOST + ':' + this.PORT + this.FORMS_SUBDIR },
get WS_BASE_PATH() { return (this.SECURE ? 'wss' : 'ws') + '://' + this.HOST + ':' + this.PORT + this.SUBDIR; },
watch: false,
};
function setProd() {
config.DEBUG = false;
config.EXPERIMENTAL = false;
config.CONFIGURATION = 'prod';
}
function srcWatch(spec) {
const result = gulp.src(spec);
if(!config.watch)
return result;
return result.pipe(watch(spec));
}
gulp.task('clean-output', [], function() {
return del([config.outDir]);
});
function buildApp() {
var clientBrowserify = browserify({
entries: ['client/index.js'],
cache: {},
packageCache: {},
debug: config.DEBUG,
bare: true,
});
// Force using the d3 browser build instead of the node build
clientBrowserify.require('./node_modules/d3/build/d3.js', {expose: 'd3'});
if(!config.DEBUG) {
clientBrowserify.transform(
babelify.configure({
presets: [
['env', {
targets: {
browsers: ['last 2 versions', 'not ie < 11']
},
useBuiltins: true,
}]
]
})
);
}
clientBrowserify.transform(ngHtml2Js({
module: 'investigator.Templates',
requireAngular: true,
baseDir: './client/',
}))
var formsBrowserify = browserify({
entries: ['client/www/forms/index.js'],
cache: {},
packageCache: {},
debug: config.DEBUG,
bare: true,
});
// Force using the d3 browser build instead of the node build
formsBrowserify.require('./node_modules/d3/build/d3.js', {expose: 'd3'});
if(!config.DEBUG) {
formsBrowserify.transform(
babelify.configure({
presets: [
['env', {
targets: {
browsers: ['last 2 versions']
},
useBuiltins: true,
}]
]
})
);
}
formsBrowserify.transform(ngHtml2Js({
module: 'Forms.Templates',
requireAngular: true,
baseDir: './client/',
}))
if(config.watch) {
clientBrowserify = watchify(clientBrowserify);
clientBrowserify.on('update', bundleClient);
clientBrowserify.on('log', obj => console.log(obj));
formsBrowserify = watchify(formsBrowserify);
formsBrowserify.on('update', bundleForms);
formsBrowserify.on('log', obj => console.log(obj));
}
function bundleClient() {
return clientBrowserify
.bundle()
.on('error', err => console.error(err))
// Pretend the browserify bundle is a file js/bundle.js
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(debug({title: 'pre-sourcemaps'}))
.pipe(sourcemaps.init({loadMaps: true}))
// Transform the pipeline
.pipe(config.DEBUG ? debug({title: 'Skipping uglify'}) : uglify({mangle: false}))
.pipe(debug({title: 'post-uglify'}))
.pipe(sourcemaps.write(''))
.pipe(gulp.dest(config.outDir + '/js'))
.pipe(debug({title: 'bundle end'}));
}
function bundleForms() {
return formsBrowserify
.bundle()
.on('error', err => console.error(err))
// Pretend the browserify bundle is a file js/bundle.js
.pipe(source('forms-bundle.js'))
.pipe(buffer())
.pipe(debug({title: 'pre-sourcemaps'}))
.pipe(sourcemaps.init({loadMaps: true}))
// Transform the pipeline
.pipe(config.DEBUG ? debug({title: 'Skipping uglify'}) : uglify({mangle: false}))
.pipe(debug({title: 'post-uglify'}))
.pipe(sourcemaps.write(''))
.pipe(gulp.dest(config.outDir + '/js'))
.pipe(debug({title: 'bundle end'}));
}
return es.merge([
srcWatch(['client/www/static/**'])
.pipe(gulp.dest(config.outDir + '/static')),
srcWatch([
'node_modules/bootswatch/paper/bootstrap.min.css',
'node_modules/codemirror/lib/codemirror.css',
'node_modules/codemirror/addon/hint/show-hint.css',
'node_modules/angular/angular-csp.css',
'client/www/js/app.css',
])
.pipe(concat('js/app.css'))
.pipe(gulp.dest(config.outDir)),
bundleClient(),
bundleForms(),
srcWatch('client/www/**/*.shtml')
.pipe(preprocess({context: config}))
.pipe(rename({extname: '.html'}))
.pipe(gulp.dest(config.outDir))
]).pipe(debug());
}
gulp.task('watch-dev', ['clean-output'], function() {
config.watch = true;
concat = continuousConcat;
buildApp();
});
gulp.task('build-dev', ['clean-output'], function() {
config.watch = false;
buildApp();
});
gulp.task('build-exp-prod', ['clean-output'], function() {
config.watch = false;
setExpProd();
buildApp();
});
gulp.task('build-prod', [], function() {
config.watch = false;
setProd();
buildApp();
});