Add ES2015 runtime transpilation sample

This commit is contained in:
SteveSandersonMS
2015-11-02 11:15:34 -08:00
parent f693bd60e3
commit 60d77e7b92
19 changed files with 233 additions and 0 deletions

View File

@@ -0,0 +1 @@
/node_modules/

View File

@@ -0,0 +1,18 @@
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
namespace ES2015Example.Controllers
{
public class HomeController : Controller
{
public async Task<IActionResult> Index(int pageIndex)
{
return View();
}
public IActionResult Error()
{
return View("~/Views/Shared/Error.cshtml");
}
}
}

View File

@@ -0,0 +1,19 @@
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.NodeServices;
namespace ES2015Example.Controllers
{
public class ScriptController : Controller
{
private static NodeInstance nodeInstance = new NodeInstance();
public async Task<ContentResult> Transpile(string filename)
{
// TODO: Don't hard-code wwwroot; use proper path conversions
var fileContents = System.IO.File.ReadAllText("wwwroot/" + filename);
var transpiledResult = await nodeInstance.Invoke("transpilation.js", fileContents);
return Content(transpiledResult, "application/javascript");
}
}
}

View File

@@ -0,0 +1,78 @@
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Routing.Template;
using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
namespace ES2015Example
{
public class Startup
{
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// Setup configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime.
public void ConfigureServices(IServiceCollection services)
{
// Add MVC services to the services container.
services.AddMvc();
}
// Configure is called after ConfigureServices is called.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.MinimumLevel = LogLevel.Information;
loggerFactory.AddConsole();
loggerFactory.AddDebug();
// Configure the HTTP request pipeline.
// Add the platform handler to the request pipeline.
app.UseIISPlatformHandler();
// Add the following to the request pipeline only in development environment.
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// Add Error handling middleware which catches all application specific errors and
// send the request to the following path or controller action.
app.UseExceptionHandler("/Home/Error");
}
app.UseMvc(routes => {
routes.MapRoute(
name: "Script",
template: "{*filename}",
defaults: new { controller="Script", action="Transpile" },
constraints: new { filename = @"js/(.*?)\.js" }
);
});
// Add static files to the request pipeline.
app.UseStaticFiles();
// Add MVC to the request pipeline.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action?}/{id?}",
defaults: new { controller="Home", action = "Index" });
});
}
}
}

View File

@@ -0,0 +1,6 @@
Hello
@section scripts {
<script src="lib/system.js"></script>
<script>System.import('js/main.js');</script>
}

View File

@@ -0,0 +1,6 @@
@{
ViewData["Title"] = "Error";
}
<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

View File

@@ -0,0 +1,11 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>ES2015 Example</title>
</head>
<body>
@RenderBody()
@RenderSection("scripts", required: false)
</body>
</html>

View File

@@ -0,0 +1,2 @@
@using ES2015Example
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"

View File

@@ -0,0 +1,3 @@
@{
Layout = "_Layout";
}

View File

@@ -0,0 +1 @@
{}

View File

@@ -0,0 +1,6 @@
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs"
}
}

View File

@@ -0,0 +1,9 @@
{
"name": "ES2015Example",
"version": "0.0.0",
"dependencies": {
"babel-core": "^5.8.29",
"body-parser": "^1.14.1",
"express": "^4.13.3"
}
}

View File

@@ -0,0 +1,45 @@
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"tooling": {
"defaultNamespace": "ES2015Example"
},
"dependencies": {
"Microsoft.AspNet.Diagnostics": "1.0.0-beta8",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-beta8",
"Microsoft.AspNet.Mvc": "6.0.0-beta8",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta8",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-beta8",
"Microsoft.AspNet.StaticFiles": "1.0.0-beta8",
"Microsoft.AspNet.Tooling.Razor": "1.0.0-beta8",
"Microsoft.Framework.Configuration.Json": "1.0.0-beta8",
"Microsoft.Framework.Logging": "1.0.0-beta8",
"Microsoft.Framework.Logging.Console": "1.0.0-beta8",
"Microsoft.Framework.Logging.Debug": "1.0.0-beta8",
"Microsoft.AspNet.NodeServices": "1.0.0-alpha1"
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
},
"frameworks": {
"dnx451": {},
"dnxcore50": {}
},
"exclude": [
"wwwroot",
"node_modules",
"bower_components"
],
"publishExclude": [
"node_modules",
"bower_components",
"**.xproj",
"**.user",
"**.vspscc"
],
"scripts": {
"prepublish": [
"npm install"
]
}
}

View File

@@ -0,0 +1,6 @@
var babelCore = require('babel-core');
module.exports = function(cb, fileContents) {
var result = babelCore.transform(fileContents, {});
cb(null, result.code);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

View File

@@ -0,0 +1,5 @@
export default class Greeting {
getMessage() {
return 'Hello from the ES2015 class';
}
}

View File

@@ -0,0 +1,3 @@
import Greeting from './greeting.js';
console.log(new Greeting().getMessage());

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
</handlers>
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false"/>
</system.webServer>
</configuration>