From e5c59359c4ccbae31756db4c6f45f2a56cbe2a47 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 3 Apr 2019 16:12:31 +0100 Subject: [PATCH] Prevent bundling of Node polyfills when importing TestUtils/TestRenderer (#15305) --- packages/shared/enqueueTask.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/shared/enqueueTask.js b/packages/shared/enqueueTask.js index 02f4ee220672a..c2cfde1f28025 100644 --- a/packages/shared/enqueueTask.js +++ b/packages/shared/enqueueTask.js @@ -12,10 +12,13 @@ import warningWithoutStack from './warningWithoutStack'; let didWarnAboutMessageChannel = false; let enqueueTask; try { + // read require off the module object to get around the bundlers. + // we don't want them to detect a require and bundle a Node polyfill. + let requireString = ('require' + Math.random()).slice(0, 7); + let nodeRequire = module && module[requireString]; // assuming we're in node, let's try to get node's - // version of setImmediate, bypassing fake timers if any - let r = require; // trick packagers not to bundle this stuff. - enqueueTask = r('timers').setImmediate; + // version of setImmediate, bypassing fake timers if any. + enqueueTask = nodeRequire('timers').setImmediate; } catch (_err) { // we're in a browser // we can't use regular timers because they may still be faked