Last active
June 3, 2018 21:55
-
-
Save sliekens/87f023305e910d9c6fa8ab96d7f92ac7 to your computer and use it in GitHub Desktop.
Make IIS support extension-less imports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ES6ModulesHttpModule : IHttpModule | |
{ | |
public void Dispose() | |
{ | |
} | |
public void Init(HttpApplication context) | |
{ | |
context.BeginRequest += ContextOnBeginRequest; | |
} | |
private void ContextOnBeginRequest(object sender, EventArgs e) | |
{ | |
var app = (HttpApplication)sender; | |
var moduleImport = app.Request.UrlReferrer?.ToString() | |
.EndsWith(".js", StringComparison.OrdinalIgnoreCase); | |
if (moduleImport.GetValueOrDefault()) | |
{ | |
var extensionless = !app.Request.Path.EndsWith(".js", StringComparison.OrdinalIgnoreCase); | |
if (extensionless) | |
{ | |
app.Response.RedirectPermanent(app.Request.Url + ".js"); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Using ES6 modules in IIS</title> | |
</head> | |
<body> | |
<script type="module" src="myModule.js"></script> | |
<script type="module"> | |
// extensionless import! | |
import { square } from './myModule'; | |
console.log('5² = ', square(5)); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function square(num) { | |
return num * num; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!-- | |
For more information on how to configure your ASP.NET application, please visit | |
https://go.microsoft.com/fwlink/?LinkId=301880 | |
--> | |
<configuration> | |
<system.webServer> | |
<modules> | |
<add name="esm" type="ES6ModulesHttpModule" /> | |
</modules> | |
</system.webServer> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment