84 lines
13 KiB
JavaScript
84 lines
13 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const testcafe_hammerhead_1 = __importDefault(require("testcafe-hammerhead"));
|
||
|
const asyncToGenerator_1 = __importDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
||
|
const lodash_1 = require("lodash");
|
||
|
const load_libs_1 = __importDefault(require("./babel/load-libs"));
|
||
|
const runtime_1 = require("../errors/runtime");
|
||
|
const types_1 = require("../errors/types");
|
||
|
const format_babel_produced_code_1 = __importDefault(require("./babel/format-babel-produced-code"));
|
||
|
const get_base_babel_options_1 = __importDefault(require("./babel/get-base-babel-options"));
|
||
|
const ANONYMOUS_FN_RE = /^function\s*\*?\s*\(/;
|
||
|
const ES6_OBJ_METHOD_NAME_RE = /^(\S+?)\s*\(/;
|
||
|
const USE_STRICT_RE = /^('|")use strict('|");?/;
|
||
|
const TRAILING_SEMICOLON_RE = /;\s*$/;
|
||
|
const REGENERATOR_FOOTPRINTS_RE = /(_index\d+\.default|_regenerator\d+\.default|regeneratorRuntime)(\(\))?\.wrap\(function func\$\(_context\)/;
|
||
|
const ASYNC_TO_GENERATOR_OUTPUT_CODE = (0, format_babel_produced_code_1.default)((0, asyncToGenerator_1.default)(lodash_1.noop).toString());
|
||
|
const CLIENT_FUNCTION_BODY_WRAPPER = code => `const func = (${code});`;
|
||
|
const CLIENT_FUNCTION_WRAPPER = ({ code, dependencies }) => `(function(){${dependencies} ${code} return func;})();`;
|
||
|
let loadedBabelOptions = null;
|
||
|
function getBabelOptions() {
|
||
|
const { presetEnvForClientFunction, transformForOfAsArray } = (0, load_libs_1.default)();
|
||
|
return Object.assign({}, get_base_babel_options_1.default, {
|
||
|
presets: [{ plugins: [transformForOfAsArray] }, presetEnvForClientFunction],
|
||
|
});
|
||
|
}
|
||
|
function ensureLoadedBabelOptions() {
|
||
|
if (!loadedBabelOptions) {
|
||
|
const { babel } = (0, load_libs_1.default)();
|
||
|
const opts = getBabelOptions();
|
||
|
loadedBabelOptions = babel.loadOptions(opts);
|
||
|
}
|
||
|
return loadedBabelOptions;
|
||
|
}
|
||
|
function downgradeES(fnCode) {
|
||
|
const { babel } = (0, load_libs_1.default)();
|
||
|
const opts = ensureLoadedBabelOptions();
|
||
|
const compiled = babel.transform(fnCode, opts);
|
||
|
return compiled.code
|
||
|
.replace(USE_STRICT_RE, '')
|
||
|
.trim();
|
||
|
}
|
||
|
function getDependenciesDefinition(dependencies) {
|
||
|
return Object
|
||
|
.keys(dependencies)
|
||
|
.reduce((code, name) => {
|
||
|
return code + `var ${name}=__dependencies$['${name}'];`;
|
||
|
}, '');
|
||
|
}
|
||
|
function makeFnCodeSuitableForParsing(fnCode) {
|
||
|
// NOTE: 'function() {}' -> '(function() {})'
|
||
|
if (ANONYMOUS_FN_RE.test(fnCode))
|
||
|
return `(${fnCode})`;
|
||
|
// NOTE: 'myFn () {}' -> 'function myFn() {}'
|
||
|
const match = fnCode.match(ES6_OBJ_METHOD_NAME_RE);
|
||
|
if (match && match[1] !== 'function')
|
||
|
return `function ${fnCode}`;
|
||
|
return fnCode;
|
||
|
}
|
||
|
function containsAsyncToGeneratorOutputCode(code) {
|
||
|
const formattedCode = (0, format_babel_produced_code_1.default)(code);
|
||
|
return formattedCode.includes(ASYNC_TO_GENERATOR_OUTPUT_CODE);
|
||
|
}
|
||
|
function compileClientFunction(fnCode, dependencies, instantiationCallsiteName, compilationCallsiteName) {
|
||
|
if (containsAsyncToGeneratorOutputCode(fnCode))
|
||
|
throw new runtime_1.ClientFunctionAPIError(compilationCallsiteName, instantiationCallsiteName, types_1.RUNTIME_ERRORS.regeneratorInClientFunctionCode);
|
||
|
fnCode = makeFnCodeSuitableForParsing(fnCode);
|
||
|
fnCode = CLIENT_FUNCTION_BODY_WRAPPER(fnCode);
|
||
|
// NOTE: we need to recompile ES6 code for the browser if we are on newer versions of Node.
|
||
|
fnCode = downgradeES(fnCode);
|
||
|
fnCode = testcafe_hammerhead_1.default.processScript(fnCode, false);
|
||
|
// NOTE: check compiled code for regenerator injection
|
||
|
if (REGENERATOR_FOOTPRINTS_RE.test(fnCode))
|
||
|
throw new runtime_1.ClientFunctionAPIError(compilationCallsiteName, instantiationCallsiteName, types_1.RUNTIME_ERRORS.regeneratorInClientFunctionCode);
|
||
|
if (!TRAILING_SEMICOLON_RE.test(fnCode))
|
||
|
fnCode += ';';
|
||
|
const dependenciesDefinition = dependencies ? getDependenciesDefinition(dependencies) : '';
|
||
|
return CLIENT_FUNCTION_WRAPPER({ code: fnCode, dependencies: dependenciesDefinition });
|
||
|
}
|
||
|
exports.default = compileClientFunction;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29tcGlsZS1jbGllbnQtZnVuY3Rpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvY29tcGlsZXIvY29tcGlsZS1jbGllbnQtZnVuY3Rpb24uanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFBQSw4RUFBNkM7QUFDN0MsK0ZBQXVFO0FBQ3ZFLG1DQUE4QjtBQUM5QixrRUFBOEM7QUFDOUMsK0NBQTJEO0FBQzNELDJDQUFpRDtBQUNqRCxvR0FBeUU7QUFDekUsNEZBQWdFO0FBRWhFLE1BQU0sZUFBZSxHQUFrQixzQkFBc0IsQ0FBQztBQUM5RCxNQUFNLHNCQUFzQixHQUFXLGNBQWMsQ0FBQztBQUN0RCxNQUFNLGFBQWEsR0FBb0IseUJBQXlCLENBQUM7QUFDakUsTUFBTSxxQkFBcUIsR0FBWSxPQUFPLENBQUM7QUFDL0MsTUFBTSx5QkFBeUIsR0FBUSw0R0FBNEcsQ0FBQztBQUNwSixNQUFNLDhCQUE4QixHQUFHLElBQUE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