mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-22 17:47:53 +00:00
Add ES2015 runtime transpilation sample
This commit is contained in:
1
samples/misc/ES2015Transpilation/.gitignore
vendored
Normal file
1
samples/misc/ES2015Transpilation/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/node_modules/
|
||||
18
samples/misc/ES2015Transpilation/Controllers/HomeController.cs
Executable file
18
samples/misc/ES2015Transpilation/Controllers/HomeController.cs
Executable file
@@ -0,0 +1,18 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
|
||||
namespace ES2015Example.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
public async Task<IActionResult> Index(int pageIndex)
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
public IActionResult Error()
|
||||
{
|
||||
return View("~/Views/Shared/Error.cshtml");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.NodeServices;
|
||||
|
||||
namespace ES2015Example.Controllers
|
||||
{
|
||||
public class ScriptController : Controller
|
||||
{
|
||||
private static NodeInstance nodeInstance = new NodeInstance();
|
||||
|
||||
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 nodeInstance.Invoke("transpilation.js", fileContents);
|
||||
return Content(transpiledResult, "application/javascript");
|
||||
}
|
||||
}
|
||||
}
|
||||
78
samples/misc/ES2015Transpilation/Startup.cs
Executable file
78
samples/misc/ES2015Transpilation/Startup.cs
Executable file
@@ -0,0 +1,78 @@
|
||||
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;
|
||||
|
||||
namespace ES2015Example
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
|
||||
{
|
||||
// Setup configuration sources.
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(appEnv.ApplicationBasePath)
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddEnvironmentVariables();
|
||||
Configuration = builder.Build();
|
||||
}
|
||||
|
||||
public IConfigurationRoot Configuration { get; set; }
|
||||
|
||||
// This method gets called by the runtime.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
// Add MVC services to the services container.
|
||||
services.AddMvc();
|
||||
}
|
||||
|
||||
// Configure is called after ConfigureServices is called.
|
||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||
{
|
||||
loggerFactory.MinimumLevel = LogLevel.Information;
|
||||
loggerFactory.AddConsole();
|
||||
loggerFactory.AddDebug();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
||||
// Add the platform handler to the request pipeline.
|
||||
app.UseIISPlatformHandler();
|
||||
|
||||
// Add the following to the request pipeline only in development environment.
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Add Error handling middleware which catches all application specific errors and
|
||||
// send the request to the following path or controller action.
|
||||
app.UseExceptionHandler("/Home/Error");
|
||||
}
|
||||
|
||||
app.UseMvc(routes => {
|
||||
routes.MapRoute(
|
||||
name: "Script",
|
||||
template: "{*filename}",
|
||||
defaults: new { controller="Script", action="Transpile" },
|
||||
constraints: new { filename = @"js/(.*?)\.js" }
|
||||
);
|
||||
});
|
||||
|
||||
// Add static files to the request pipeline.
|
||||
app.UseStaticFiles();
|
||||
|
||||
// Add MVC to the request pipeline.
|
||||
app.UseMvc(routes =>
|
||||
{
|
||||
routes.MapRoute(
|
||||
name: "default",
|
||||
template: "{controller}/{action?}/{id?}",
|
||||
defaults: new { controller="Home", action = "Index" });
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
6
samples/misc/ES2015Transpilation/Views/Home/Index.cshtml
Executable file
6
samples/misc/ES2015Transpilation/Views/Home/Index.cshtml
Executable file
@@ -0,0 +1,6 @@
|
||||
Hello
|
||||
|
||||
@section scripts {
|
||||
<script src="lib/system.js"></script>
|
||||
<script>System.import('js/main.js');</script>
|
||||
}
|
||||
6
samples/misc/ES2015Transpilation/Views/Shared/Error.cshtml
Executable file
6
samples/misc/ES2015Transpilation/Views/Shared/Error.cshtml
Executable file
@@ -0,0 +1,6 @@
|
||||
@{
|
||||
ViewData["Title"] = "Error";
|
||||
}
|
||||
|
||||
<h1 class="text-danger">Error.</h1>
|
||||
<h2 class="text-danger">An error occurred while processing your request.</h2>
|
||||
11
samples/misc/ES2015Transpilation/Views/Shared/_Layout.cshtml
Executable file
11
samples/misc/ES2015Transpilation/Views/Shared/_Layout.cshtml
Executable file
@@ -0,0 +1,11 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>ES2015 Example</title>
|
||||
</head>
|
||||
<body>
|
||||
@RenderBody()
|
||||
@RenderSection("scripts", required: false)
|
||||
</body>
|
||||
</html>
|
||||
2
samples/misc/ES2015Transpilation/Views/_ViewImports.cshtml
Executable file
2
samples/misc/ES2015Transpilation/Views/_ViewImports.cshtml
Executable file
@@ -0,0 +1,2 @@
|
||||
@using ES2015Example
|
||||
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
|
||||
3
samples/misc/ES2015Transpilation/Views/_ViewStart.cshtml
Executable file
3
samples/misc/ES2015Transpilation/Views/_ViewStart.cshtml
Executable file
@@ -0,0 +1,3 @@
|
||||
@{
|
||||
Layout = "_Layout";
|
||||
}
|
||||
1
samples/misc/ES2015Transpilation/appsettings.json
Executable file
1
samples/misc/ES2015Transpilation/appsettings.json
Executable file
@@ -0,0 +1 @@
|
||||
{}
|
||||
6
samples/misc/ES2015Transpilation/jsconfig.json
Normal file
6
samples/misc/ES2015Transpilation/jsconfig.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES6",
|
||||
"module": "commonjs"
|
||||
}
|
||||
}
|
||||
9
samples/misc/ES2015Transpilation/package.json
Normal file
9
samples/misc/ES2015Transpilation/package.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "ES2015Example",
|
||||
"version": "0.0.0",
|
||||
"dependencies": {
|
||||
"babel-core": "^5.8.29",
|
||||
"body-parser": "^1.14.1",
|
||||
"express": "^4.13.3"
|
||||
}
|
||||
}
|
||||
45
samples/misc/ES2015Transpilation/project.json
Executable file
45
samples/misc/ES2015Transpilation/project.json
Executable file
@@ -0,0 +1,45 @@
|
||||
{
|
||||
"webroot": "wwwroot",
|
||||
"version": "1.0.0-*",
|
||||
"tooling": {
|
||||
"defaultNamespace": "ES2015Example"
|
||||
},
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Diagnostics": "1.0.0-beta8",
|
||||
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-beta8",
|
||||
"Microsoft.AspNet.Mvc": "6.0.0-beta8",
|
||||
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta8",
|
||||
"Microsoft.AspNet.Server.Kestrel": "1.0.0-beta8",
|
||||
"Microsoft.AspNet.StaticFiles": "1.0.0-beta8",
|
||||
"Microsoft.AspNet.Tooling.Razor": "1.0.0-beta8",
|
||||
"Microsoft.Framework.Configuration.Json": "1.0.0-beta8",
|
||||
"Microsoft.Framework.Logging": "1.0.0-beta8",
|
||||
"Microsoft.Framework.Logging.Console": "1.0.0-beta8",
|
||||
"Microsoft.Framework.Logging.Debug": "1.0.0-beta8",
|
||||
"Microsoft.AspNet.NodeServices": "1.0.0-alpha1"
|
||||
},
|
||||
"commands": {
|
||||
"web": "Microsoft.AspNet.Server.Kestrel"
|
||||
},
|
||||
"frameworks": {
|
||||
"dnx451": {},
|
||||
"dnxcore50": {}
|
||||
},
|
||||
"exclude": [
|
||||
"wwwroot",
|
||||
"node_modules",
|
||||
"bower_components"
|
||||
],
|
||||
"publishExclude": [
|
||||
"node_modules",
|
||||
"bower_components",
|
||||
"**.xproj",
|
||||
"**.user",
|
||||
"**.vspscc"
|
||||
],
|
||||
"scripts": {
|
||||
"prepublish": [
|
||||
"npm install"
|
||||
]
|
||||
}
|
||||
}
|
||||
6
samples/misc/ES2015Transpilation/transpilation.js
Normal file
6
samples/misc/ES2015Transpilation/transpilation.js
Normal file
@@ -0,0 +1,6 @@
|
||||
var babelCore = require('babel-core');
|
||||
|
||||
module.exports = function(cb, fileContents) {
|
||||
var result = babelCore.transform(fileContents, {});
|
||||
cb(null, result.code);
|
||||
}
|
||||
BIN
samples/misc/ES2015Transpilation/wwwroot/favicon.ico
Executable file
BIN
samples/misc/ES2015Transpilation/wwwroot/favicon.ico
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 31 KiB |
5
samples/misc/ES2015Transpilation/wwwroot/js/greeting.js
Normal file
5
samples/misc/ES2015Transpilation/wwwroot/js/greeting.js
Normal file
@@ -0,0 +1,5 @@
|
||||
export default class Greeting {
|
||||
getMessage() {
|
||||
return 'Hello from the ES2015 class';
|
||||
}
|
||||
}
|
||||
3
samples/misc/ES2015Transpilation/wwwroot/js/main.js
Normal file
3
samples/misc/ES2015Transpilation/wwwroot/js/main.js
Normal file
@@ -0,0 +1,3 @@
|
||||
import Greeting from './greeting.js';
|
||||
|
||||
console.log(new Greeting().getMessage());
|
||||
5
samples/misc/ES2015Transpilation/wwwroot/lib/system.js
Normal file
5
samples/misc/ES2015Transpilation/wwwroot/lib/system.js
Normal file
File diff suppressed because one or more lines are too long
9
samples/misc/ES2015Transpilation/wwwroot/web.config
Normal file
9
samples/misc/ES2015Transpilation/wwwroot/web.config
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<system.webServer>
|
||||
<handlers>
|
||||
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
|
||||
</handlers>
|
||||
<httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false"/>
|
||||
</system.webServer>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user