Skip to content

Commit

Permalink
Babel: use for...of transform for client scripts (#2783)
Browse files Browse the repository at this point in the history
  • Loading branch information
Farfurix authored and AndreyBelym committed Aug 31, 2018
1 parent 279422f commit cf5780b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"async-exit-hook": "^1.1.2",
"babel-core": "^6.22.1",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-for-of-as-array": "^1.1.1",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-preset-env": "^1.1.8",
"babel-preset-flow": "^6.23.0",
Expand Down
10 changes: 8 additions & 2 deletions src/client/.babelrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{
"compact": false,
"presets": [
["env", { "loose": true }]
["env", {
"loose": true,
"exclude": [
"transform-es2015-typeof-symbol"
]
}]
],
"plugins": [
"add-module-exports"
"add-module-exports",
"transform-for-of-as-array"
]
}
18 changes: 3 additions & 15 deletions src/compiler/compile-client-function.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import hammerhead from 'testcafe-hammerhead';
import asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import { noop, escapeRegExp as escapeRe } from 'lodash';
import { noop } from 'lodash';
import loadBabelLibs from './load-babel-libs';
import { ClientFunctionAPIError } from '../errors/runtime';
import MESSAGE from '../errors/runtime/message';
Expand Down Expand Up @@ -29,27 +29,15 @@ var babelArtifactPolyfills = {
re: /_stringify(\d+)\.default/,
getCode: match => `var _stringify${match[1]} = { default: JSON.stringify };`,
removeMatchingCode: false
},

'typeof': {
re: new RegExp(escapeRe(
'var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? ' +
'function (obj) {return typeof obj;} : ' +
'function (obj) {return obj && typeof Symbol === "function" && obj.constructor === Symbol ' +
'&& obj !== Symbol.prototype ? "symbol" : typeof obj;};'
), 'g'),

getCode: () => 'var _typeof = function(obj) { return typeof obj; };',
removeMatchingCode: true
}
};


function getBabelOptions () {
var { presetFallback } = loadBabelLibs();
var { presetFallback, transformForOfAsArray } = loadBabelLibs();

return {
presets: [presetFallback],
presets: [{ plugins: [transformForOfAsArray] }, presetFallback],
sourceMaps: false,
retainLines: true,
ast: false,
Expand Down
10 changes: 9 additions & 1 deletion src/compiler/load-babel-libs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ function getOptsForPresetEnv () {
};
}

function getOptsForPresetFallback () {
return {
loose: true,
exclude: ['transform-es2015-typeof-symbol']
};
}

// NOTE: lazy load heavy dependencies
export default function loadBabelLibs () {
return {
Expand All @@ -14,7 +21,8 @@ export default function loadBabelLibs () {
presetFlow: require('babel-preset-flow'),
transformClassProperties: require('babel-plugin-transform-class-properties'),
transformRuntime: require('babel-plugin-transform-runtime'),
presetFallback: require('babel-preset-env').default(null, { loose: true }),
transformForOfAsArray: require('babel-plugin-transform-for-of-as-array').default,
presetFallback: require('babel-preset-env').default(null, getOptsForPresetFallback()),
presetEnv: require('babel-preset-env').default(null, getOptsForPresetEnv())
};
}
4 changes: 0 additions & 4 deletions test/server/compiler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,6 @@ describe('Compiler', function () {
return testClientFnCompilation('json-stringify');
});

it('Should polyfill Babel `typeof` artifacts', function () {
return testClientFnCompilation('typeof');
});

describe('Regression', function () {
it('Should compile ES6 object method (GH-1279)', function () {
return testClientFnCompilation('gh1279');
Expand Down

0 comments on commit cf5780b

Please sign in to comment.