switcharoo is a web server and build system for building polymorphic React applications in node or io.js. It is part of a larger series of plugins that, together, form frontend applications for reddit.
This application provides the core to set up an Express web server and html5 history api, and have these send a request object (containing path, data, etc) and a callback (a promise interface) to the App.
This application also provides a build system and a base css framework, shared across plugins.
+---------+ +---------------+
| express | | html5 history |
+---------+ | api |
| +---------------+
req, cb req, cb
| |
\ /
------------------------
|
v
+--------+
| App.js |
+--------+
|
(router)
|
v
+--------+
| plugin | -> cb.resolve({ body: reactElement })
| | -> cb.reject({ status: 401 })
+--------+
The App has an instance of an Express-like request router that it uses to map requests to the appropriate handling function, and is run on both the client- and server- side. The React lifecycle can be used to control client-specific code.
Plugins register themselves via two interfaces:
- Route handlers that take two paramaters,
reqandres.resis a promise interface that should be called usingres.resolve({ body: reactElement, status: 200})orres.reject({ body: error, status: 400}). (Body and status are optional in both cases, but should generally be added.) A complete example of route handling can be seen at switcharoo-plugin-core. - Mutators that modify the rendering of react components. An element query syntax is provided, documentation forthcoming.
- Install node.js v0.12+ or io.js v1.8.1+.
- Make sure that npm's /node_modules/ directory is in your PATH with the variable name NODE_PATH.
- Fork and clone this project.
- Also fork and clone any plugins you plan on developing. In these, run
npm linkto cause the local version of the plugin to be linked to npm. You'll also want tonpm linkfrom withinreddit-mobileso that plugins can resolve circular dependencies. - Run
npm installto install other dependencies.rm -rf node_modules/<plugin_name>if you are doing local plugin development as well. - Run
git submodule update --initto download this project's submodules - Run
npm run buildto build the assets (npm run watchto set up a filesystem watcher, which will include linked plugins.) - Run
npm startto start the web server. Optionally, create a startup script atstart.shthat sets environment variables and starts the server;start.shhas been added to the.gitignoreand will not get checked in.