Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Babel: use for...of transform for client scripts #2783

Merged
merged 2 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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