Skip to content

Commit e410aff

Browse files
Switch ES2015 example to use middleware inlined into Startup.cs instead of MVC controller/action
1 parent 0c59f67 commit e410aff

3 files changed

Lines changed: 21 additions & 35 deletions

File tree

samples/misc/ES2015Transpilation/Controllers/ScriptController.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

samples/misc/ES2015Transpilation/Startup.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using Microsoft.AspNet.Builder;
22
using Microsoft.AspNet.Hosting;
3-
using Microsoft.AspNet.Routing.Template;
43
using Microsoft.Dnx.Runtime;
54
using Microsoft.Framework.Configuration;
65
using Microsoft.Framework.DependencyInjection;
76
using Microsoft.Framework.Logging;
87
using Microsoft.AspNet.NodeServices;
8+
using Microsoft.AspNet.Http;
99

1010
namespace ES2015Example
1111
{
@@ -34,7 +34,7 @@ public void ConfigureServices(IServiceCollection services)
3434
}
3535

3636
// Configure is called after ConfigureServices is called.
37-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
37+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, INodeServices nodeServices)
3838
{
3939
loggerFactory.MinimumLevel = LogLevel.Information;
4040
loggerFactory.AddConsole();
@@ -57,13 +57,20 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
5757
app.UseExceptionHandler("/Home/Error");
5858
}
5959

60-
app.UseMvc(routes => {
61-
routes.MapRoute(
62-
name: "Script",
63-
template: "{*filename}",
64-
defaults: new { controller="Script", action="Transpile" },
65-
constraints: new { filename = @"js/(.*?)\.js" }
66-
);
60+
// Dynamically transpile any .js files under the '/js/' directory
61+
app.Use(next => async context => {
62+
var requestPath = context.Request.Path.Value;
63+
if (requestPath.StartsWith("/js/") && requestPath.EndsWith(".js")) {
64+
var fileInfo = env.WebRootFileProvider.GetFileInfo(requestPath);
65+
if (fileInfo.Exists) {
66+
var transpiled = await nodeServices.Invoke("transpilation.js", fileInfo.PhysicalPath, requestPath);
67+
await context.Response.WriteAsync(transpiled);
68+
return;
69+
}
70+
}
71+
72+
// Not a JS file, or doesn't exist - let some other middleware handle it
73+
await next.Invoke(context);
6774
});
6875

6976
// Add static files to the request pipeline.
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
var fs = require('fs');
12
var babelCore = require('babel-core');
23

3-
module.exports = function(cb, fileContents, url) {
4-
var result = babelCore.transform(fileContents, {
4+
module.exports = function(cb, physicalPath, requestPath) {
5+
var originalContents = fs.readFileSync(physicalPath);
6+
var result = babelCore.transform(originalContents, {
57
sourceMaps: 'inline',
6-
sourceFileName: '/sourcemapped/' + url
8+
sourceFileName: '/sourcemapped' + requestPath
79
});
810
cb(null, result.code);
911
}

0 commit comments

Comments
 (0)