Skip to content

Commit 60d77e7

Browse files
Add ES2015 runtime transpilation sample
1 parent f693bd6 commit 60d77e7

19 files changed

Lines changed: 233 additions & 0 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNet.Mvc;
3+
4+
namespace ES2015Example.Controllers
5+
{
6+
public class HomeController : Controller
7+
{
8+
public async Task<IActionResult> Index(int pageIndex)
9+
{
10+
return View();
11+
}
12+
13+
public IActionResult Error()
14+
{
15+
return View("~/Views/Shared/Error.cshtml");
16+
}
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNet.Mvc;
3+
using Microsoft.AspNet.NodeServices;
4+
5+
namespace ES2015Example.Controllers
6+
{
7+
public class ScriptController : Controller
8+
{
9+
private static NodeInstance nodeInstance = new NodeInstance();
10+
11+
public async Task<ContentResult> Transpile(string filename)
12+
{
13+
// TODO: Don't hard-code wwwroot; use proper path conversions
14+
var fileContents = System.IO.File.ReadAllText("wwwroot/" + filename);
15+
var transpiledResult = await nodeInstance.Invoke("transpilation.js", fileContents);
16+
return Content(transpiledResult, "application/javascript");
17+
}
18+
}
19+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Microsoft.AspNet.Builder;
2+
using Microsoft.AspNet.Hosting;
3+
using Microsoft.AspNet.Routing.Template;
4+
using Microsoft.Dnx.Runtime;
5+
using Microsoft.Framework.Configuration;
6+
using Microsoft.Framework.DependencyInjection;
7+
using Microsoft.Framework.Logging;
8+
9+
namespace ES2015Example
10+
{
11+
public class Startup
12+
{
13+
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
14+
{
15+
// Setup configuration sources.
16+
var builder = new ConfigurationBuilder()
17+
.SetBasePath(appEnv.ApplicationBasePath)
18+
.AddJsonFile("appsettings.json")
19+
.AddEnvironmentVariables();
20+
Configuration = builder.Build();
21+
}
22+
23+
public IConfigurationRoot Configuration { get; set; }
24+
25+
// This method gets called by the runtime.
26+
public void ConfigureServices(IServiceCollection services)
27+
{
28+
// Add MVC services to the services container.
29+
services.AddMvc();
30+
}
31+
32+
// Configure is called after ConfigureServices is called.
33+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
34+
{
35+
loggerFactory.MinimumLevel = LogLevel.Information;
36+
loggerFactory.AddConsole();
37+
loggerFactory.AddDebug();
38+
39+
// Configure the HTTP request pipeline.
40+
41+
// Add the platform handler to the request pipeline.
42+
app.UseIISPlatformHandler();
43+
44+
// Add the following to the request pipeline only in development environment.
45+
if (env.IsDevelopment())
46+
{
47+
app.UseDeveloperExceptionPage();
48+
}
49+
else
50+
{
51+
// Add Error handling middleware which catches all application specific errors and
52+
// send the request to the following path or controller action.
53+
app.UseExceptionHandler("/Home/Error");
54+
}
55+
56+
app.UseMvc(routes => {
57+
routes.MapRoute(
58+
name: "Script",
59+
template: "{*filename}",
60+
defaults: new { controller="Script", action="Transpile" },
61+
constraints: new { filename = @"js/(.*?)\.js" }
62+
);
63+
});
64+
65+
// Add static files to the request pipeline.
66+
app.UseStaticFiles();
67+
68+
// Add MVC to the request pipeline.
69+
app.UseMvc(routes =>
70+
{
71+
routes.MapRoute(
72+
name: "default",
73+
template: "{controller}/{action?}/{id?}",
74+
defaults: new { controller="Home", action = "Index" });
75+
});
76+
}
77+
}
78+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Hello
2+
3+
@section scripts {
4+
<script src="lib/system.js"></script>
5+
<script>System.import('js/main.js');</script>
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Error";
3+
}
4+
5+
<h1 class="text-danger">Error.</h1>
6+
<h2 class="text-danger">An error occurred while processing your request.</h2>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>ES2015 Example</title>
6+
</head>
7+
<body>
8+
@RenderBody()
9+
@RenderSection("scripts", required: false)
10+
</body>
11+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@using ES2015Example
2+
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
Layout = "_Layout";
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)