Skip to content

Instantly share code, notes, and snippets.

@sliekens
Last active June 3, 2018 21:55
Show Gist options
  • Save sliekens/87f023305e910d9c6fa8ab96d7f92ac7 to your computer and use it in GitHub Desktop.
Save sliekens/87f023305e910d9c6fa8ab96d7f92ac7 to your computer and use it in GitHub Desktop.
Make IIS support extension-less imports
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");
}
}
}
}
<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>
export function square(num) {
return num * num;
}
<?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