diff --git a/static/electron-stubs.notjs b/static/electron-stubs.notjs new file mode 100644 index 000000000..57d46bbf3 --- /dev/null +++ b/static/electron-stubs.notjs @@ -0,0 +1,12 @@ +{ + remote: { + process: { + env: {}, + }, + getCurrentWindow: function() { + return { + isFocused: function() {return true;} + }; + }, + }, +} diff --git a/static/transforms/electron-stubs.js b/static/transforms/electron-stubs.js new file mode 100644 index 000000000..d7d3c5bbf --- /dev/null +++ b/static/transforms/electron-stubs.js @@ -0,0 +1,33 @@ +/** + * 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 + */ + +var babylon = require('@babel/parser'); +var fs = require('fs'); + +var electronStubs = babylon.parseExpression( + fs.readFileSync('static/electron-stubs.notjs').toString(), +); + +module.exports = function(babel) { + return { + name: 'replace-electron-requires-with-stubs', + visitor: { + CallExpression(path) { + if ( + path.node.type === 'CallExpression' && + path.node.callee.type === 'Identifier' && + path.node.callee.name === 'require' && + path.node.arguments.length > 0 + ) { + if (path.node.arguments[0].value === 'electron') { + path.replaceWith(electronStubs); + } + } + }, + }, + }; +}; diff --git a/static/transforms/index.js b/static/transforms/index.js index 90a41a4fb..be2d0f04a 100644 --- a/static/transforms/index.js +++ b/static/transforms/index.js @@ -37,7 +37,11 @@ function transform({filename, options, src}) { require('./dynamic-requires.js'), ]; - if (!options.isTestRunner) { + if (options.isTestRunner) { + if (process.env.USE_ELECTRON_STUBS) { + plugins.push(require('./electron-stubs.js')); + } + } else { // Replacing require statements with electronRequire to prevent metro from // resolving them. electronRequire are resolved during runtime by electron. // As the tests are not bundled by metro and run in @jest-runner/electron,