11using Microsoft . AspNet . Builder ;
22using Microsoft . AspNet . Hosting ;
3- using Microsoft . AspNet . Routing . Template ;
43using Microsoft . Dnx . Runtime ;
54using Microsoft . Framework . Configuration ;
65using Microsoft . Framework . DependencyInjection ;
76using Microsoft . Framework . Logging ;
87using Microsoft . AspNet . NodeServices ;
8+ using Microsoft . AspNet . Http ;
99
1010namespace 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.
0 commit comments