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.Hosting;
using Microsoft.AspNet.Routing.Template;
using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.AspNet.NodeServices;
using Microsoft.AspNet.Http;
namespace ES2015Example
{
@@ -34,7 +34,7 @@ namespace ES2015Example
}
// 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.AddConsole();
@@ -57,13 +57,20 @@ namespace ES2015Example
app.UseExceptionHandler("/Home/Error");
}
app.UseMvc(routes => {
routes.MapRoute(
name: "Script",
template: "{*filename}",
defaults: new { controller="Script", action="Transpile" },
constraints: new { filename = @"js/(.*?)\.js" }
);
// Dynamically transpile any .js files under the '/js/' directory
app.Use(next => async context => {
var requestPath = context.Request.Path.Value;
if (requestPath.StartsWith("/js/") && requestPath.EndsWith(".js")) {
var fileInfo = env.WebRootFileProvider.GetFileInfo(requestPath);
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.

View File

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