Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f693bd6

Browse filesBrowse files
Initial state
1 parent 0e1fa2e commit f693bd6
Copy full SHA for f693bd6

File tree

Expand file treeCollapse file tree

110 files changed

+6722
-0
lines changed
Open diff view settings
Filter options

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree

110 files changed

+6722
-0
lines changed
Open diff view settings
Collapse file

‎.gitignore‎

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vs
2+
*.xproj.user
3+
project.lock.json
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Collapse file
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
3+
using Microsoft.AspNet.Http;
4+
using Microsoft.AspNet.NodeServices;
5+
using Microsoft.AspNet.Http.Extensions;
6+
7+
namespace Microsoft.AspNet.NodeServices.Angular
8+
{
9+
[HtmlTargetElement(Attributes = PrerenderModuleAttributeName)]
10+
public class AngularRunAtServerTagHelper : TagHelper
11+
{
12+
static StringAsTempFile nodeScript;
13+
14+
static AngularRunAtServerTagHelper() {
15+
// Consider populating this lazily
16+
var script = EmbeddedResourceReader.Read(typeof (AngularRunAtServerTagHelper), "/Content/Node/angular-rendering.js");
17+
nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
18+
}
19+
20+
const string PrerenderModuleAttributeName = "aspnet-ng2-prerender-module";
21+
const string PrerenderExportAttributeName = "aspnet-ng2-prerender-export";
22+
23+
private static NodeInstance nodeInstance = new NodeInstance();
24+
25+
[HtmlAttributeName(PrerenderModuleAttributeName)]
26+
public string ModuleName { get; set; }
27+
28+
[HtmlAttributeName(PrerenderExportAttributeName)]
29+
public string ExportName { get; set; }
30+
31+
private IHttpContextAccessor contextAccessor;
32+
33+
public AngularRunAtServerTagHelper(IHttpContextAccessor contextAccessor)
34+
{
35+
this.contextAccessor = contextAccessor;
36+
}
37+
38+
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
39+
{
40+
var result = await nodeInstance.InvokeExport(nodeScript.FileName, "renderComponent", new {
41+
componentModule = this.ModuleName,
42+
componentExport = this.ExportName,
43+
tagName = output.TagName,
44+
baseUrl = UriHelper.GetEncodedUrl(this.contextAccessor.HttpContext.Request)
45+
});
46+
output.SuppressOutput();
47+
output.PostElement.AppendEncoded(result);
48+
}
49+
}
50+
}
Collapse file
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var path = require('path');
2+
var ngUniversal = require('angular2-universal-patched');
3+
var ng = require('angular2/angular2');
4+
var ngRouter = require('angular2/router');
5+
6+
module.exports = {
7+
renderComponent: function(callback, options) {
8+
// Find the component class. Use options.componentExport if specified, otherwise convert tag-name to PascalCase.
9+
var loadedModule = require(path.resolve(process.cwd(), options.componentModule));
10+
var componentExport = options.componentExport || options.tagName.replace(/(-|^)([a-z])/g, function (m1, m2, char) { return char.toUpperCase(); });
11+
var component = loadedModule[componentExport];
12+
if (!component) {
13+
throw new Error('The module "' + options.componentModule + '" has no export named "' + componentExport + '"');
14+
}
15+
16+
var serverBindings = [
17+
ngRouter.ROUTER_BINDINGS,
18+
ngUniversal.HTTP_PROVIDERS,
19+
ng.provide(ngUniversal.BASE_URL, { useValue: options.baseUrl }),
20+
ngUniversal.SERVER_LOCATION_PROVIDERS
21+
];
22+
23+
return ngUniversal.renderToString(component, serverBindings).then(
24+
function(successValue) { callback(null, successValue); },
25+
function(errorValue) { callback(errorValue); }
26+
);
27+
}
28+
};
Collapse file
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"version": "1.0.0-alpha1",
3+
"description": "Microsoft.AspNet.NodeServices.Angular Class Library",
4+
"authors": [
5+
"Microsoft"
6+
],
7+
"tags": [
8+
""
9+
],
10+
"projectUrl": "",
11+
"licenseUrl": "",
12+
"tooling": {
13+
"defaultNamespace": "Microsoft.AspNet.NodeServices.Angular"
14+
},
15+
"frameworks": {
16+
"dnx451": {},
17+
"dnxcore50": {
18+
"dependencies": {
19+
"Microsoft.CSharp": "4.0.1-beta-*",
20+
"System.Collections": "4.0.11-beta-*",
21+
"System.Linq": "4.0.1-beta-*",
22+
"System.Runtime": "4.0.21-beta-*",
23+
"System.Threading": "4.0.11-beta-*"
24+
}
25+
}
26+
},
27+
"dependencies": {
28+
"Microsoft.AspNet.NodeServices": "1.0.0-alpha1",
29+
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta8"
30+
},
31+
"resource": [
32+
"Content/**/*"
33+
]
34+
}
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Collapse file
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var React = require('react');
4+
var ReactDOMServer = require('react-dom/server');
5+
var createMemoryHistory = require('history/lib/createMemoryHistory');
6+
var babelCore = require('babel-core');
7+
var babelConfig = {};
8+
9+
var origJsLoader = require.extensions['.js'];
10+
require.extensions['.js'] = loadViaBabel;
11+
require.extensions['.jsx'] = loadViaBabel;
12+
13+
function loadViaBabel(module, filename) {
14+
// Assume that all the app's own code is ES2015+ (optionally with JSX), but that none of the node_modules are.
15+
// The distinction is important because ES2015+ forces strict mode, and it may break ES3/5 if you try to run it in strict
16+
// mode when the developer didn't expect that (e.g., current versions of underscore.js can't be loaded in strict mode).
17+
var useBabel = filename.indexOf('node_modules') < 0;
18+
if (useBabel) {
19+
var transformedFile = babelCore.transformFileSync(filename, babelConfig);
20+
return module._compile(transformedFile.code, filename);
21+
} else {
22+
return origJsLoader.apply(this, arguments);
23+
}
24+
}
25+
26+
module.exports = {
27+
renderToString: function(callback, options) {
28+
var resolvedPath = path.resolve(process.cwd(), options.moduleName);
29+
var requestedModule = require(resolvedPath);
30+
var component = requestedModule[options.exportName];
31+
if (!component) {
32+
throw new Error('The module "' + resolvedPath + '" has no export named "' + options.exportName + '"');
33+
}
34+
35+
var history = createMemoryHistory(options.baseUrl);
36+
var reactElement = React.createElement(component, { history: history });
37+
var html = ReactDOMServer.renderToString(reactElement);
38+
callback(null, html);
39+
}
40+
};
Collapse file
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Threading.Tasks;
2+
3+
namespace Microsoft.AspNet.NodeServices.React
4+
{
5+
public static class ReactRenderer
6+
{
7+
private static StringAsTempFile nodeScript;
8+
private static NodeInstance nodeInstance = new NodeInstance();
9+
10+
static ReactRenderer() {
11+
// Consider populating this lazily
12+
var script = EmbeddedResourceReader.Read(typeof (ReactRenderer), "/Content/Node/react-rendering.js");
13+
nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
14+
}
15+
16+
public static async Task<string> RenderToString(string moduleName, string exportName, string baseUrl) {
17+
return await nodeInstance.InvokeExport(nodeScript.FileName, "renderToString", new {
18+
moduleName,
19+
exportName,
20+
baseUrl
21+
});
22+
}
23+
}
24+
}
Collapse file
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"version": "1.0.0-alpha1",
3+
"description": "Microsoft.AspNet.NodeServices.React Class Library",
4+
"authors": [
5+
"Microsoft"
6+
],
7+
"tags": [
8+
""
9+
],
10+
"projectUrl": "",
11+
"licenseUrl": "",
12+
"tooling": {
13+
"defaultNamespace": "Microsoft.AspNet.NodeServices.React"
14+
},
15+
"frameworks": {
16+
"dnx451": {},
17+
"dnxcore50": {
18+
"dependencies": {
19+
"Microsoft.CSharp": "4.0.1-beta-*",
20+
"System.Collections": "4.0.11-beta-*",
21+
"System.Linq": "4.0.1-beta-*",
22+
"System.Runtime": "4.0.21-beta-*",
23+
"System.Threading": "4.0.11-beta-*"
24+
}
25+
}
26+
},
27+
"dependencies": {
28+
"Microsoft.AspNet.NodeServices": "1.0.0-alpha1"
29+
},
30+
"resource": [
31+
"Content/**/*"
32+
]
33+
}
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.