forked from marsjaninzmarsa/polyfill-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
karma-polyfill-library-plugin.js
62 lines (49 loc) · 1.65 KB
/
karma-polyfill-library-plugin.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
"use strict";
const path = require('path');
const createPattern = function (path) {
return {pattern: path, included: true, served: true, watched: false};
};
const fileForMiddlewareToOverride = '/lib/index.js';
const initPolyfillLibraryFile = function (files) {
files.unshift(createPattern(path.join(__dirname, fileForMiddlewareToOverride)));
};
function isAPolyfillRequest(request) {
return request.url.startsWith(`/base${fileForMiddlewareToOverride}`);
}
function initPolyfillLibraryMiddleware(config) {
return async function polyfillBundleMiddleWare(request, response, next) {
if (!isAPolyfillRequest(request)) {
next();
} else {
await respondWithPolyfillBundle(config, request, response);
}
};
}
function createPolyfillLibraryConfigFor(features) {
return features.split(',').reduce((config, feature) => {
return Object.assign(config, {
[feature]: {
flags: new Set()
}
});
}, {});
}
async function respondWithPolyfillBundle(config, request, response) {
const polyfillLibrary = require("./lib/index.js");
const params = {
features: createPolyfillLibraryConfigFor(config.features),
minify: false,
stream: false,
uaString: request.headers["user-agent"]
};
const bundle = await polyfillLibrary.getPolyfillString(params);
response.setHeader("Content-Type", "application/javascript; charset=utf-8");
response.writeHead(200);
response.end(bundle);
}
initPolyfillLibraryFile.$inject = ['config.files'];
initPolyfillLibraryMiddleware.$inject = ['config'];
module.exports = {
'framework:polyfill-library': ['factory', initPolyfillLibraryFile],
'middleware:polyfill-library': ['factory', initPolyfillLibraryMiddleware]
};