-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generating the js bundle and standalone bundles for each page.
- Loading branch information
Showing
6 changed files
with
193 additions
and
9 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
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,73 @@ | ||
var React = require('react') | ||
var render = require('react-dom').render | ||
var createClass = require('create-react-class') | ||
var createHistory = require('history').createBrowserHistory | ||
var amd = require('micro-amd')() | ||
var catchLinks = require('catch-links') | ||
|
||
const {standaloneURL} = require(process.env.utils) | ||
|
||
window.define = amd.define.bind(amd) | ||
window.define.amd = true | ||
|
||
const helmet = process.env.helmet | ||
const body = process.env.body | ||
|
||
var history = createHistory({ | ||
basename: '' | ||
}) | ||
|
||
var Main = createClass({ | ||
displayName: 'ReactSiteMain', | ||
|
||
getInitialState: function () { | ||
return { | ||
component: this.props.component, | ||
props: this.props.props | ||
} | ||
}, | ||
|
||
componentDidMount: function () { | ||
var self = this | ||
|
||
history.listen(function (location) { | ||
console.log('> navigating', location.pathname) | ||
amd.require([standaloneURL(location.pathname)], function (page) { | ||
var props = page.props | ||
props.location = location | ||
|
||
self.setState({ | ||
component: page.component, | ||
props: props | ||
}) | ||
}) | ||
}) | ||
|
||
catchLinks(document.body, function (href) { | ||
history.push(href) | ||
}) | ||
}, | ||
|
||
render: function () { | ||
var Body = require(body) | ||
var Helmet = require(helmet) | ||
|
||
return React.createElement(Body, this.state.props, | ||
React.createElement(Helmet, this.state.props), | ||
React.createElement(this.state.component, this.state.props) | ||
) | ||
} | ||
}) | ||
|
||
amd.require([standaloneURL(window.location.pathname)], function (page) { | ||
var props = page.props | ||
props.location = { | ||
pathname: window.location.pathname, | ||
search: '' | ||
} | ||
|
||
render(React.createElement(Main, { | ||
component: page.component, | ||
props: props | ||
}), document.body) | ||
}) |
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,2 @@ | ||
module.exports.props = process.env.props | ||
module.exports.component = require(process.env.componentpath) |
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,16 @@ | ||
module.exports.standaloneURL = standaloneURL | ||
|
||
function standaloneURL (pathname, isStatic) { | ||
var url | ||
if (pathname === '/') { | ||
url = '/index.js' | ||
} else { | ||
url = pathname + pathname.split('/').slice(-2)[0] + '.js' | ||
} | ||
|
||
if (!isStatic && process.env.NODE_ENV !== 'production') { | ||
url += '?t=' + Date.now() | ||
} | ||
|
||
return url | ||
} |