From d3b937f990012a31b8d917e220f4ed2f0a4fd2d3 Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Tue, 2 Jun 2020 17:57:43 -0700 Subject: [PATCH] Make sure LogBox is not included in production bundles (#28984) Summary: I was doing some bundle inspection and noticed the LogBox module was included. It does amount to around 15kb so I think it is worth making sure it is not there. To fix it I moved some imports to require inside __DEV__ blocks to make sure metro is able to remove these imports. ## Changelog [General] [Fixed] - Make sure LogBox is not included in production bundles Pull Request resolved: https://github.com/facebook/react-native/pull/28984 Test Plan: Tested using react-native-bundle-visualizer and made sure nothing from LogBox was included in the bundle after these changes. Reviewed By: TheSavior Differential Revision: D21794466 Pulled By: rickhanlonii fbshipit-source-id: 6cb0c0a89633e9850019bd61478c35e9c21638dc --- Libraries/Core/ExceptionsManager.js | 4 ++-- Libraries/LogBox/LogBox.js | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Libraries/Core/ExceptionsManager.js b/Libraries/Core/ExceptionsManager.js index a151d26c5d5ff1..b1abe891f18f0b 100644 --- a/Libraries/Core/ExceptionsManager.js +++ b/Libraries/Core/ExceptionsManager.js @@ -11,7 +11,6 @@ 'use strict'; import type {ExtendedError} from './Devtools/parseErrorStack'; -import * as LogBoxData from '../LogBox/Data/LogBoxData'; import type {ExceptionData} from './NativeExceptionsManager'; class SyntheticError extends Error { @@ -104,7 +103,8 @@ function reportException( console.error(data.message); } - if (isHandledByLogBox) { + if (__DEV__ && isHandledByLogBox) { + const LogBoxData = require('../LogBox/Data/LogBoxData'); LogBoxData.addException({ ...data, isComponentError: !!e.isComponentError, diff --git a/Libraries/LogBox/LogBox.js b/Libraries/LogBox/LogBox.js index 92f5133638aa4e..6c489e3e4ea0d8 100644 --- a/Libraries/LogBox/LogBox.js +++ b/Libraries/LogBox/LogBox.js @@ -12,8 +12,6 @@ import Platform from '../Utilities/Platform'; import RCTLog from '../Utilities/RCTLog'; -import * as LogBoxData from './Data/LogBoxData'; -import {parseLogBoxLog, parseInterpolation} from './Data/parseLogBoxLog'; import type {IgnorePattern} from './Data/LogBoxData'; @@ -23,6 +21,9 @@ let LogBox; * LogBox displays logs in the app. */ if (__DEV__) { + const LogBoxData = require('./Data/LogBoxData'); + const {parseLogBoxLog, parseInterpolation} = require('./Data/parseLogBoxLog'); + // LogBox needs to insert itself early, // in order to access the component stacks appended by React DevTools. const {error, warn} = console;