Skip to content

Commit

Permalink
Bundle auto-bind because of Create React App
Browse files Browse the repository at this point in the history
😔
  • Loading branch information
sindresorhus committed May 1, 2018
1 parent 4e7e55c commit 33c1762
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,3 +1,4 @@
language: node_js
node_js:
- '10'
- '8'
50 changes: 50 additions & 0 deletions auto-bind.js
@@ -0,0 +1,50 @@
// From https://github.com/sindresorhus/auto-bind/blob/master/index.js
// Bundled because of Create React App…
'use strict';
module.exports = (self, options) => {
options = Object.assign({}, options);

const filter = key => {
const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key);

if (options.include) {
return options.include.some(match);
}

if (options.exclude) {
return !options.exclude.some(match);
}

return true;
};

for (const key of Object.getOwnPropertyNames(self.constructor.prototype)) {
const val = self[key];

if (key !== 'constructor' && typeof val === 'function' && filter(key)) {
self[key] = val.bind(self);
}
}

return self;
};

const excludedReactMethods = [
'componentWillMount',
'render',
'componentDidMount',
'componentWillReceiveProps',
'shouldComponentUpdate',
'componentWillUpdate',
'componentDidUpdate',
'componentWillUnmount',
'componentDidCatch',
'setState',
'forceUpdate'
];

module.exports.react = (self, options) => {
options = Object.assign({}, options);
options.exclude = (options.exclude || []).concat(excludedReactMethods);
return module.exports(self, options);
};
4 changes: 2 additions & 2 deletions index.d.ts
@@ -1,5 +1,7 @@
import * as React from 'react';

export function autoBind(el: React.ReactNode, options?: any): React.ReactNode;

export function classNames(...args: any[]): string;

export function isStatelessComponent(component: React.ComponentClass): boolean;
Expand All @@ -8,8 +10,6 @@ export function getDisplayName(component: React.ComponentClass): string;

export const canUseDOM: boolean;

export function autoBind(el: React.ReactNode, options?: any): React.ReactNode;

interface IfProps {
condition: boolean;
children?: React.ReactNode;
Expand Down
20 changes: 10 additions & 10 deletions index.js
@@ -1,7 +1,7 @@
/* eslint-disable no-unused-vars */
import React from 'react';
import PropTypes from 'prop-types';
import _autoBind from 'auto-bind';
import _autoBind from './auto-bind';

export const autoBind = _autoBind.react;

Expand All @@ -25,6 +25,15 @@ export const classNames = (...args) => {
return [...ret].join(' ');
};

export const isStatelessComponent = Component => !(
typeof Component.prototype !== 'undefined' &&
typeof Component.prototype.render === 'function'
);

export const getDisplayName = Component => Component.displayName || Component.name || 'Component';

export const canUseDOM = typeof window !== 'undefined' && 'document' in window && 'createElement' in window.document;

export const If = props => props.condition ? (props.render ? props.render() : props.children) : null;
If.propTypes = {
condition: PropTypes.bool.isRequired,
Expand Down Expand Up @@ -115,12 +124,3 @@ export class BodyClass extends ElementClass {
}
}
BodyClass.propTypes = ElementClass.propTypes;

export const isStatelessComponent = Component => !(
typeof Component.prototype !== 'undefined' &&
typeof Component.prototype.render === 'function'
);

export const getDisplayName = Component => Component.displayName || Component.name || 'Component';

export const canUseDOM = typeof window !== 'undefined' && 'document' in window && 'createElement' in window.document;
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -15,7 +15,7 @@
"node": ">=8"
},
"scripts": {
"build": "babel index.js --out-dir=dist",
"build": "babel index.js auto-bind.js --out-dir=dist",
"test": "xo && ava",
"prepublishOnly": "npm run build"
},
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Expand Up @@ -4,7 +4,7 @@ import React from 'react';
import {renderIntoDocument} from 'react-dom/test-utils';
import render from 'react-test-renderer';
import browserEnv from 'browser-env';
import {
import { // eslint-disable-line unicorn/import-index
classNames,
If,
For,
Expand Down

0 comments on commit 33c1762

Please sign in to comment.