[I18n] Inject Intl Polyfill to PhantomJS (#25465)

* [I18n] Inject Intl Polyfill to PhantomJS

* Refactor injection code

* Move intl to "dependencies"

* Move 'intl' to the root 'package.json'

* Fix polyfills paths

* Move intl to x-pack package.json
This commit is contained in:
Leanid Shutau 2018-11-16 11:06:26 +03:00 committed by GitHub
parent 6df775c3ba
commit e44609753f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 29 deletions

View file

@ -178,6 +178,7 @@
"humps": "2.0.1",
"icalendar": "0.7.1",
"inline-style": "^2.0.0",
"intl": "^1.2.5",
"isomorphic-fetch": "2.2.1",
"joi": "^13.5.2",
"jquery": "^3.3.1",

View file

@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/
import path from 'path';
import { randomBytes } from 'crypto';
import { fromCallback } from 'bluebird';
import { transformFn } from './transform_fn';
@ -125,7 +124,24 @@ export function PhantomDriver({ page, browser, zoom, logger }) {
randomBytes(6).toString('base64'),
].join('-');
return _injectPromise(page)
const intlPath = require.resolve('intl/dist/Intl.min.js');
const promisePath = require.resolve('bluebird/js/browser/bluebird.js');
return injectPolyfill(
page,
intlPath,
function hasIntl() {
return (window.Intl !== undefined);
}
)
.then(() =>
injectPolyfill(
page,
promisePath,
function hasPromise() {
return (window.Promise !== undefined);
}
))
.then(() => {
return fromCallback(cb => {
page.evaluate(transformFn(evaluateWrapper), transformFn(fn).toString(), uniqId, args, cb);
@ -315,35 +331,26 @@ export function PhantomDriver({ page, browser, zoom, logger }) {
};
}
async function injectPolyfill(page, pathToPolyfillFile, checkFunction) {
const hasPolyfill = await fromCallback(cb => {
page.evaluate(checkFunction, cb);
});
function _injectPromise(page) {
function checkForPromise() {
return fromCallback(cb => {
page.evaluate(function hasPromise() {
return (typeof window.Promise !== 'undefined');
}, cb);
});
if (hasPolyfill) {
return;
}
return checkForPromise()
.then(hasPromise => {
if (hasPromise) return;
const status = await fromCallback(cb => page.injectJs(pathToPolyfillFile, cb));
const nodeModules = path.resolve(__dirname, '..', '..', '..', '..', '..', '..', 'node_modules');
const promisePath = path.join(nodeModules, 'bluebird', 'js', 'browser', 'bluebird.js');
return fromCallback(cb => page.injectJs(promisePath, cb))
.then(status => {
if (status !== true) {
return Promise.reject('Failed to load Promise library');
}
})
.then(checkForPromise)
.then(hasPromiseLoaded => {
if (hasPromiseLoaded !== true) {
return Promise.reject('Failed to inject Promise');
}
});
});
if (!status) {
return Promise.reject(`Failed to load ${pathToPolyfillFile} library`);
}
const hasPolyfillLoaded = await fromCallback(cb => {
page.evaluate(checkFunction, cb);
});
if (!hasPolyfillLoaded) {
return Promise.reject(`Failed to inject ${pathToPolyfillFile}`);
}
}

View file

@ -11239,6 +11239,11 @@ intl-relativeformat@^2.1.0:
dependencies:
intl-messageformat "^2.0.0"
intl@^1.2.5:
version "1.2.5"
resolved "https://registry.yarnpkg.com/intl/-/intl-1.2.5.tgz#82244a2190c4e419f8371f5aa34daa3420e2abde"
integrity sha1-giRKIZDE5Bn4Nx9ao02qNCDiq94=
into-stream@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/into-stream/-/into-stream-3.1.0.tgz#96fb0a936c12babd6ff1752a17d05616abd094c6"