Skip to content

Commit

Permalink
Smaller production builds
Browse files Browse the repository at this point in the history
Builds on the exclusion of PropTypes from production builds:

- Remove 'lodash' and use smaller modules for equivalent functions.
- Remove use of some unnecessary Facebook utilities.
- Remove 'TouchableBounce'; it isn't part of React Native anymore.
- Remove stray import of 'react-dom/server'.
- Exclude 'StyleSheetValidation' from production.

Measuring the UMD build (gzip)…

Before: ~100KB
After: ~60KB
  • Loading branch information
necolas committed Nov 23, 2016
1 parent d65c92e commit 66c996c
Show file tree
Hide file tree
Showing 21 changed files with 74 additions and 440 deletions.
6 changes: 3 additions & 3 deletions examples/demos/Game2048/Game2048.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var {
AppRegistry,
StyleSheet,
Text,
TouchableBounce,
TouchableOpacity,
View,
} = ReactNative;

Expand Down Expand Up @@ -139,9 +139,9 @@ class GameEndOverlay extends React.Component {
return (
<View style={styles.overlay}>
<Text style={styles.overlayMessage}>{message}</Text>
<TouchableBounce onPress={this.props.onRestart} style={styles.tryAgain}>
<TouchableOpacity onPress={this.props.onRestart} style={styles.tryAgain}>
<Text style={styles.tryAgainText}>Try Again?</Text>
</TouchableBounce>
</TouchableOpacity>
</View>
);
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
},
"dependencies": {
"animated": "^0.1.3",
"array-find-index": "^1.0.2",
"babel-plugin-transform-react-remove-prop-types": "^0.2.11",
"babel-runtime": "^6.11.6",
"debounce": "^1.0.0",
"deep-assign": "^2.0.0",
"fbjs": "^0.8.4",
"inline-style-prefixer": "^2.0.1",
"lodash": "^4.15.0",
"react-dom": "~15.3.2",
"react-textarea-autosize": "^4.0.4",
"react-timer-mixin": "^0.13.3",
Expand Down
2 changes: 1 addition & 1 deletion src/apis/AppRegistry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { Component } from 'react';
import invariant from 'fbjs/lib/invariant';
import { unmountComponentAtNode } from 'react/lib/ReactMount';
import renderApplication, { prerenderApplication } from './renderApplication';
import renderApplication, { getApplication } from './renderApplication';

const runnables = {};

Expand Down
1 change: 0 additions & 1 deletion src/apis/AppRegistry/renderApplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import invariant from 'fbjs/lib/invariant';
import { render } from 'react/lib/ReactMount';
import ReactDOMServer from 'react-dom/server';
import ReactNativeApp from './ReactNativeApp';
import StyleSheet from '../../apis/StyleSheet';
import React, { Component } from 'react';
Expand Down
2 changes: 1 addition & 1 deletion src/apis/AppState/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment';
import findIndex from 'lodash/findIndex';
import findIndex from 'array-find-index';
import invariant from 'fbjs/lib/invariant';

const EVENT_TYPES = [ 'change' ];
Expand Down
2 changes: 1 addition & 1 deletion src/apis/AsyncStorage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*/
import merge from 'lodash/merge';
import merge from 'deep-assign';

const mergeLocalStorageItem = (key, value) => {
const oldValue = window.localStorage.getItem(key);
Expand Down
2 changes: 1 addition & 1 deletion src/apis/Dimensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @flow
*/

import debounce from 'lodash/debounce';
import debounce from 'debounce';
import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment';
import invariant from 'fbjs/lib/invariant';

Expand Down
9 changes: 4 additions & 5 deletions src/apis/InteractionManager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
*/

import invariant from 'fbjs/lib/invariant';
import keyMirror from 'fbjs/lib/keyMirror';

const InteractionManager = {
Events: keyMirror({
interactionStart: true,
interactionComplete: true
}),
Events: {
interactionStart: 'interactionStart',
interactionComplete: 'interactionComplete'
},

/**
* Schedule a function to run after all interactions have completed.
Expand Down
2 changes: 1 addition & 1 deletion src/apis/NetInfo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment';
import findIndex from 'lodash/findIndex';
import findIndex from 'array-find-index';
import invariant from 'fbjs/lib/invariant';

const connection = ExecutionEnvironment.canUseDOM && (
Expand Down
12 changes: 11 additions & 1 deletion src/apis/StyleSheet/__tests__/index-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
/* eslint-env jasmine, jest */

import { getDefaultStyleSheet } from '../css';
import isPlainObject from 'lodash/isPlainObject';
import StyleSheet from '..';

const isPlainObject = (x) => {
const toString = Object.prototype.toString;
let proto;
/* eslint-disable */
return (
toString.call(x) === '[object Object]' &&
(proto = Object.getPrototypeOf(x), proto === null || proto === Object.getPrototypeOf({}))
);
/* eslint-enable */
};

describe('apis/StyleSheet', () => {
beforeEach(() => {
StyleSheet._reset();
Expand Down
5 changes: 3 additions & 2 deletions src/apis/StyleSheet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment';
import flattenStyle from '../../modules/flattenStyle';
import React from 'react';
import ReactNativePropRegistry from '../../modules/ReactNativePropRegistry';
import StyleSheetValidation from './StyleSheetValidation';

let styleElement;
let shouldInsertStyleSheet = ExecutionEnvironment.canUseDOM;
Expand Down Expand Up @@ -52,7 +51,9 @@ module.exports = {

const result = {};
for (const key in styles) {
StyleSheetValidation.validateStyle(key, styles);
if (process.env.NODE_ENV !== 'production') {
require('./StyleSheetValidation').validateStyle(key, styles);
}
result[key] = ReactNativePropRegistry.register(styles[key]);
}
return result;
Expand Down
7 changes: 1 addition & 6 deletions src/components/ListView/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import applyNativeMethods from '../../modules/applyNativeMethods';
import ListViewDataSource from './ListViewDataSource';
import ListViewPropTypes from './ListViewPropTypes';
import pick from 'lodash/pick';
import ScrollView from '../ScrollView';
import View from '../View';
import React, { Component } from 'react';

const scrollViewProps = Object.keys(ScrollView.propTypes);

class ListView extends Component {
static propTypes = ListViewPropTypes;

Expand Down Expand Up @@ -90,9 +87,7 @@ class ListView extends Component {
}
}

const props = pick(this.props, scrollViewProps);

return React.cloneElement(this.props.renderScrollComponent(props), {
return React.cloneElement(this.props.renderScrollComponent(this.props), {
ref: this._setScrollViewRef
}, header, children, footer);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ScrollView/ScrollViewBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @flow
*/

import debounce from 'lodash/debounce';
import debounce from 'debounce';
import View from '../View';
import React, { Component, PropTypes } from 'react';

Expand Down
39 changes: 19 additions & 20 deletions src/components/Touchable/Touchable.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

/* @edit start */
const BoundingDimensions = require('./BoundingDimensions');
const keyMirror = require('fbjs/lib/keyMirror');
const normalizeColor = require('../../modules/normalizeColor');
const Position = require('./Position');
const React = require('react');
Expand Down Expand Up @@ -111,16 +110,16 @@ const View = require('../../components/View');
/**
* Touchable states.
*/
var States = keyMirror({
NOT_RESPONDER: null, // Not the responder
RESPONDER_INACTIVE_PRESS_IN: null, // Responder, inactive, in the `PressRect`
RESPONDER_INACTIVE_PRESS_OUT: null, // Responder, inactive, out of `PressRect`
RESPONDER_ACTIVE_PRESS_IN: null, // Responder, active, in the `PressRect`
RESPONDER_ACTIVE_PRESS_OUT: null, // Responder, active, out of `PressRect`
RESPONDER_ACTIVE_LONG_PRESS_IN: null, // Responder, active, in the `PressRect`, after long press threshold
RESPONDER_ACTIVE_LONG_PRESS_OUT: null, // Responder, active, out of `PressRect`, after long press threshold
ERROR: null
});
var States = {
NOT_RESPONDER: 'NOT_RESPONDER', // Not the responder
RESPONDER_INACTIVE_PRESS_IN: 'RESPONDER_INACTIVE_PRESS_IN', // Responder, inactive, in the `PressRect`
RESPONDER_INACTIVE_PRESS_OUT: 'RESPONDER_INACTIVE_PRESS_OUT', // Responder, inactive, out of `PressRect`
RESPONDER_ACTIVE_PRESS_IN: 'RESPONDER_ACTIVE_PRESS_IN', // Responder, active, in the `PressRect`
RESPONDER_ACTIVE_PRESS_OUT: 'RESPONDER_ACTIVE_PRESS_OUT', // Responder, active, out of `PressRect`
RESPONDER_ACTIVE_LONG_PRESS_IN: 'RESPONDER_ACTIVE_LONG_PRESS_IN', // Responder, active, in the `PressRect`, after long press threshold
RESPONDER_ACTIVE_LONG_PRESS_OUT: 'RESPONDER_ACTIVE_LONG_PRESS_OUT', // Responder, active, out of `PressRect`, after long press threshold
ERROR: 'ERROR'
};

/**
* Quick lookup map for states that are considered to be "active"
Expand All @@ -147,15 +146,15 @@ var IsLongPressingIn = {
/**
* Inputs to the state machine.
*/
var Signals = keyMirror({
DELAY: null,
RESPONDER_GRANT: null,
RESPONDER_RELEASE: null,
RESPONDER_TERMINATED: null,
ENTER_PRESS_RECT: null,
LEAVE_PRESS_RECT: null,
LONG_PRESS_DETECTED: null,
});
var Signals = {
DELAY: 'DELAY',
RESPONDER_GRANT: 'RESPONDER_GRANT',
RESPONDER_RELEASE: 'RESPONDER_RELEASE',
RESPONDER_TERMINATED: 'RESPONDER_TERMINATED',
ENTER_PRESS_RECT: 'ENTER_PRESS_RECT',
LEAVE_PRESS_RECT: 'LEAVE_PRESS_RECT',
LONG_PRESS_DETECTED: 'LONG_PRESS_DETECTED',
};

/**
* Mapping from States x Signals => States
Expand Down

0 comments on commit 66c996c

Please sign in to comment.