mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-24 10:40:23 +00:00
Rename packages. Bump versions to -alpha6
This commit is contained in:
1
Microsoft.AspNet.AngularServices/.gitignore
vendored
Normal file
1
Microsoft.AspNet.AngularServices/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/bin/
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Http.Extensions;
|
||||
using Microsoft.AspNet.NodeServices;
|
||||
using Microsoft.Dnx.Runtime;
|
||||
|
||||
namespace Microsoft.AspNet.AngularServices
|
||||
{
|
||||
[HtmlTargetElement(Attributes = PrerenderModuleAttributeName)]
|
||||
public class AngularPrerenderTagHelper : TagHelper
|
||||
{
|
||||
static INodeServices fallbackNodeServices; // Used only if no INodeServices was registered with DI
|
||||
|
||||
const string PrerenderModuleAttributeName = "asp-ng2-prerender-module";
|
||||
const string PrerenderExportAttributeName = "asp-ng2-prerender-export";
|
||||
|
||||
[HtmlAttributeName(PrerenderModuleAttributeName)]
|
||||
public string ModuleName { get; set; }
|
||||
|
||||
[HtmlAttributeName(PrerenderExportAttributeName)]
|
||||
public string ExportName { get; set; }
|
||||
|
||||
private IHttpContextAccessor contextAccessor;
|
||||
private INodeServices nodeServices;
|
||||
|
||||
public AngularPrerenderTagHelper(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 result = await AngularRenderer.RenderToString(
|
||||
nodeServices: this.nodeServices,
|
||||
componentModuleName: this.ModuleName,
|
||||
componentExportName: this.ExportName,
|
||||
componentTagName: output.TagName,
|
||||
requestUrl: UriHelper.GetEncodedUrl(this.contextAccessor.HttpContext.Request)
|
||||
);
|
||||
output.SuppressOutput();
|
||||
output.PostElement.AppendEncoded(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Microsoft.AspNet.AngularServices/AngularRenderer.cs
Normal file
25
Microsoft.AspNet.AngularServices/AngularRenderer.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.NodeServices;
|
||||
|
||||
namespace Microsoft.AspNet.AngularServices
|
||||
{
|
||||
public static class AngularRenderer
|
||||
{
|
||||
private static StringAsTempFile nodeScript;
|
||||
|
||||
static AngularRenderer() {
|
||||
// Consider populating this lazily
|
||||
var script = EmbeddedResourceReader.Read(typeof (AngularRenderer), "/Content/Node/angular-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 componentTagName, string requestUrl) {
|
||||
return await nodeServices.InvokeExport<string>(nodeScript.FileName, "renderToString", new {
|
||||
moduleName = componentModuleName,
|
||||
exportName = componentExportName,
|
||||
tagName = componentTagName,
|
||||
requestUrl = requestUrl
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
47
Microsoft.AspNet.AngularServices/Content/Node/angular-rendering.js
vendored
Normal file
47
Microsoft.AspNet.AngularServices/Content/Node/angular-rendering.js
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
var path = require('path');
|
||||
var ngUniversal = require('angular2-universal-patched');
|
||||
var ng = require('angular2/angular2');
|
||||
var ngRouter = require('angular2/router');
|
||||
|
||||
function getExportOrThrow(moduleInstance, moduleFilename, exportName) {
|
||||
if (!(exportName in moduleInstance)) {
|
||||
throw new Error('The module "' + moduleFilename + '" has no export named "' + exportName + '"');
|
||||
}
|
||||
return moduleInstance[exportName];
|
||||
}
|
||||
|
||||
function findAngularComponent(options) {
|
||||
var resolvedPath = path.resolve(process.cwd(), options.moduleName);
|
||||
var loadedModule = require(resolvedPath);
|
||||
if (options.exportName) {
|
||||
// If exportName is specified explicitly, use it
|
||||
return getExportOrThrow(loadedModule, resolvedPath, 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 {
|
||||
// Otherwise, guess the export name by converting tag-name to PascalCase
|
||||
var tagNameAsPossibleExport = options.tagName.replace(/(-|^)([a-z])/g, function (m1, m2, char) { return char.toUpperCase(); });
|
||||
return getExportOrThrow(loadedModule, resolvedPath, tagNameAsPossibleExport);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
renderToString: function(callback, options) {
|
||||
var component = findAngularComponent(options);
|
||||
var serverBindings = [
|
||||
ngRouter.ROUTER_BINDINGS,
|
||||
ngUniversal.HTTP_PROVIDERS,
|
||||
ng.provide(ngUniversal.BASE_URL, { useValue: options.requestUrl }),
|
||||
ngUniversal.SERVER_LOCATION_PROVIDERS
|
||||
];
|
||||
|
||||
return ngUniversal.renderToString(component, serverBindings).then(
|
||||
function(successValue) { callback(null, successValue); },
|
||||
function(errorValue) { callback(errorValue); }
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -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>421807e6-b62c-417b-b901-46c5dedaa8f1</ProjectGuid>
|
||||
<RootNamespace>Microsoft.AspNet.AngularServices</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>
|
||||
35
Microsoft.AspNet.AngularServices/project.json
Normal file
35
Microsoft.AspNet.AngularServices/project.json
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"version": "1.0.0-alpha6",
|
||||
"description": "Microsoft.AspNet.AngularServices Class Library",
|
||||
"authors": [
|
||||
"Microsoft"
|
||||
],
|
||||
"tags": [
|
||||
""
|
||||
],
|
||||
"projectUrl": "",
|
||||
"licenseUrl": "",
|
||||
"tooling": {
|
||||
"defaultNamespace": "Microsoft.AspNet.AngularServices"
|
||||
},
|
||||
"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