mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-24 02:30:13 +00:00
Initial state
This commit is contained in:
1
Microsoft.AspNet.NodeServices.Angular/.gitignore
vendored
Normal file
1
Microsoft.AspNet.NodeServices.Angular/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/bin/
|
||||
@@ -0,0 +1,50 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.NodeServices;
|
||||
using Microsoft.AspNet.Http.Extensions;
|
||||
|
||||
namespace Microsoft.AspNet.NodeServices.Angular
|
||||
{
|
||||
[HtmlTargetElement(Attributes = PrerenderModuleAttributeName)]
|
||||
public class AngularRunAtServerTagHelper : TagHelper
|
||||
{
|
||||
static StringAsTempFile nodeScript;
|
||||
|
||||
static AngularRunAtServerTagHelper() {
|
||||
// Consider populating this lazily
|
||||
var script = EmbeddedResourceReader.Read(typeof (AngularRunAtServerTagHelper), "/Content/Node/angular-rendering.js");
|
||||
nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
|
||||
}
|
||||
|
||||
const string PrerenderModuleAttributeName = "aspnet-ng2-prerender-module";
|
||||
const string PrerenderExportAttributeName = "aspnet-ng2-prerender-export";
|
||||
|
||||
private static NodeInstance nodeInstance = new NodeInstance();
|
||||
|
||||
[HtmlAttributeName(PrerenderModuleAttributeName)]
|
||||
public string ModuleName { get; set; }
|
||||
|
||||
[HtmlAttributeName(PrerenderExportAttributeName)]
|
||||
public string ExportName { get; set; }
|
||||
|
||||
private IHttpContextAccessor contextAccessor;
|
||||
|
||||
public AngularRunAtServerTagHelper(IHttpContextAccessor contextAccessor)
|
||||
{
|
||||
this.contextAccessor = contextAccessor;
|
||||
}
|
||||
|
||||
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
var result = await nodeInstance.InvokeExport(nodeScript.FileName, "renderComponent", new {
|
||||
componentModule = this.ModuleName,
|
||||
componentExport = this.ExportName,
|
||||
tagName = output.TagName,
|
||||
baseUrl = UriHelper.GetEncodedUrl(this.contextAccessor.HttpContext.Request)
|
||||
});
|
||||
output.SuppressOutput();
|
||||
output.PostElement.AppendEncoded(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Microsoft.AspNet.NodeServices.Angular/Content/Node/angular-rendering.js
vendored
Normal file
28
Microsoft.AspNet.NodeServices.Angular/Content/Node/angular-rendering.js
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
var path = require('path');
|
||||
var ngUniversal = require('angular2-universal-patched');
|
||||
var ng = require('angular2/angular2');
|
||||
var ngRouter = require('angular2/router');
|
||||
|
||||
module.exports = {
|
||||
renderComponent: function(callback, options) {
|
||||
// Find the component class. Use options.componentExport if specified, otherwise convert tag-name to PascalCase.
|
||||
var loadedModule = require(path.resolve(process.cwd(), options.componentModule));
|
||||
var componentExport = options.componentExport || options.tagName.replace(/(-|^)([a-z])/g, function (m1, m2, char) { return char.toUpperCase(); });
|
||||
var component = loadedModule[componentExport];
|
||||
if (!component) {
|
||||
throw new Error('The module "' + options.componentModule + '" has no export named "' + componentExport + '"');
|
||||
}
|
||||
|
||||
var serverBindings = [
|
||||
ngRouter.ROUTER_BINDINGS,
|
||||
ngUniversal.HTTP_PROVIDERS,
|
||||
ng.provide(ngUniversal.BASE_URL, { useValue: options.baseUrl }),
|
||||
ngUniversal.SERVER_LOCATION_PROVIDERS
|
||||
];
|
||||
|
||||
return ngUniversal.renderToString(component, serverBindings).then(
|
||||
function(successValue) { callback(null, successValue); },
|
||||
function(errorValue) { callback(errorValue); }
|
||||
);
|
||||
}
|
||||
};
|
||||
34
Microsoft.AspNet.NodeServices.Angular/project.json
Normal file
34
Microsoft.AspNet.NodeServices.Angular/project.json
Normal file
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"version": "1.0.0-alpha1",
|
||||
"description": "Microsoft.AspNet.NodeServices.Angular Class Library",
|
||||
"authors": [
|
||||
"Microsoft"
|
||||
],
|
||||
"tags": [
|
||||
""
|
||||
],
|
||||
"projectUrl": "",
|
||||
"licenseUrl": "",
|
||||
"tooling": {
|
||||
"defaultNamespace": "Microsoft.AspNet.NodeServices.Angular"
|
||||
},
|
||||
"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-alpha1",
|
||||
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta8"
|
||||
},
|
||||
"resource": [
|
||||
"Content/**/*"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user