mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-23 01:58:29 +00:00
Rename packages. Bump versions to -alpha6
This commit is contained in:
1
Microsoft.AspNet.ReactServices/.gitignore
vendored
Normal file
1
Microsoft.AspNet.ReactServices/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/bin/
|
||||
54
Microsoft.AspNet.ReactServices/Content/Node/react-rendering.js
vendored
Normal file
54
Microsoft.AspNet.ReactServices/Content/Node/react-rendering.js
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var React = require('react');
|
||||
var ReactDOMServer = require('react-dom/server');
|
||||
var createMemoryHistory = require('history/lib/createMemoryHistory');
|
||||
var babelCore = require('babel-core');
|
||||
var babelConfig = {};
|
||||
|
||||
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
|
||||
// mode when the developer didn't expect that (e.g., current versions of underscore.js can't be loaded in strict mode).
|
||||
var useBabel = filename.indexOf('node_modules') < 0;
|
||||
if (useBabel) {
|
||||
var transformedFile = babelCore.transformFileSync(filename, babelConfig);
|
||||
return module._compile(transformedFile.code, filename);
|
||||
} else {
|
||||
return origJsLoader.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
renderToString: function(callback, options) {
|
||||
var component = findReactComponent(options);
|
||||
var history = createMemoryHistory(options.requestUrl);
|
||||
var reactElement = React.createElement(component, { history: history });
|
||||
var html = ReactDOMServer.renderToString(reactElement);
|
||||
callback(null, html);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>b04381de-991f-4831-a0b5-fe1bd3ef80c4</ProjectGuid>
|
||||
<RootNamespace>Microsoft.AspNet.ReactServices</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
51
Microsoft.AspNet.ReactServices/ReactPrerenderTagHelper.cs
Normal file
51
Microsoft.AspNet.ReactServices/ReactPrerenderTagHelper.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.NodeServices;
|
||||
using Microsoft.Dnx.Runtime;
|
||||
|
||||
namespace Microsoft.AspNet.ReactServices
|
||||
{
|
||||
[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 serviceProvider, IHttpContextAccessor contextAccessor)
|
||||
{
|
||||
this.contextAccessor = contextAccessor;
|
||||
this.nodeServices = (INodeServices)serviceProvider.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) {
|
||||
var appEnv = (IApplicationEnvironment)serviceProvider.GetService(typeof(IApplicationEnvironment));
|
||||
this.nodeServices = fallbackNodeServices = Configuration.CreateNodeServices(NodeHostingModel.Http, appEnv.ApplicationBasePath);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
24
Microsoft.AspNet.ReactServices/ReactRenderer.cs
Normal file
24
Microsoft.AspNet.ReactServices/ReactRenderer.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.NodeServices;
|
||||
|
||||
namespace Microsoft.AspNet.ReactServices
|
||||
{
|
||||
public static class ReactRenderer
|
||||
{
|
||||
private static StringAsTempFile nodeScript;
|
||||
|
||||
static ReactRenderer() {
|
||||
// Consider populating this lazily
|
||||
var script = EmbeddedResourceReader.Read(typeof (ReactRenderer), "/Content/Node/react-rendering.js");
|
||||
nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
|
||||
}
|
||||
|
||||
public static async Task<string> RenderToString(INodeServices nodeServices, string componentModuleName, string componentExportName, string requestUrl) {
|
||||
return await nodeServices.InvokeExport<string>(nodeScript.FileName, "renderToString", new {
|
||||
moduleName = componentModuleName,
|
||||
exportName = componentExportName,
|
||||
requestUrl = requestUrl
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Microsoft.AspNet.ReactServices/project.json
Normal file
35
Microsoft.AspNet.ReactServices/project.json
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"version": "1.0.0-alpha6",
|
||||
"description": "Microsoft.AspNet.ReactServices Class Library",
|
||||
"authors": [
|
||||
"Microsoft"
|
||||
],
|
||||
"tags": [
|
||||
""
|
||||
],
|
||||
"projectUrl": "",
|
||||
"licenseUrl": "",
|
||||
"tooling": {
|
||||
"defaultNamespace": "Microsoft.AspNet.ReactServices"
|
||||
},
|
||||
"frameworks": {
|
||||
"dnx451": {},
|
||||
"dnxcore50": {
|
||||
"dependencies": {
|
||||
"Microsoft.CSharp": "4.0.1-beta-*",
|
||||
"System.Collections": "4.0.11-beta-*",
|
||||
"System.Linq": "4.0.1-beta-*",
|
||||
"System.Runtime": "4.0.21-beta-*",
|
||||
"System.Threading": "4.0.11-beta-*"
|
||||
}
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.NodeServices": "1.0.0-alpha6",
|
||||
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta8",
|
||||
"Microsoft.Dnx.Runtime.Abstractions": "1.0.0-beta8"
|
||||
},
|
||||
"resource": [
|
||||
"Content/**/*"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user