From 76d40e7ddffd46751609dddddace6aa9e59f9f99 Mon Sep 17 00:00:00 2001 From: John Knox Date: Mon, 22 Oct 2018 08:58:55 -0700 Subject: [PATCH] Use node's perf_hooks Summary: I tried switching Flipper to use the node.js module: https://nodejs.org/api/perf_hooks.html instead of electrons window.performance. But the module couldn't be found. So instead, I'm just replacing it in the pure node tests. This means tests without node can work without issues. Reviewed By: danielbuechele Differential Revision: D10302114 fbshipit-source-id: b3b8aa5fb350a8246fcdfc2a80cc72daec2448d7 --- .../__tests__/electron-stubs.node.js | 36 +++++++++++++++++++ static/transforms/electron-stubs.js | 12 +++++++ 2 files changed, 48 insertions(+) create mode 100644 static/transforms/__tests__/electron-stubs.node.js diff --git a/static/transforms/__tests__/electron-stubs.node.js b/static/transforms/__tests__/electron-stubs.node.js new file mode 100644 index 000000000..a1f7757e1 --- /dev/null +++ b/static/transforms/__tests__/electron-stubs.node.js @@ -0,0 +1,36 @@ +/** + * Copyright 2018-present Facebook. + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * @format + */ + +import {transform} from '@babel/core'; +import generate from '@babel/generator'; + +import electronStubs from '../electron-stubs'; + +const babelOptions = { + ast: true, + plugins: [electronStubs], + filename: 'index.js', +}; + +test('transform electron requires to inlined stubs', () => { + const src = 'require("electron")'; + const transformed = transform(src, babelOptions).ast; + const body = transformed.program.body[0]; + expect(body.type).toBe('ExpressionStatement'); + expect(body.expression.properties.map(p => p.key.name)).toContain('remote'); +}); + +test('transform performance calls to requires', () => { + for (const method of ['mark', 'measure']) { + const src = `performance.${method}('something')`; + const transformed = transform(src, babelOptions).ast; + const {code} = generate(transformed); + expect(code).toBe( + `require('perf_hooks').performance.${method}('something');`, + ); + } +}); diff --git a/static/transforms/electron-stubs.js b/static/transforms/electron-stubs.js index d7d3c5bbf..9c2019766 100644 --- a/static/transforms/electron-stubs.js +++ b/static/transforms/electron-stubs.js @@ -11,6 +11,7 @@ var fs = require('fs'); var electronStubs = babylon.parseExpression( fs.readFileSync('static/electron-stubs.notjs').toString(), ); +var perfHooks = babylon.parseExpression("require('perf_hooks').performance"); module.exports = function(babel) { return { @@ -27,6 +28,17 @@ module.exports = function(babel) { path.replaceWith(electronStubs); } } + if ( + path.node.type === 'CallExpression' && + path.node.callee.type === 'MemberExpression' && + path.node.callee.object.name === 'performance' + ) { + // 'perf_hooks' was added in Node 8.5.0 but doesn't appear to be + // present in electron. We can remove this and switch to using + // interval when it is. Until then, continue using browser.performance + // for real and swap in node's perf_hooks when we dont have electron. + path.node.callee.object = perfHooks; + } }, }, };