Skip to content

Commit

Permalink
Removed the pathToReact option.
Browse files Browse the repository at this point in the history
  • Loading branch information
markfinger committed May 23, 2015
1 parent 81d97df commit 7f99255
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 173 deletions.
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ react-render

[![Build Status](https://travis-ci.org/markfinger/react-render.svg?branch=master)](https://travis-ci.org/markfinger/react-render)

Handles the simple use case of loading in a component and rendering it to markup.

The renderer will attempt to avoid React version incompatibilities by resolving
a path from your component to the local React package.
Handles the simple use case of importing a component and rendering it to markup.


Installation
Expand Down Expand Up @@ -42,11 +39,6 @@ reactRender({
// markup. Defaults to false.
toStaticMarkup: true

// An absolute path that can be used to load the version of React.
// If the renderer can not find React from the component's path, you
// can specify it.
pathToReact: '...'

}, function(err, markup) {
if (err) throw err;

Expand Down
72 changes: 17 additions & 55 deletions lib/Component.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
var React = require('react');
var path = require('path');
var resolve = require('resolve');

var Component = function Component(opts) {
this.opts = opts;
this.path = null;
this.component = null;
this.factory = null;
this.React = null;
};

Component.prototype.getPath = function getPath(cb) {
Expand Down Expand Up @@ -47,37 +46,6 @@ Component.prototype.getComponent = function getComponent(cb) {
}.bind(this));
};

Component.prototype.getReact = function getReact(cb) {
if (this.React) return cb(null, this.React);

if (this.opts.pathToReact) {
try {
this.React = require(this.opts.pathToReact);
} catch(err) {
return cb(err);
}
return cb(null, this.React);
}

this.getPath(function(err, _path) {
if (err) return cb(err);

resolve('react', {
basedir: path.dirname(_path)
}, function(err, res) {
if (err) return cb(err);

try {
this.React = require(res);
} catch(err) {
return cb(err);
}

cb(null, this.React);
}.bind(this));
}.bind(this));
};

Component.prototype.getFactory = function getFactory(cb) {
if (this.factory) {
return cb(null, this.factory);
Expand All @@ -86,39 +54,33 @@ Component.prototype.getFactory = function getFactory(cb) {
this.getComponent(function(err, component) {
if (err) return cb(err);

this.getReact(function(err, React) {
if (err) return cb(err);

try {
this.factory = React.createFactory(component);
} catch(err) {
cb(err);
}
try {
this.factory = React.createFactory(component);
} catch(err) {
cb(err);
}

cb(null, this.factory);
});
cb(null, this.factory);
}.bind(this));
};

Component.prototype._render = function _render(props, toStaticMarkup, cb) {
this.getFactory(function(err, factory) {
if (err) return cb(err);

this.getReact(function(err, React) {
if (err) return cb(err);
if (err) return cb(err);

var render = (
toStaticMarkup ? React.renderToStaticMarkup : React.renderToString
).bind(React);
var render = (
toStaticMarkup ? React.renderToStaticMarkup : React.renderToString
).bind(React);

try {
var markup = render(factory(props));
} catch (err) {
return cb(err);
}
try {
var markup = render(factory(props));
} catch (err) {
return cb(err);
}

cb(null, markup);
});
cb(null, markup);
}.bind(this));
};

Expand Down
16 changes: 0 additions & 16 deletions lib/cache.js

This file was deleted.

24 changes: 20 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
var _ = require('lodash');
var Component = require('./Component');
var cache = require('./cache');

var components = {
_cache: [],
find: function(opts) {
return _.find(this._cache, function(obj) {
return _.isEqual(obj.opts, opts);
});
},
add: function(obj) {
this._cache.push(obj);
},
clear: function() {
this._cache = [];
}
};

var reactRender = function reactRender(opts, cb) {
var _opts = _.pick(opts, 'component', 'path', 'pathToReact');
var _opts = _.pick(opts, 'component', 'path');

var component = cache.find(_opts);
var component = components.find(_opts);

if (!component) {
component = new Component(_opts);
cache.add(component);
components.add(component);
}

var props = opts.props;
Expand All @@ -28,4 +42,6 @@ var reactRender = function reactRender(opts, cb) {
}
};

reactRender._components = components;

module.exports = reactRender;
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
"url": "https://github.com/markfinger/react-render.git"
},
"dependencies": {
"lodash": "^3.6.0",
"resolve": "^1.1.6"
"lodash": "^3.6.0"
},
"peerDependencies": {
"react": ">= 0.12"
},
"devDependencies": {
"chai": "^2.1.1",
"mocha": "^2.2.1",
"react": "*"
"mocha": "^2.2.1"
},
"scripts": {
"test": "mocha",
Expand Down
35 changes: 0 additions & 35 deletions test/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,39 +114,4 @@ describe('Component', function() {
done();
});
});
it('can resolve React from a component\'s path', function(done) {
var component = new Component({
path: Hello
});

component.getReact(function(err, React) {
assert.isNull(err);
assert.strictEqual(React, require('react'));
done();
});
});
it('can provide an error if React is unresolvable from a component\'s path', function(done) {
var component = new Component({
path: path.join(__dirname, '..', '..', 'test.js')
});

component.getReact(function(err, React) {
assert.isNotNull(err);
assert.instanceOf(err, Error);
assert.isUndefined(React);
done();
});
});
it('can accept an option denoting a path to react', function(done) {
var component = new Component({
path: Hello,
pathToReact: path.join(__dirname, '..', 'node_modules', 'react')
});

component.getReact(function(err, React) {
assert.isNull(err);
assert.strictEqual(React, require('react'));
done();
});
});
});
79 changes: 29 additions & 50 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
var path = require('path');
var assert = require('chai').assert;
var reactRender = require('..');
var cache = require('../lib/cache');
var Component = require('../lib/Component');

var Hello = path.join(__dirname, 'test_components', 'Hello.js');
var ErrorThrowingComponent = path.join(__dirname, 'test_components', 'ErrorThrowingComponent.js');

describe('reactRender', function() {
beforeEach(function() {
cache.clear();
reactRender._components.clear();
});
it('is a function', function() {
assert.isFunction(reactRender);
Expand Down Expand Up @@ -91,57 +90,57 @@ describe('reactRender', function() {
});
});
it('should create `Component` instances when called', function(done) {
assert.isArray(cache._cache);
assert.equal(cache._cache.length, 0);
assert.isArray(reactRender._components._cache);
assert.equal(reactRender._components._cache.length, 0);

reactRender({
path: Hello
}, function() {
assert.equal(cache._cache.length, 1);
assert.instanceOf(cache._cache[0], Component);
assert.equal(cache._cache[0].component, require(Hello));
assert.equal(reactRender._components._cache.length, 1);
assert.instanceOf(reactRender._components._cache[0], Component);
assert.equal(reactRender._components._cache[0].component, require(Hello));
done();
});
});
it('should reuse Component instances when called', function() {
assert.isArray(cache._cache);
assert.equal(cache._cache.length, 0);
assert.isArray(reactRender._components._cache);
assert.equal(reactRender._components._cache.length, 0);

reactRender({component: Hello}, function() {});
assert.equal(cache._cache.length, 1);
assert.instanceOf(cache._cache[0], Component);
assert.equal(cache._cache[0].component, Hello);
assert.equal(reactRender._components._cache.length, 1);
assert.instanceOf(reactRender._components._cache[0], Component);
assert.equal(reactRender._components._cache[0].component, Hello);


reactRender({path: 'foo'}, function() {});
assert.equal(cache._cache.length, 2);
assert.instanceOf(cache._cache[1], Component);
assert.equal(cache._cache[1].opts.path, 'foo');
assert.equal(reactRender._components._cache.length, 2);
assert.instanceOf(reactRender._components._cache[1], Component);
assert.equal(reactRender._components._cache[1].opts.path, 'foo');

reactRender({path: 'foo'}, function() {});
assert.equal(cache._cache.length, 2);
assert.instanceOf(cache._cache[1], Component);
assert.equal(cache._cache[1].opts.path, 'foo');
assert.equal(reactRender._components._cache.length, 2);
assert.instanceOf(reactRender._components._cache[1], Component);
assert.equal(reactRender._components._cache[1].opts.path, 'foo');

reactRender({path: 'foo', props: {bar: 1}}, function() {});
assert.equal(cache._cache.length, 2);
assert.instanceOf(cache._cache[1], Component);
assert.equal(cache._cache[1].opts.path, 'foo');
assert.equal(reactRender._components._cache.length, 2);
assert.instanceOf(reactRender._components._cache[1], Component);
assert.equal(reactRender._components._cache[1].opts.path, 'foo');

reactRender({path: 'bar', serializedProps: {bar: 1}}, function() {});
assert.equal(cache._cache.length, 3);
assert.instanceOf(cache._cache[2], Component);
assert.equal(cache._cache[2].opts.path, 'bar');
assert.equal(reactRender._components._cache.length, 3);
assert.instanceOf(reactRender._components._cache[2], Component);
assert.equal(reactRender._components._cache[2].opts.path, 'bar');

reactRender({path: 'foo', serializedProps: '{"bar": 1}'}, function() {});
assert.equal(cache._cache.length, 3);
assert.instanceOf(cache._cache[1], Component);
assert.equal(cache._cache[1].opts.path, 'foo');
assert.equal(reactRender._components._cache.length, 3);
assert.instanceOf(reactRender._components._cache[1], Component);
assert.equal(reactRender._components._cache[1].opts.path, 'foo');

reactRender({path: 'bar', serializedProps: '{"bar": 1}'}, function() {});
assert.equal(cache._cache.length, 3);
assert.instanceOf(cache._cache[2], Component);
assert.equal(cache._cache[2].opts.path, 'bar');
assert.equal(reactRender._components._cache.length, 3);
assert.instanceOf(reactRender._components._cache[2], Component);
assert.equal(reactRender._components._cache[2].opts.path, 'bar');
});
it('passes up errors thrown during a component\'s rendering', function(done) {
reactRender({
Expand All @@ -154,24 +153,4 @@ describe('reactRender', function() {
done();
});
});
it('can accept an option denoting a path to react', function(done) {
reactRender({
path: Hello,
pathToReact: path.join(__dirname, '..', 'node_modules', 'react'),
toStaticMarkup: true
}, function(err, markup) {
assert.isNull(err);
assert.equal(markup, '<div>Hello </div>');

reactRender({
path: Hello,
pathToReact: path.join(__dirname, '..', '..')
}, function(err, markup) {
assert.isNotNull(err);
assert.instanceOf(err, Error);
assert.isUndefined(markup);
done();
});
});
});
});

0 comments on commit 7f99255

Please sign in to comment.