forked from hypothesis/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
327 lines (274 loc) · 9.17 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* eslint-env node */
'use strict';
var path = require('path');
var batch = require('gulp-batch');
var changed = require('gulp-changed');
var commander = require('commander');
var debounce = require('lodash.debounce');
var endOfStream = require('end-of-stream');
var gulp = require('gulp');
var gulpIf = require('gulp-if');
var gulpUtil = require('gulp-util');
var postcss = require('gulp-postcss');
var postcssURL = require('postcss-url');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var through = require('through2');
var createBundle = require('./scripts/gulp/create-bundle');
var manifest = require('./scripts/gulp/manifest');
var vendorBundles = require('./scripts/gulp/vendor-bundles');
var IS_PRODUCTION_BUILD = process.env.NODE_ENV === 'production';
var SCRIPT_DIR = 'build/scripts';
var STYLE_DIR = 'build/styles';
var FONTS_DIR = 'build/fonts';
var IMAGES_DIR = 'build/images';
var TEMPLATES_DIR = 'src/sidebar/templates';
// LiveReloadServer instance for sending messages to connected
// development clients
var liveReloadServer;
// List of file paths that changed since the last live-reload
// notification was dispatched
var liveReloadChangedFiles = [];
function parseCommandLine() {
commander
// Test configuration.
// See https://github.com/karma-runner/karma-mocha#configuration
.option('--grep [pattern]', 'Run only tests matching a given pattern')
.parse(process.argv);
if (commander.grep) {
gulpUtil.log(`Running tests matching pattern /${commander.grep}/`);
}
return {
grep: commander.grep,
};
}
var taskArgs = parseCommandLine();
function isSASSFile(file) {
return file.path.match(/\.scss$/);
}
function getEnv(key) {
if (!process.env.hasOwnProperty(key)) {
throw new Error(`Environment variable ${key} is not set`);
}
return process.env[key];
}
/** A list of all modules included in vendor bundles. */
var vendorModules = Object.keys(vendorBundles.bundles)
.reduce(function (deps, key) {
return deps.concat(vendorBundles.bundles[key]);
}, []);
// Builds the bundles containing vendor JS code
gulp.task('build-vendor-js', function () {
var finished = [];
Object.keys(vendorBundles.bundles).forEach(function (name) {
finished.push(createBundle({
name: name,
require: vendorBundles.bundles[name],
minify: IS_PRODUCTION_BUILD,
path: SCRIPT_DIR,
noParse: vendorBundles.noParseModules,
}));
});
return Promise.all(finished);
});
var appBundleBaseConfig = {
path: SCRIPT_DIR,
external: vendorModules,
minify: IS_PRODUCTION_BUILD,
noParse: vendorBundles.noParseModules,
};
var appBundles = [{
// The sidebar application for displaying and editing annotations.
name: 'app',
transforms: ['coffee'],
entry: './src/sidebar/app',
},{
// The annotation layer which handles displaying highlights, presenting
// annotation tools on the page and instantiating the sidebar application.
name: 'injector',
entry: './src/annotator/main',
transforms: ['coffee'],
}];
var appBundleConfigs = appBundles.map(function (config) {
return Object.assign({}, appBundleBaseConfig, config);
});
gulp.task('build-js', ['build-vendor-js'], function () {
return Promise.all(appBundleConfigs.map(function (config) {
return createBundle(config);
}));
});
gulp.task('watch-js', ['build-vendor-js'], function () {
appBundleConfigs.forEach(function (config) {
createBundle(config, {watch: true});
});
});
var styleFiles = [
// H
'./src/styles/annotator/inject.scss',
'./src/styles/annotator/pdfjs-overrides.scss',
'./src/styles/app.scss',
// Vendor
'./src/styles/vendor/angular-csp.css',
'./src/styles/vendor/icomoon.css',
'./src/styles/vendor/katex.min.css',
'./node_modules/angular-toastr/dist/angular-toastr.css',
];
gulp.task('build-css', function () {
// Rewrite font URLs to look for fonts in 'build/fonts' instead of
// 'build/styles/fonts'
function rewriteCSSURL(url) {
return url.replace(/^fonts\//, '../fonts/');
}
var sassOpts = {
outputStyle: IS_PRODUCTION_BUILD ? 'compressed' : 'nested',
};
var cssURLRewriter = postcssURL({
url: rewriteCSSURL,
});
return gulp.src(styleFiles)
.pipe(sourcemaps.init())
.pipe(gulpIf(isSASSFile, sass(sassOpts).on('error', sass.logError)))
.pipe(postcss([require('autoprefixer'), cssURLRewriter]))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(STYLE_DIR));
});
gulp.task('watch-css', ['build-css'], function () {
var vendorCSS = styleFiles.filter(function (path) {
return path.endsWith('.css');
});
var styleFileGlobs = vendorCSS.concat('./src/styles/**/*.scss');
gulp.watch(styleFileGlobs, ['build-css']);
});
var fontFiles = 'src/styles/vendor/fonts/*.woff';
gulp.task('build-fonts', function () {
gulp.src(fontFiles)
.pipe(changed(FONTS_DIR))
.pipe(gulp.dest(FONTS_DIR));
});
gulp.task('watch-fonts', ['build-fonts'], function () {
gulp.watch(fontFiles, ['build-fonts']);
});
var imageFiles = 'src/images/**/*';
gulp.task('build-images', function () {
gulp.src(imageFiles)
.pipe(changed(IMAGES_DIR))
.pipe(gulp.dest(IMAGES_DIR));
});
gulp.task('watch-images', ['build-images'], function () {
gulp.watch(imageFiles, ['build-images']);
});
gulp.task('watch-templates', function () {
gulp.watch(TEMPLATES_DIR + '/*.html', function (file) {
liveReloadServer.notifyChanged([file.path]);
});
});
var MANIFEST_SOURCE_FILES = 'build/@(fonts|images|scripts|styles)/*.@(js|css|woff|jpg|png|svg)';
var prevManifest = {};
/**
* Return an array of asset paths that changed between
* two versions of a manifest.
*/
function changedAssets(prevManifest, newManifest) {
return Object.keys(newManifest).filter(function (asset) {
return newManifest[asset] !== prevManifest[asset];
});
}
var debouncedLiveReload = debounce(function () {
// Notify dev clients about the changed assets. Note: This currently has an
// issue that if CSS, JS and templates are all changed in quick succession,
// some of the assets might be empty/incomplete files that are still being
// generated when this is invoked, causing the reload to fail.
//
// Live reload notifications are debounced to reduce the likelihood of this
// happening.
liveReloadServer.notifyChanged(liveReloadChangedFiles);
liveReloadChangedFiles = [];
}, 250);
function triggerLiveReload(changedFiles) {
if (!liveReloadServer) {
return;
}
liveReloadChangedFiles = liveReloadChangedFiles.concat(changedFiles);
debouncedLiveReload();
}
/**
* Generate a JSON manifest mapping file paths to
* URLs containing cache-busting query string parameters.
*/
function generateManifest() {
gulp.src(MANIFEST_SOURCE_FILES)
.pipe(manifest({name: 'manifest.json'}))
.pipe(through.obj(function (file, enc, callback) {
gulpUtil.log('Updated asset manifest');
var newManifest = JSON.parse(file.contents.toString());
var changed = changedAssets(prevManifest, newManifest);
prevManifest = newManifest;
triggerLiveReload(changed);
this.push(file);
callback();
}))
.pipe(gulp.dest('build/'));
}
gulp.task('watch-manifest', function () {
gulp.watch(MANIFEST_SOURCE_FILES, batch(function (events, done) {
endOfStream(generateManifest(), function () {
done();
});
}));
});
gulp.task('start-live-reload-server', function () {
var LiveReloadServer = require('./scripts/gulp/live-reload-server');
liveReloadServer = new LiveReloadServer(3000, 'http://localhost:5000');
});
gulp.task('build',
['build-js',
'build-css',
'build-fonts',
'build-images'],
generateManifest);
gulp.task('watch',
['start-live-reload-server',
'watch-js',
'watch-css',
'watch-fonts',
'watch-images',
'watch-manifest',
'watch-templates']);
function runKarma(baseConfig, opts, done) {
// See https://github.com/karma-runner/karma-mocha#configuration
var cliOpts = {
client: {
mocha: {
grep: taskArgs.grep,
},
},
};
// Work around a bug in Karma 1.10 which causes console log messages not to
// be displayed when using a non-default reporter.
// See https://github.com/karma-runner/karma/pull/2220
var BaseReporter = require('karma/lib/reporters/base');
BaseReporter.decoratorFactory.$inject =
BaseReporter.decoratorFactory.$inject.map(dep =>
dep.replace('browserLogOptions', 'browserConsoleLogOptions'));
var karma = require('karma');
new karma.Server(Object.assign({}, {
configFile: path.resolve(__dirname, baseConfig),
}, cliOpts, opts), done).start();
}
gulp.task('test', function (callback) {
runKarma('./src/karma.config.js', {singleRun:true}, callback);
});
gulp.task('test-watch', function (callback) {
runKarma('./src/karma.config.js', {}, callback);
});
gulp.task('upload-sourcemaps', ['build-js'], function () {
var uploadToSentry = require('./scripts/gulp/upload-to-sentry');
var opts = {
key: getEnv('SENTRY_API_KEY'),
organization: getEnv('SENTRY_ORGANIZATION'),
};
var projects = getEnv('SENTRY_PROJECTS').split(',');
var release = getEnv('SENTRY_RELEASE_VERSION');
return gulp.src(['build/scripts/*.js', 'build/scripts/*.map'])
.pipe(uploadToSentry(opts, projects, release));
});