-
Notifications
You must be signed in to change notification settings - Fork 940
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(examples): make it an isomorphic app
- Loading branch information
Showing
10 changed files
with
291 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,5 +42,6 @@ node_modules | |
.module-cache | ||
bower_components | ||
public/assets | ||
public/tmp | ||
public/index.html | ||
public/*.gz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
"use strict"; | ||
|
||
var React = require("react"); | ||
var evaluate = require("eval"); | ||
|
||
var SEPERATOR = "----------------------------------------";// 40 chars | ||
|
||
function IsomorphicReactPluginFactory (_options_) { | ||
var options = _options_ || {}; | ||
this.serverComponentPath = options.serverComponentPath; | ||
this.serverMarkupPath = options.serverMarkupPath; | ||
this.htmlOutputPath = options.htmlOutputPath; | ||
|
||
if ("string" !== typeof options.htmlDoctype) { | ||
options.htmlDoctype = "<!DOCTYPE html>"; | ||
} | ||
this.htmlDoctype = options.htmlDoctype; | ||
|
||
var evaluateOptions = this.evaluateOptions = options.evaluateOptions || {}; | ||
if (false !== evaluateOptions.includeGlobals) { | ||
evaluateOptions.includeGlobals = true; | ||
} | ||
|
||
return { | ||
clientPlugin: new ClientPlugin(this), | ||
serverPlugin: new ServerPlugin(this), | ||
}; | ||
} | ||
|
||
IsomorphicReactPluginFactory.prototype.receivedClientAssets = function (clientAssets) { | ||
this.clientAssets = clientAssets; | ||
if (this.serverAssets) { | ||
this.runCompilation(); | ||
} | ||
} | ||
|
||
IsomorphicReactPluginFactory.prototype.receivedServerEmit = function (serverAssets, done) { | ||
this.serverAssets = serverAssets; | ||
this.done = done; | ||
if (this.clientAssets) { | ||
this.runCompilation(); | ||
} | ||
} | ||
|
||
IsomorphicReactPluginFactory.prototype.evaluate = function (serverPath, errorMessage) { | ||
var serverAsset = this.serverAssets[serverPath]; | ||
if (undefined === serverAsset) { | ||
throw new Error(errorMessage + ": \"" + serverPath + "\""); | ||
} | ||
|
||
var source = serverAsset.source(); | ||
var includeGlobals = this.evaluateOptions.includeGlobals; | ||
|
||
try { | ||
return evaluate(source, includeGlobals); | ||
} catch (error) { | ||
error.message = serverPath + "\n" + SEPERATOR + SEPERATOR + error.message; | ||
error.stack = error.stack + "\n" + SEPERATOR + SEPERATOR + source + "\n" + SEPERATOR + SEPERATOR; | ||
throw error; | ||
} | ||
} | ||
|
||
IsomorphicReactPluginFactory.prototype.runCompilation = function () { | ||
var error; | ||
|
||
try { | ||
var ServerComponent = this.evaluate(this.serverComponentPath, "Component for server rendering is missing"); | ||
var componentString = React.renderToString(React.createElement(ServerComponent)); | ||
|
||
var MarkupComponent = this.evaluate(this.serverMarkupPath, "Component for markup generation is missing"); | ||
var htmlString = React.renderToStaticMarkup( | ||
React.createElement(MarkupComponent, { | ||
componentString: componentString, | ||
clientAssets: this.clientAssets, | ||
}) | ||
); | ||
|
||
this.serverAssets[this.htmlOutputPath] = createAssetFromContents( | ||
this.htmlDoctype + htmlString | ||
); | ||
|
||
} catch (_error_) { | ||
error = _error_; | ||
} | ||
this.done(error); | ||
} | ||
|
||
function ClientPlugin (isomorphicReactPluginFactory) { | ||
this.pluginFactory = isomorphicReactPluginFactory; | ||
} | ||
|
||
ClientPlugin.prototype.apply = function (compiler) { | ||
compiler.plugin("emit", (function (compilation, done) { | ||
var assets = getAssetsFromCompilation(compilation); | ||
|
||
this.pluginFactory.receivedClientAssets(assets); | ||
|
||
done(); | ||
}).bind(this)); | ||
} | ||
|
||
function ServerPlugin (isomorphicReactPluginFactory) { | ||
this.pluginFactory = isomorphicReactPluginFactory; | ||
} | ||
|
||
ServerPlugin.prototype.apply = function (compiler) { | ||
compiler.plugin("emit", (function (compilation, done) { | ||
|
||
this.pluginFactory.receivedServerEmit(compilation.assets, done); | ||
}).bind(this)); | ||
} | ||
|
||
module.exports = IsomorphicReactPluginFactory; | ||
|
||
// Shamelessly stolen from html-webpack-plugin - Thanks @ampedandwired :) | ||
function getAssetsFromCompilation (compiler) { | ||
var assets = {}; | ||
var webpackStatsJson = compiler.getStats().toJson(); | ||
for (var chunk in webpackStatsJson.assetsByChunkName) { | ||
var chunkValue = webpackStatsJson.assetsByChunkName[chunk]; | ||
|
||
// Webpack outputs an array for each chunk when using sourcemaps | ||
if (chunkValue instanceof Array) { | ||
// Is the main bundle always the first element? | ||
chunkValue = chunkValue[0]; | ||
} | ||
|
||
if (compiler.options.output.publicPath) { | ||
chunkValue = compiler.options.output.publicPath + chunkValue; | ||
} | ||
assets[chunk] = chunkValue; | ||
} | ||
return assets; | ||
} | ||
|
||
function createAssetFromContents(contents) { | ||
return { | ||
source: function() { | ||
return contents; | ||
}, | ||
size: function() { | ||
return contents.length; | ||
} | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from "react/addons"; | ||
|
||
const {PropTypes} = React; | ||
|
||
const ReactHtml = React.createClass({ | ||
|
||
propTypes: { | ||
componentString: PropTypes.string.isRequired, | ||
clientAssets: PropTypes.object.isRequired, | ||
}, | ||
|
||
render () { | ||
const {props, state} = this, | ||
{clientAssets} = props; | ||
|
||
return ( | ||
<html> | ||
<head> | ||
<title>React Google Maps | tomchentw</title> | ||
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp" /> | ||
<script type="text/javascript" src="prism.min.js" /> | ||
</head> | ||
<body> | ||
<div id="react-container" dangerouslySetInnerHTML={{__html: props.componentString}}/> | ||
<script type="text/javascript" src={clientAssets["assets/main"]} /> | ||
</body> | ||
</html> | ||
); | ||
} | ||
}); | ||
|
||
export default ReactHtml; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from "react/addons"; | ||
|
||
import ReactRoot from "./ReactRoot"; | ||
|
||
export default ReactRoot; |
Oops, something went wrong.