From 9fe2611e570a16273fde146f31e9ca18dd2427fd Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 24 Nov 2015 18:39:20 +0500 Subject: [PATCH] Added static method for rendering --- README.md | 6 +++++- bin/react | 8 +++++++- lib/facade.js | 8 ++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b08744c..a5a3512 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ react-cli -h, --help output usage information -V, --version output the version number -p, --props [props] Props to pass into the component + -s, --static Use static render ## Examples @@ -21,7 +22,10 @@ Without passing `props`: react-cli my-component.jsx - Passing `props` react-cli my-component.jsx -p "{\"foo\": \"bar\"}" + +With static render + + react-cli my-component.jsx -s diff --git a/bin/react b/bin/react index f88a551..b84262d 100755 --- a/bin/react +++ b/bin/react @@ -8,10 +8,16 @@ commander .version(require('../package.json').version) .usage('[source file]') .option('-p, --props [props]', 'Props to pass into the component') + .option('-s, --static', 'Use static render') .parse(process.argv); if (commander.args[0]) { - var html = cli.render(commander.args[0], commander.props); + var html; + if (commander.static) { + html = cli.renderStatic(commander.args[0], commander.props); + } else { + html = cli.render(commander.args[0], commander.props); + } console.log(html); } else { commander.help(); diff --git a/lib/facade.js b/lib/facade.js index 44614eb..f8d279f 100644 --- a/lib/facade.js +++ b/lib/facade.js @@ -11,5 +11,13 @@ module.exports = { var ComponentFactory = React.createFactory(Component); return React.renderToString(ComponentFactory(props)); + }, + renderStatic: function (componentPath, props) { + var componentPath = path.resolve(process.cwd(), componentPath); + var Component = require(componentPath); + props = props && JSON.parse(props); + + var ComponentFactory = React.createFactory(Component); + return React.renderToStaticMarkup(ComponentFactory(props)); } };