forked from hypothesis/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
342 lines (297 loc) · 9.25 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* eslint-env node */
'use strict';
const { mkdirSync, readdirSync } = require('fs');
const path = require('path');
const changed = require('gulp-changed');
const commander = require('commander');
const log = require('fancy-log');
const gulp = require('gulp');
const replace = require('gulp-replace');
const rename = require('gulp-rename');
const through = require('through2');
const createBundle = require('./scripts/gulp/create-bundle');
const createStyleBundle = require('./scripts/gulp/create-style-bundle');
const manifest = require('./scripts/gulp/manifest');
const serveDev = require('./dev-server/serve-dev');
const servePackage = require('./dev-server/serve-package');
const vendorBundles = require('./scripts/gulp/vendor-bundles');
const IS_PRODUCTION_BUILD = process.env.NODE_ENV === 'production';
const SCRIPT_DIR = 'build/scripts';
const STYLE_DIR = 'build/styles';
const FONTS_DIR = 'build/fonts';
const IMAGES_DIR = 'build/images';
function parseCommandLine() {
commander
.option(
'--grep [pattern]',
'Run only tests where filename matches a pattern'
)
.parse(process.argv);
return {
grep: commander.grep,
};
}
const taskArgs = parseCommandLine();
/** A list of all modules included in vendor bundles. */
const 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 () {
const 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);
});
const appBundleBaseConfig = {
path: SCRIPT_DIR,
external: vendorModules,
minify: IS_PRODUCTION_BUILD,
noParse: vendorBundles.noParseModules,
};
const appBundles = [
{
// The entry point for both the Hypothesis client and the sidebar
// application. This is responsible for loading the rest of the assets needed
// by the client.
name: 'boot',
entry: './src/boot/index',
transforms: ['babel'],
},
{
// The sidebar application for displaying and editing annotations.
name: 'sidebar',
transforms: ['babel'],
entry: './src/sidebar/index',
},
{
// The annotation layer which handles displaying highlights, presenting
// annotation tools on the page and instantiating the sidebar application.
name: 'annotator',
entry: './src/annotator/index',
transforms: ['babel'],
},
];
// Polyfill bundles. Polyfills are grouped into "sets" (one bundle per set)
// based on major ECMAScript version or DOM API. Some large polyfills
// (eg. for String.prototype.normalize) are additionally separated out into
// their own bundles.
//
// To add a new polyfill:
// - Add the relevant dependencies to the project
// - Create an entry point in `src/boot/polyfills/{set}` and a feature
// detection function in `src/boot/polyfills/index.js`
// - Add the polyfill set name to the required dependencies for the parts of
// the client that need it in `src/boot/boot.js`
const polyfillBundles = readdirSync('./src/boot/polyfills/')
.filter(name => name.endsWith('.js') && name !== 'index.js')
.map(name => name.replace(/\.js$/, ''))
.map(set => ({
name: `polyfills-${set}`,
entry: `./src/boot/polyfills/${set}`,
transforms: ['babel'],
}));
const appBundleConfigs = appBundles.concat(polyfillBundles).map(config => {
return Object.assign({}, appBundleBaseConfig, config);
});
gulp.task(
'build-js',
gulp.parallel('build-vendor-js', function () {
return Promise.all(
appBundleConfigs.map(function (config) {
return createBundle(config);
})
);
})
);
gulp.task(
'watch-js',
gulp.series('build-vendor-js', function watchJS() {
appBundleConfigs.forEach(function (config) {
createBundle(config, { watch: true });
});
})
);
const cssBundles = [
// H
'./src/styles/annotator/annotator.scss',
'./src/styles/annotator/pdfjs-overrides.scss',
'./src/styles/sidebar/sidebar.scss',
// Vendor
'./node_modules/katex/dist/katex.min.css',
];
gulp.task('build-css', function () {
mkdirSync(STYLE_DIR, { recursive: true });
const bundles = cssBundles.map(entry =>
createStyleBundle({
input: entry,
output: `${STYLE_DIR}/${path.basename(entry, path.extname(entry))}.css`,
minify: IS_PRODUCTION_BUILD,
})
);
return Promise.all(bundles);
});
gulp.task(
'watch-css',
gulp.series('build-css', function watchCSS() {
const vendorCSS = cssBundles.filter(path => path.endsWith('.css'));
const styleFileGlobs = vendorCSS.concat('./src/styles/**/*.scss');
gulp.watch(styleFileGlobs, gulp.task('build-css'));
})
);
const fontFiles = [
'src/styles/vendor/fonts/*.woff',
'node_modules/katex/dist/fonts/*.woff',
'node_modules/katex/dist/fonts/*.woff2',
];
gulp.task('build-fonts', function () {
return gulp
.src(fontFiles)
.pipe(changed(FONTS_DIR))
.pipe(gulp.dest(FONTS_DIR));
});
gulp.task(
'watch-fonts',
gulp.series('build-fonts', function watchFonts() {
gulp.watch(fontFiles, gulp.task('build-fonts'));
})
);
const imageFiles = 'src/images/**/*';
gulp.task('build-images', function () {
return gulp
.src(imageFiles)
.pipe(changed(IMAGES_DIR))
.pipe(gulp.dest(IMAGES_DIR));
});
gulp.task(
'watch-images',
gulp.series('build-images', function watchImages() {
gulp.watch(imageFiles, gulp.task('build-images'));
})
);
const MANIFEST_SOURCE_FILES =
'build/@(fonts|images|scripts|styles)/*.@(js|css|woff|jpg|png|svg)';
let isFirstBuild = true;
/**
* Generates the `build/boot.js` script which serves as the entry point for
* the Hypothesis client.
*
* @param {Object} manifest - Manifest mapping asset paths to cache-busted URLs
* @param {Object} options - Options for generating the boot script
*/
function generateBootScript(manifest, { usingDevServer = false } = {}) {
const { version } = require('./package.json');
const defaultNotebookAppUrl = process.env.NOTEBOOK_APP_URL
? `${process.env.NOTEBOOK_APP_URL}`
: '{current_scheme}://{current_host}:5000/notebook';
const defaultSidebarAppUrl = process.env.SIDEBAR_APP_URL
? `${process.env.SIDEBAR_APP_URL}`
: '{current_scheme}://{current_host}:5000/app.html';
let defaultAssetRoot;
if (process.env.NODE_ENV === 'production' && !usingDevServer) {
defaultAssetRoot = 'https://cdn.hypothes.is/hypothesis';
} else {
defaultAssetRoot = '{current_scheme}://{current_host}:3001/hypothesis';
}
defaultAssetRoot = `${defaultAssetRoot}/${version}/`;
if (isFirstBuild) {
log(`Sidebar app URL: ${defaultSidebarAppUrl}`);
log(`Notebook app URL: ${defaultNotebookAppUrl}`);
log(`Client asset root URL: ${defaultAssetRoot}`);
isFirstBuild = false;
}
gulp
.src('build/scripts/boot.bundle.js')
.pipe(replace('__MANIFEST__', JSON.stringify(manifest)))
.pipe(replace('__ASSET_ROOT__', defaultAssetRoot))
.pipe(replace('__NOTEBOOK_APP_URL__', defaultNotebookAppUrl))
.pipe(replace('__SIDEBAR_APP_URL__', defaultSidebarAppUrl))
// Strip sourcemap link. It will have been invalidated by the previous
// replacements and the bundle is so small that it isn't really valuable.
.pipe(replace(/^\/\/# sourceMappingURL=\S+$/m, ''))
.pipe(rename('boot.js'))
.pipe(gulp.dest('build/'));
}
/**
* Generate a JSON manifest mapping file paths to
* URLs containing cache-busting query string parameters.
*/
function generateManifest(opts) {
return gulp
.src(MANIFEST_SOURCE_FILES)
.pipe(manifest({ name: 'manifest.json' }))
.pipe(
through.obj(function (file, enc, callback) {
const newManifest = JSON.parse(file.contents.toString());
// Expand template vars in boot script bundle
generateBootScript(newManifest, opts);
this.push(file);
callback();
})
)
.pipe(gulp.dest('build/'));
}
gulp.task('watch-manifest', function () {
gulp.watch(MANIFEST_SOURCE_FILES, { delay: 500 }, function updateManifest() {
return generateManifest({ usingDevServer: true });
});
});
gulp.task('serve-package', function () {
servePackage(3001);
});
gulp.task('serve-test-pages', function () {
serveDev(3000, { clientUrl: `//{current_host}:3001/hypothesis` });
});
const buildAssets = gulp.parallel(
'build-js',
'build-css',
'build-fonts',
'build-images'
);
gulp.task('build', gulp.series(buildAssets, generateManifest));
gulp.task(
'watch',
gulp.parallel(
'serve-package',
'serve-test-pages',
'watch-js',
'watch-css',
'watch-fonts',
'watch-images',
'watch-manifest'
)
);
function runKarma({ singleRun }, done) {
const karma = require('karma');
new karma.Server(
{
configFile: path.resolve(__dirname, './src/karma.config.js'),
grep: taskArgs.grep,
singleRun,
},
done
).start();
}
// Unit and integration testing tasks.
// Some (eg. a11y) tests rely on CSS bundles, so build these first.
gulp.task(
'test',
gulp.series('build-css', done => runKarma({ singleRun: true }, done))
);
gulp.task(
'test-watch',
gulp.series('build-css', done => runKarma({ singleRun: false }, done))
);