-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
32 lines (30 loc) · 1.25 KB
/
app.js
File metadata and controls
32 lines (30 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict';
const path = require('path');
const vm = require('vm');
const NativeModule = require('module');
module.exports = app => {
if (app.view) {
app.view.resolve = function(name) {
return Promise.resolve(name);
};
}
if (app.react) {
app.react.render = async (name, locals, options) => {
const filePath = path.isAbsolute(name) ? name : path.join(app.config.view.root[0], name);
const code = await app.webpack.fileSystem.readWebpackMemoryFile(filePath, name);
if (!code) {
throw new Error(`read webpack memory file[${filePath}] content is empty, please check if the file exists`);
}
const wrapper = NativeModule.wrap(code);
// can‘t find async chunk file fix: https://github.com/easy-team/egg-react-webpack-boilerplate/issues/23
module.id = filePath;
module.filename = filePath;
vm.runInThisContext(wrapper)(exports, require, module, filePath, path.dirname(filePath));
const reactClass = module.exports && module.exports.default ? module.exports : exports.default ? exports : module.exports;
if (options && options.markup) {
return app.react.renderToStaticMarkup(reactClass, locals);
}
return app.react.renderElement(reactClass, locals);
};
}
};