Skip to content

Commit

Permalink
Prefer using inline prevals over exporting a constant
Browse files Browse the repository at this point in the history
This will allow Rollup to optimize builds for UMD, ESM and CJS, without minification.
  • Loading branch information
eliperkins committed Feb 5, 2018
1 parent 2494363 commit 221d422
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 28 deletions.
21 changes: 11 additions & 10 deletions src/downshift.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import React, {Component} from 'react'
import PropTypes from 'prop-types'
import preval from 'preval.macro'
import setA11yStatus from './set-a11y-status'
import {
cbToCb,
Expand All @@ -13,8 +14,6 @@ import {
getA11yStatusMessage,
unwrapArray,
isDOMElement,
isPreact,
isReactNative,
getElementProps,
noop,
requiredProp,
Expand Down Expand Up @@ -220,7 +219,7 @@ class Downshift extends Component {

scrollHighlightedItemIntoView = () => {
/* istanbul ignore else (react-native) */
if (!isReactNative) {
if (preval`module.exports = process.env.BUILD_REACT_NATIVE !== 'true'`) {
const node = this.getItemNodeFromIndex(this.getState().highlightedIndex)
const rootNode = this._rootNode
scrollIntoView(node, rootNode)
Expand Down Expand Up @@ -541,7 +540,7 @@ class Downshift extends Component {

getButtonProps = ({onClick, onKeyDown, ...rest} = {}) => {
const {isOpen} = this.getState()
const enabledEventHandlers = isReactNative
const enabledEventHandlers = preval`module.exports = process.env.BUILD_REACT_NATIVE === 'true'`
? /* istanbul ignore next (react-native) */
{
onPress: composeEventHandlers(onClick, this.button_handleClick),
Expand Down Expand Up @@ -654,10 +653,12 @@ class Downshift extends Component {

input_getOnChangeKey() {
/* istanbul ignore next (preact) */
if (isPreact) {
if (preval`module.exports = process.env.BUILD_PREACT === 'true'`) {
return 'onInput'
/* istanbul ignore next (react-native) */
} else if (isReactNative) {
} else if (
preval`module.exports = process.env.BUILD_REACT_NATIVE === 'true'`
) {
return 'onChangeText'
} else {
return 'onChange'
Expand All @@ -674,7 +675,7 @@ class Downshift extends Component {
this.internalSetState({
type: Downshift.stateChangeTypes.changeInput,
isOpen: true,
inputValue: isReactNative
inputValue: preval`module.exports = process.env.BUILD_REACT_NATIVE === 'true'`
? /* istanbul ignore next (react-native) */ event
: event.target.value,
})
Expand Down Expand Up @@ -708,7 +709,7 @@ class Downshift extends Component {
this.items[index] = item
}

const onSelectKey = isReactNative
const onSelectKey = preval`module.exports = process.env.BUILD_REACT_NATIVE === 'true'`
? /* istanbul ignore next (react-native) */ 'onPress'
: 'onClick'
return {
Expand Down Expand Up @@ -783,7 +784,7 @@ class Downshift extends Component {
})
this.previousResultCount = resultCount
/* istanbul ignore else (react-native) */
if (!isReactNative) {
if (preval`module.exports = process.env.BUILD_REACT_NATIVE !== 'true'`) {
setA11yStatus(status)
}
}, 200)
Expand All @@ -793,7 +794,7 @@ class Downshift extends Component {
// and we don't want to update the status if the component has been umounted
this._isMounted = true
/* istanbul ignore if (react-native) */
if (isReactNative) {
if (preval`module.exports = process.env.BUILD_REACT_NATIVE === 'true'`) {
this.cleanup = () => {
this._isMounted = false
}
Expand Down
18 changes: 0 additions & 18 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import preval from 'preval.macro'

let idCounter = 1

/**
Expand Down Expand Up @@ -226,20 +224,6 @@ function isDOMElement(element) {
}
}

/**
* When built, this constant will signify if the current platform is React Native.
* This can be used to optimize codepaths for other platforms without conflating
* concepts because of the React Native target.
* @return {Boolean} whether or not the platform is React Native
*/
/* istanbul ignore next (react-native) */
const isReactNative = preval`module.exports = process.env.BUILD_REACT_NATIVE === 'true'`

// Disabling coverage here is necessary due to a weird deal with
// babel-plugin-istanbul + preval.macro. No idea...
/* istanbul ignore next (preact) */
const isPreact = preval`module.exports = process.env.BUILD_PREACT === 'true'`

/**
* @param {Object} element (P)react element
* @return {Object} the props
Expand Down Expand Up @@ -291,8 +275,6 @@ export {
getA11yStatusMessage,
unwrapArray,
isDOMElement,
isPreact,
isReactNative,
getElementProps,
noop,
requiredProp,
Expand Down

0 comments on commit 221d422

Please sign in to comment.