Switch ES2015 example to use middleware inlined into Startup.cs instead of MVC controller/action

This commit is contained in:
SteveSandersonMS
2015-11-02 21:02:47 -08:00
parent 0c59f670b2
commit e410affbd8
3 changed files with 21 additions and 35 deletions

View File

@@ -1,23 +0,0 @@
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.NodeServices;
namespace ES2015Example.Controllers
{
public class ScriptController : Controller
{
private INodeServices nodeServices;
public ScriptController(INodeServices nodeServices) {
this.nodeServices = nodeServices;
}
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 this.nodeServices.Invoke("transpilation.js", fileContents, Request.Path.Value);
return Content(transpiledResult, "application/javascript");
}
}
}

View File

@@ -1,11 +1,11 @@
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Routing.Template;
using Microsoft.Dnx.Runtime; using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Configuration; using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
using Microsoft.AspNet.NodeServices; using Microsoft.AspNet.NodeServices;
using Microsoft.AspNet.Http;
namespace ES2015Example namespace ES2015Example
{ {
@@ -34,7 +34,7 @@ namespace ES2015Example
} }
// Configure is called after ConfigureServices is called. // Configure is called after ConfigureServices is called.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, INodeServices nodeServices)
{ {
loggerFactory.MinimumLevel = LogLevel.Information; loggerFactory.MinimumLevel = LogLevel.Information;
loggerFactory.AddConsole(); loggerFactory.AddConsole();
@@ -57,13 +57,20 @@ namespace ES2015Example
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
} }
app.UseMvc(routes => { // Dynamically transpile any .js files under the '/js/' directory
routes.MapRoute( app.Use(next => async context => {
name: "Script", var requestPath = context.Request.Path.Value;
template: "{*filename}", if (requestPath.StartsWith("/js/") && requestPath.EndsWith(".js")) {
defaults: new { controller="Script", action="Transpile" }, var fileInfo = env.WebRootFileProvider.GetFileInfo(requestPath);
constraints: new { filename = @"js/(.*?)\.js" } if (fileInfo.Exists) {
); var transpiled = await nodeServices.Invoke("transpilation.js", fileInfo.PhysicalPath, requestPath);
await context.Response.WriteAsync(transpiled);
return;
}
}
// Not a JS file, or doesn't exist - let some other middleware handle it
await next.Invoke(context);
}); });
// Add static files to the request pipeline. // Add static files to the request pipeline.

View File

@@ -1,9 +1,11 @@
var fs = require('fs');
var babelCore = require('babel-core'); var babelCore = require('babel-core');
module.exports = function(cb, fileContents, url) { module.exports = function(cb, physicalPath, requestPath) {
var result = babelCore.transform(fileContents, { var originalContents = fs.readFileSync(physicalPath);
var result = babelCore.transform(originalContents, {
sourceMaps: 'inline', sourceMaps: 'inline',
sourceFileName: '/sourcemapped/' + url sourceFileName: '/sourcemapped' + requestPath
}); });
cb(null, result.code); cb(null, result.code);
} }