From e410affbd86f79b4c5d05f8911fad217dac5e9c2 Mon Sep 17 00:00:00 2001 From: SteveSandersonMS Date: Mon, 2 Nov 2015 21:02:47 -0800 Subject: [PATCH] Switch ES2015 example to use middleware inlined into Startup.cs instead of MVC controller/action --- .../Controllers/ScriptController.cs | 23 ----------------- samples/misc/ES2015Transpilation/Startup.cs | 25 ++++++++++++------- .../misc/ES2015Transpilation/transpilation.js | 8 +++--- 3 files changed, 21 insertions(+), 35 deletions(-) delete mode 100644 samples/misc/ES2015Transpilation/Controllers/ScriptController.cs diff --git a/samples/misc/ES2015Transpilation/Controllers/ScriptController.cs b/samples/misc/ES2015Transpilation/Controllers/ScriptController.cs deleted file mode 100644 index 4b598be..0000000 --- a/samples/misc/ES2015Transpilation/Controllers/ScriptController.cs +++ /dev/null @@ -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 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"); - } - } -} diff --git a/samples/misc/ES2015Transpilation/Startup.cs b/samples/misc/ES2015Transpilation/Startup.cs index ea55a3f..a95bd1a 100755 --- a/samples/misc/ES2015Transpilation/Startup.cs +++ b/samples/misc/ES2015Transpilation/Startup.cs @@ -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. diff --git a/samples/misc/ES2015Transpilation/transpilation.js b/samples/misc/ES2015Transpilation/transpilation.js index ac239e5..972a4d7 100644 --- a/samples/misc/ES2015Transpilation/transpilation.js +++ b/samples/misc/ES2015Transpilation/transpilation.js @@ -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); }