Made polyfill route more flexible
This commit is contained in:
@@ -5,14 +5,14 @@ const worker_1 = require("threads/worker");
|
|||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const polyfill_analyzer_1 = require("@10xjs/polyfill-analyzer");
|
const polyfill_analyzer_1 = require("@10xjs/polyfill-analyzer");
|
||||||
const polyfills_1 = require("@10xjs/polyfill-analyzer/dist/polyfills");
|
const polyfills_1 = require("@10xjs/polyfill-analyzer/dist/polyfills");
|
||||||
worker_1.expose(() => {
|
worker_1.expose((fileToWatch) => {
|
||||||
const exclude = [
|
const exclude = [
|
||||||
"console.markTimeline",
|
"console.markTimeline",
|
||||||
"console.timeline",
|
"console.timeline",
|
||||||
"console.timelineEnd",
|
"console.timelineEnd",
|
||||||
];
|
];
|
||||||
const featureList = polyfill_analyzer_1.analyze({
|
const featureList = polyfill_analyzer_1.analyze({
|
||||||
source: fs.readFileSync('./public/js/bundle.js', 'utf-8'),
|
source: fs.readFileSync(fileToWatch, 'utf-8'),
|
||||||
include: polyfills_1.default.filter(x => !exclude.includes(x)),
|
include: polyfills_1.default.filter(x => !exclude.includes(x)),
|
||||||
// Not all features listed by polyfillLibrary.listAllPolyfills()` can be detected.
|
// Not all features listed by polyfillLibrary.listAllPolyfills()` can be detected.
|
||||||
unsupportedPolyfill: 'ignore',
|
unsupportedPolyfill: 'ignore',
|
||||||
|
|||||||
2
out/polyfill.d.ts
vendored
2
out/polyfill.d.ts
vendored
@@ -12,7 +12,7 @@ export declare type PolyfillOptions = {
|
|||||||
uaString: string;
|
uaString: string;
|
||||||
rum: boolean;
|
rum: boolean;
|
||||||
};
|
};
|
||||||
declare function getRouter(opts?: Partial<PolyfillOptions>): RequestHandler;
|
declare function getRouter(fileToWatch: string, opts?: Partial<PolyfillOptions>): RequestHandler;
|
||||||
export declare const Polyfill: {
|
export declare const Polyfill: {
|
||||||
getRouter: typeof getRouter;
|
getRouter: typeof getRouter;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -14,19 +14,19 @@ const polyfillLibrary = require("polyfill-library");
|
|||||||
const _1 = require(".");
|
const _1 = require(".");
|
||||||
const threads_1 = require("threads");
|
const threads_1 = require("threads");
|
||||||
const features = new _1.WaitForSync();
|
const features = new _1.WaitForSync();
|
||||||
(() => __awaiter(void 0, void 0, void 0, function* () {
|
function getRouter(fileToWatch, opts) {
|
||||||
|
(() => __awaiter(this, void 0, void 0, function* () {
|
||||||
const worker = yield threads_1.spawn(new threads_1.Worker("./polyfill-worker"));
|
const worker = yield threads_1.spawn(new threads_1.Worker("./polyfill-worker"));
|
||||||
const feats = yield worker();
|
const feats = yield worker(fileToWatch);
|
||||||
yield threads_1.Thread.terminate(worker);
|
yield threads_1.Thread.terminate(worker);
|
||||||
return feats;
|
return feats;
|
||||||
}))()
|
}))()
|
||||||
.then(feats => {
|
.then(feats => {
|
||||||
feats["fetch"] = {};
|
feats["fetch"] = {};
|
||||||
_1.Logger.debug("Polyfill analysed:", Object.keys(feats));
|
_1.Logger.debug("Polyfill analysed:", Object.keys(feats));
|
||||||
features.setData(feats);
|
features.setData(feats);
|
||||||
})
|
})
|
||||||
.catch(err => features.setError(err));
|
.catch(err => features.setError(err));
|
||||||
function getRouter(opts) {
|
|
||||||
const options = Object.assign({ minify: _1.DefaultConfig.isProduction, unknown: "polyfill" }, opts);
|
const options = Object.assign({ minify: _1.DefaultConfig.isProduction, unknown: "polyfill" }, opts);
|
||||||
return (req, res) => __awaiter(this, void 0, void 0, function* () {
|
return (req, res) => __awaiter(this, void 0, void 0, function* () {
|
||||||
const polyfillBundle = yield polyfillLibrary.getPolyfillString(Object.assign(Object.assign({}, options), { uaString: req.header("user-agent"), features: yield features.resolve(), stream: false }));
|
const polyfillBundle = yield polyfillLibrary.getPolyfillString(Object.assign(Object.assign({}, options), { uaString: req.header("user-agent"), features: yield features.resolve(), stream: false }));
|
||||||
|
|||||||
@@ -5,14 +5,14 @@ import {analyze} from '@10xjs/polyfill-analyzer';
|
|||||||
import allPolyfills from '@10xjs/polyfill-analyzer/dist/polyfills';
|
import allPolyfills from '@10xjs/polyfill-analyzer/dist/polyfills';
|
||||||
import {PolyfillFeatureList} from 'polyfill-library';
|
import {PolyfillFeatureList} from 'polyfill-library';
|
||||||
|
|
||||||
expose(() => {
|
expose((fileToWatch) => {
|
||||||
const exclude = [
|
const exclude = [
|
||||||
"console.markTimeline",
|
"console.markTimeline",
|
||||||
"console.timeline",
|
"console.timeline",
|
||||||
"console.timelineEnd",
|
"console.timelineEnd",
|
||||||
];
|
];
|
||||||
const featureList = analyze({
|
const featureList = analyze({
|
||||||
source: fs.readFileSync('./public/js/bundle.js', 'utf-8'),
|
source: fs.readFileSync(fileToWatch, 'utf-8'),
|
||||||
include: allPolyfills.filter(x => !exclude.includes(x)),
|
include: allPolyfills.filter(x => !exclude.includes(x)),
|
||||||
// Not all features listed by polyfillLibrary.listAllPolyfills()` can be detected.
|
// Not all features listed by polyfillLibrary.listAllPolyfills()` can be detected.
|
||||||
unsupportedPolyfill: 'ignore',
|
unsupportedPolyfill: 'ignore',
|
||||||
|
|||||||
@@ -19,14 +19,14 @@ export type PolyfillOptions = {
|
|||||||
rum: boolean,
|
rum: boolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
const features = new WaitForSync<PolyfillFeatureList>();
|
function getRouter(fileToWatch: string, opts?: Partial<PolyfillOptions>): RequestHandler {
|
||||||
|
const features = new WaitForSync<PolyfillFeatureList>();
|
||||||
(async () => {
|
(async () => {
|
||||||
const worker = await spawn<WorkerFunction>(new Worker("./polyfill-worker"));
|
const worker = await spawn<WorkerFunction>(new Worker("./polyfill-worker"));
|
||||||
const feats = await worker() as PolyfillFeatureList;
|
const feats = await worker(fileToWatch) as PolyfillFeatureList;
|
||||||
await Thread.terminate(worker);
|
await Thread.terminate(worker);
|
||||||
return feats;
|
return feats;
|
||||||
})()
|
})()
|
||||||
.then(feats => {
|
.then(feats => {
|
||||||
feats["fetch"] = {};
|
feats["fetch"] = {};
|
||||||
Logger.debug("Polyfill analysed:", Object.keys(feats));
|
Logger.debug("Polyfill analysed:", Object.keys(feats));
|
||||||
@@ -34,8 +34,6 @@ const features = new WaitForSync<PolyfillFeatureList>();
|
|||||||
})
|
})
|
||||||
.catch(err => features.setError(err));
|
.catch(err => features.setError(err));
|
||||||
|
|
||||||
|
|
||||||
function getRouter(opts?: Partial<PolyfillOptions>): RequestHandler {
|
|
||||||
const options: Partial<PolyfillOptions> = {
|
const options: Partial<PolyfillOptions> = {
|
||||||
minify: DefaultConfig.isProduction,
|
minify: DefaultConfig.isProduction,
|
||||||
unknown: "polyfill",
|
unknown: "polyfill",
|
||||||
|
|||||||
Reference in New Issue
Block a user