Skip to content

Commit

Permalink
removed node and added more info to the migration guide
Browse files Browse the repository at this point in the history
  • Loading branch information
jbhatab committed May 12, 2016
1 parent a7e6b91 commit e3624ea
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 134 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.md
Expand Up @@ -8,10 +8,13 @@ Contributors: please follow the recommendations outlined at [keepachangelog.com]
##### Breaking Changes
- Added automatic compilation of assets at precompile is now done by ReactOnRails. Thus, you don't need to provide your own assets.rake file that does the precompilation.
[#398](https://github.com/shakacode/react_on_rails/pull/398) by [robwise](https://github.com/robwise), [jbhatab](https://github.com/jbhatab), and [justin808](https://github.com/justin808).
- **Migration to v6: you can either:**
- **Migration to v6**

1. Specify a `config/react_on_rails` setting for `npm_build_production_command` to be nil to turn this feature off.
2. Specify the script command you want to run to build your production assets, and remove your assets.rake file.
- To configure the asset compliation you can either
1. Specify a `config/react_on_rails` setting for `npm_build_production_command` to be nil to turn this feature off.
2. Specify the script command you want to run to build your production assets, and remove your assets.rake file.

- If you are using the ReactOnRails test helper, then you will need to add the 'config.npm_build_test_command' to your config to tell react_on_rails what command to run when you run rspec.

- See [shakacode/react-webpack-rails-tutorial #287](https://github.com/shakacode/react-webpack-rails-tutorial/pull/287/files) for an upgrade example. The PR has a few comments on the upgrade.

Expand All @@ -20,6 +23,11 @@ Here is the addition to the generated config file:
# This configures the script to run to build the production assets by webpack. Set this to nil
# if you don't want react_on_rails building this file for you.
config.npm_build_production_command = "npm run build:production"

# If you are using the ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
# with rspec then this controls what npm command is run
# to automatically refresh your webpack assets on every test run.
config.npm_build_test_command = "npm run build:test"
```

##### Fixed
Expand Down
112 changes: 112 additions & 0 deletions README.md
Expand Up @@ -72,6 +72,7 @@ Please see [Getting Started](#getting-started) for how to set up your Rails proj
- [ReactOnRails JavaScript API](#reactonrails-javascript-api)
- [React-Router](#react-router)
- [Deployment](#deployment)
+ [Integration with Node](#integration-with-node)
+ [Additional Reading](#additional-reading)
+ [Contributing](#contributing)
+ [License](#license)
Expand Down Expand Up @@ -414,6 +415,117 @@ See [ReactOnRails JavaScriptAPI](docs/api/javascript-api.md).
* See the [Heroku Deployment](docs/additional-reading/heroku-deployment.md) doc for specifics regarding Heroku.
* If you're using the node server for server rendering, you may want to do your own AWS install. We'll have more docs on this in the future.

## Integration with Node
NodeJS can be used as the backend for server-side rendering instead of ExecJS. To do this you need to add a few files and then configure react_on_rails to use NodeJS. Here are the relevant files to add.

```javascript
// client/node/package.json
{
"name": "react_on_rails_node",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./server.js -s webpack-bundle.js"
},
"dependencies": {
}
}
```

```javascript
// client/node/server.js
var net = require('net');
var fs = require('fs');

var bundlePath = '../../app/assets/webpack/';
var bundleFileName = 'webpack-bundle.js';

var currentArg;

function Handler() {
this.queue = [];
this.initialized = false;
}

Handler.prototype.handle = function (connection) {
var callback = function () {
connection.setEncoding('utf8');
connection.on('data', (data)=> {
console.log('Processing request: ' + data);
var result = eval(data);
connection.write(result);
});
};

if (this.initialized) {
callback();
} else {
this.queue.push(callback);
}
};

Handler.prototype.initialize = function () {
console.log('Processing ' + this.queue.length + ' pending requests');
var callback;
while (callback = this.queue.pop()) {
callback();
}

this.initialized = true;
};

var handler = new Handler();

process.argv.forEach((val) => {
if (val[0] == '-') {
currentArg = val.slice(1);
return;
}

if (currentArg == 's') {
bundleFileName = val;
}
});

try {
fs.mkdirSync(bundlePath);
} catch (e) {
if (e.code != 'EEXIST') throw e;
}

fs.watchFile(bundlePath + bundleFileName, (curr) => {
if (curr && curr.blocks && curr.blocks > 0) {
if (handler.initialized) {
console.log('Reloading server bundle must be implemented by restarting the node process!');
return;
}

require(bundlePath + bundleFileName);
console.log('Loaded server bundle: ' + bundlePath + bundleFileName);
handler.initialize();
}
});

var unixServer = net.createServer(function (connection) {
handler.handle(connection);
});

unixServer.listen('node.sock');

process.on('SIGINT', () => {
unixServer.close();
process.exit();
});

```

The last thing you'll need to do is change the server_render_method to "NodeJS".

```ruby
# app/config/initializers/react_on_rails.rb
config.server_render_method = "NodeJS"
```

## Additional Reading
+ [JavaScript API](docs/api/javascript-api.md)
+ [Ruby API](docs/api/ruby-api.md)
Expand Down
8 changes: 0 additions & 8 deletions lib/generators/react_on_rails/base_generator.rb
Expand Up @@ -85,14 +85,6 @@ def add_base_gems_to_gemfile
append_to_file("Gemfile", "\ngem 'therubyracer', platforms: :ruby\n")
end

def install_node_files
base_path = "base/base/"
%w(client/node/package.json
client/node/server.js).each do |file|
copy_file(base_path + file, file)
end
end

ASSETS_RB_APPEND = <<-DATA.strip_heredoc
# Add client/assets/ folders to asset pipeline's search path.
# If you do not want to move existing images and fonts from your Rails app
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit e3624ea

Please sign in to comment.