From 99527fdb494c464c9351626548bf446b39c155f4 Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Mon, 16 Nov 2020 13:11:15 +0100 Subject: [PATCH] Made polyfill route more flexible --- out/polyfill-worker.js | 4 ++-- out/polyfill.d.ts | 2 +- out/polyfill.js | 26 +++++++++++++------------- src/polyfill-worker.ts | 4 ++-- src/polyfill.ts | 30 ++++++++++++++---------------- 5 files changed, 32 insertions(+), 34 deletions(-) diff --git a/out/polyfill-worker.js b/out/polyfill-worker.js index ab2ec1b..1e8ca61 100644 --- a/out/polyfill-worker.js +++ b/out/polyfill-worker.js @@ -5,14 +5,14 @@ const worker_1 = require("threads/worker"); const fs = require("fs"); const polyfill_analyzer_1 = require("@10xjs/polyfill-analyzer"); const polyfills_1 = require("@10xjs/polyfill-analyzer/dist/polyfills"); -worker_1.expose(() => { +worker_1.expose((fileToWatch) => { const exclude = [ "console.markTimeline", "console.timeline", "console.timelineEnd", ]; 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)), // Not all features listed by polyfillLibrary.listAllPolyfills()` can be detected. unsupportedPolyfill: 'ignore', diff --git a/out/polyfill.d.ts b/out/polyfill.d.ts index cb107c7..03c5851 100644 --- a/out/polyfill.d.ts +++ b/out/polyfill.d.ts @@ -12,7 +12,7 @@ export declare type PolyfillOptions = { uaString: string; rum: boolean; }; -declare function getRouter(opts?: Partial): RequestHandler; +declare function getRouter(fileToWatch: string, opts?: Partial): RequestHandler; export declare const Polyfill: { getRouter: typeof getRouter; }; diff --git a/out/polyfill.js b/out/polyfill.js index e1db6ba..c0585ec 100644 --- a/out/polyfill.js +++ b/out/polyfill.js @@ -14,19 +14,19 @@ const polyfillLibrary = require("polyfill-library"); const _1 = require("."); const threads_1 = require("threads"); const features = new _1.WaitForSync(); -(() => __awaiter(void 0, void 0, void 0, function* () { - const worker = yield threads_1.spawn(new threads_1.Worker("./polyfill-worker")); - const feats = yield worker(); - yield threads_1.Thread.terminate(worker); - return feats; -}))() - .then(feats => { - feats["fetch"] = {}; - _1.Logger.debug("Polyfill analysed:", Object.keys(feats)); - features.setData(feats); -}) - .catch(err => features.setError(err)); -function getRouter(opts) { +function getRouter(fileToWatch, opts) { + (() => __awaiter(this, void 0, void 0, function* () { + const worker = yield threads_1.spawn(new threads_1.Worker("./polyfill-worker")); + const feats = yield worker(fileToWatch); + yield threads_1.Thread.terminate(worker); + return feats; + }))() + .then(feats => { + feats["fetch"] = {}; + _1.Logger.debug("Polyfill analysed:", Object.keys(feats)); + features.setData(feats); + }) + .catch(err => features.setError(err)); const options = Object.assign({ minify: _1.DefaultConfig.isProduction, unknown: "polyfill" }, opts); 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 })); diff --git a/src/polyfill-worker.ts b/src/polyfill-worker.ts index 838a823..61801ab 100644 --- a/src/polyfill-worker.ts +++ b/src/polyfill-worker.ts @@ -5,14 +5,14 @@ import {analyze} from '@10xjs/polyfill-analyzer'; import allPolyfills from '@10xjs/polyfill-analyzer/dist/polyfills'; import {PolyfillFeatureList} from 'polyfill-library'; -expose(() => { +expose((fileToWatch) => { const exclude = [ "console.markTimeline", "console.timeline", "console.timelineEnd", ]; 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)), // Not all features listed by polyfillLibrary.listAllPolyfills()` can be detected. unsupportedPolyfill: 'ignore', diff --git a/src/polyfill.ts b/src/polyfill.ts index a157bc7..3d27418 100644 --- a/src/polyfill.ts +++ b/src/polyfill.ts @@ -19,23 +19,21 @@ export type PolyfillOptions = { rum: boolean, } -const features = new WaitForSync(); +function getRouter(fileToWatch: string, opts?: Partial): RequestHandler { + const features = new WaitForSync(); + (async () => { + const worker = await spawn(new Worker("./polyfill-worker")); + const feats = await worker(fileToWatch) as PolyfillFeatureList; + await Thread.terminate(worker); + return feats; + })() + .then(feats => { + feats["fetch"] = {}; + Logger.debug("Polyfill analysed:", Object.keys(feats)); + features.setData(feats); + }) + .catch(err => features.setError(err)); -(async () => { - const worker = await spawn(new Worker("./polyfill-worker")); - const feats = await worker() as PolyfillFeatureList; - await Thread.terminate(worker); - return feats; -})() - .then(feats => { - feats["fetch"] = {}; - Logger.debug("Polyfill analysed:", Object.keys(feats)); - features.setData(feats); - }) - .catch(err => features.setError(err)); - - -function getRouter(opts?: Partial): RequestHandler { const options: Partial = { minify: DefaultConfig.isProduction, unknown: "polyfill",