Skip to content

A service worker script for transpiling TypeScript to JavaScript on-the-fly

Notifications You must be signed in to change notification settings

TomasHubelbauer/sw-js-to-ts-transpiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 

Repository files navigation

DEMO

This is a TypeScript to JavaScript on-the-fly transpiler service worker script.

To use it, place this code in your application:

index.js:

const response = await fetch('https://tomashubelbauer.github.io/sw-js-to-ts-transpiler/worker.js');
const text = await response.text();
await navigator.serviceWorker.register('worker.js?' + btoa(text));

worker.js:

eval(atob(self.location.search.substring(1)));

Any request to a TypeScript file will then be transpiled on the fly:

const response = fetch('code.ts');
const text = await response.text();
// `text` is now the transpiled JavaScript!
<script src="code.ts"></script>
<!-- The transpiled JavaScript is executed by the `script` tag! -->

<script src="code.ts" type="module"></script>
<!-- ESM scripts also go through the service worker 👌 -->

About

A service worker script for transpiling TypeScript to JavaScript on-the-fly

Topics

Resources

Stars

Watchers

Forks