Add react tag helper. Clean up code and make it more consistent.

This commit is contained in:
SteveSandersonMS
2015-11-04 12:19:43 -08:00
parent e410affbd8
commit 7c3d22c7b6
14 changed files with 146 additions and 63 deletions

View File

@@ -10,6 +10,26 @@ var origJsLoader = require.extensions['.js'];
require.extensions['.js'] = loadViaBabel;
require.extensions['.jsx'] = loadViaBabel;
function findReactComponent(options) {
var resolvedPath = path.resolve(process.cwd(), options.moduleName);
var loadedModule = require(resolvedPath);
if (options.exportName) {
// If exportName is specified explicitly, use it
if (!(options.exportName in loadedModule)) {
throw new Error('The module "' + resolvedPath + '" has no export named "' + options.exportName + '"');
}
return loadedModule[options.exportName];
} else if (typeof loadedModule === 'function') {
// Otherwise, if the module itself is a function, assume that is the component
return loadedModule;
} else if (typeof loadedModule.default === 'function') {
// Otherwise, if the module has a default export which is a function, assume that is the component
return loadedModule.default;
} else {
throw new Error('Cannot find React component, because no export name was specified, and the module "' + resolvedPath + '" has no default exported class.');
}
}
function loadViaBabel(module, filename) {
// Assume that all the app's own code is ES2015+ (optionally with JSX), but that none of the node_modules are.
// The distinction is important because ES2015+ forces strict mode, and it may break ES3/5 if you try to run it in strict
@@ -25,14 +45,8 @@ function loadViaBabel(module, filename) {
module.exports = {
renderToString: function(callback, options) {
var resolvedPath = path.resolve(process.cwd(), options.moduleName);
var requestedModule = require(resolvedPath);
var component = options.exportName ? requestedModule[options.exportName] : requestedModule;
if (!component) {
throw new Error('The module "' + resolvedPath + '" has no export named "' + options.exportName + '"');
}
var history = createMemoryHistory(options.baseUrl);
var component = findReactComponent(options);
var history = createMemoryHistory(options.requestUrl);
var reactElement = React.createElement(component, { history: history });
var html = ReactDOMServer.renderToString(reactElement);
callback(null, html);

View File

@@ -0,0 +1,49 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Extensions;
namespace Microsoft.AspNet.NodeServices.React
{
[HtmlTargetElement(Attributes = PrerenderModuleAttributeName)]
public class ReactPrerenderTagHelper : TagHelper
{
static INodeServices fallbackNodeServices; // Used only if no INodeServices was registered with DI
const string PrerenderModuleAttributeName = "asp-react-prerender-module";
const string PrerenderExportAttributeName = "asp-react-prerender-export";
[HtmlAttributeName(PrerenderModuleAttributeName)]
public string ModuleName { get; set; }
[HtmlAttributeName(PrerenderExportAttributeName)]
public string ExportName { get; set; }
private IHttpContextAccessor contextAccessor;
private INodeServices nodeServices;
public ReactPrerenderTagHelper(IServiceProvider nodeServices, IHttpContextAccessor contextAccessor)
{
this.contextAccessor = contextAccessor;
this.nodeServices = (INodeServices)nodeServices.GetService(typeof (INodeServices)) ?? fallbackNodeServices;
// Consider removing the following. Having it means you can get away with not putting app.AddNodeServices()
// in your startup file, but then again it might be confusing that you don't need to.
if (this.nodeServices == null) {
this.nodeServices = fallbackNodeServices = Configuration.CreateNodeServices(NodeHostingModel.Http);
}
}
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var request = this.contextAccessor.HttpContext.Request;
var result = await ReactRenderer.RenderToString(
nodeServices: this.nodeServices,
componentModuleName: this.ModuleName,
componentExportName: this.ExportName,
requestUrl: request.Path + request.QueryString.Value);
output.Content.SetContentEncoded(result);
}
}
}

View File

@@ -12,15 +12,11 @@ namespace Microsoft.AspNet.NodeServices.React
nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
}
public static Task<string> RenderToString(INodeServices nodeServices, string moduleName, string baseUrl) {
return RenderToString(nodeServices, moduleName, /* exportName */ null, baseUrl);
}
public static async Task<string> RenderToString(INodeServices nodeServices, string moduleName, string exportName, string baseUrl) {
public static async Task<string> RenderToString(INodeServices nodeServices, string componentModuleName, string componentExportName, string requestUrl) {
return await nodeServices.InvokeExport(nodeScript.FileName, "renderToString", new {
moduleName,
exportName,
baseUrl
moduleName = componentModuleName,
exportName = componentExportName,
requestUrl = requestUrl
});
}
}

View File

@@ -1,5 +1,5 @@
{
"version": "1.0.0-alpha3",
"version": "1.0.0-alpha4",
"description": "Microsoft.AspNet.NodeServices.React Class Library",
"authors": [
"Microsoft"
@@ -25,7 +25,8 @@
}
},
"dependencies": {
"Microsoft.AspNet.NodeServices": "1.0.0-alpha3"
"Microsoft.AspNet.NodeServices": "1.0.0-alpha3",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta8"
},
"resource": [
"Content/**/*"