Skip to content

Commit

Permalink
Added a pathToReact option.
Browse files Browse the repository at this point in the history
The option is an absolute path to the file which will be used to render the component.

If specified, it takes priority over the locally resolved one.

Re #1
  • Loading branch information
markfinger committed Apr 13, 2015
1 parent 4a7992e commit 85f3cf1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ react-render

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

To avoid version incompatibilities, React is resolved from the component's path.
The renderer will attempt to avoid React version incompatibilities by resolving
a path from your component to the local React package.


Installation
Expand Down Expand Up @@ -41,6 +42,11 @@ 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
9 changes: 9 additions & 0 deletions lib/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ Component.prototype.getComponent = function getComponent(cb) {
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);

Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var Component = require('./Component');
var cache = require('./cache');

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

var component = cache.find(_opts);

Expand Down
12 changes: 12 additions & 0 deletions test/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,16 @@ describe('Component', function() {
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();
});
});
});
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,24 @@ 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 85f3cf1

Please sign in to comment.