-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
43,530 additions
and
259 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 |
---|---|---|
|
@@ -6,4 +6,5 @@ coverage/ | |
run/ | ||
.DS_Store | ||
*.swp | ||
|
||
.vscode | ||
*.iml |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
console.log('app.config.env =', app.config.env); | ||
app.view.use('react', require('./lib/view')); | ||
}; |
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 |
---|---|---|
@@ -1,25 +1,14 @@ | ||
'use strict'; | ||
|
||
const VIEW_ENGINE = Symbol('app#ViewEngine'); | ||
const View = require('../../lib/view'); | ||
const engine = require('react-dom/server'); | ||
const Engine = require('../../lib/engine'); | ||
const REACT_ENGINE = Symbol('Application#react'); | ||
|
||
module.exports = { | ||
// mount `View` class to app | ||
// egg will create an instance to `ctx.view` at every request | ||
// you can use `this.render` at controller | ||
get [Symbol.for('egg#view')]() { | ||
return View; | ||
}, | ||
|
||
/** | ||
* react viewEngine | ||
* @member {Object} Application#viewEngine | ||
*/ | ||
get viewEngine() { | ||
if (!this[VIEW_ENGINE]) { | ||
this[VIEW_ENGINE] = engine; | ||
get react() { | ||
if (!this[REACT_ENGINE]) { | ||
this[REACT_ENGINE] = new Engine(this); | ||
} | ||
return this[VIEW_ENGINE]; | ||
return this[REACT_ENGINE]; | ||
}, | ||
}; |
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 |
---|---|---|
@@ -1,14 +1,15 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
|
||
module.exports = appInfo => { | ||
module.exports = () => { | ||
const config = {}; | ||
|
||
config.view = { | ||
extname: 'js', | ||
dir: path.join(appInfo.baseDir, 'app/view'), | ||
mapping: { | ||
'.js': 'react', | ||
'.jsx': 'react', | ||
}, | ||
}; | ||
|
||
return config; | ||
}; | ||
|
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 @@ | ||
'use strict'; | ||
|
||
exports.react = { | ||
cache: false, | ||
}; |
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,30 @@ | ||
'use strict'; | ||
const React = require('react'); | ||
const ReactDOMServer = require('react-dom/server'); | ||
|
||
class Engine { | ||
constructor(app) { | ||
this.app = app; | ||
this.config = app.config.react; | ||
} | ||
|
||
render(name, locals) { | ||
const reactClass = require(name); | ||
return Promise.resolve(this.renderToString(reactClass, locals)); | ||
} | ||
|
||
renderMarkup(name, locals) { | ||
const reactClass = require(name); | ||
return Promise.resolve(this.renderToStaticMarkup(reactClass, locals)); | ||
} | ||
|
||
renderToString(reactClass, locals) { | ||
return ReactDOMServer.renderToString(React.createElement(reactClass, locals)); | ||
} | ||
|
||
renderToStaticMarkup(reactClass, locals) { | ||
return ReactDOMServer.renderToStaticMarkup(React.createElement(reactClass, locals)); | ||
} | ||
} | ||
|
||
module.exports = Engine; |
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 |
---|---|---|
@@ -1,37 +1,18 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const React = require('react'); | ||
|
||
class View { | ||
|
||
constructor(ctx) { | ||
this.ctx = ctx; | ||
this.app = ctx.app; | ||
this.config = ctx.app.config.view; | ||
this.extname = this.config.extname.replace(/^\.?/, '.'); | ||
} | ||
|
||
render(name, locals) { | ||
const reactFile = path.join(this.config.dir, name + this.extname); | ||
|
||
return new Promise((resolve, reject) => { | ||
let html = '<!DOCTYPE html>'; | ||
try { | ||
const reactComponent = require(reactFile); | ||
html += this.app.viewEngine.renderToString(React.createElement(reactComponent.default || reactComponent, locals || {})); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
|
||
resolve(html); | ||
}); | ||
render(name, locals, options) { | ||
return this.app.react.render(name, locals, options); | ||
} | ||
|
||
renderString() { | ||
return Promise.reject('not implemented yet!'); | ||
} | ||
|
||
} | ||
|
||
module.exports = View; |
Oops, something went wrong.