iTranslated by AI
Swdev: True No-bundle Frontend
Have you ever thought, "I wish TypeScript could run directly in the browser"? I have.
This makes it possible.

How it works
Service Workers can be thought of as a legal MITM, allowing you to rewrite fetch requests however you like.
- At Development
- Install the Service Worker during the initial installation.
- The Service Worker, which has a built-in compiler, rewrites requests to JS based on the file extension.
- Returns the file cached by the SW with
Content-Type: text/javascript. - Supports TypeScript (
.ts,.tsx) and Svelte (.svelte+ preprocess).
- Returns the file cached by the SW with
- Starts a WebSocket server. It monitors file changes and notifies the browser of modified filenames.
- Sends the modified files to the Service Worker, clears the SW cache, and reloads.
- At Production
- Generates a
main.bundle.jsresolved using the same rules as during development.
- Generates a
Here is a diagram of the development configuration:

The file monitoring process runs with Deno's deno run --allow-read=/ permission, making it secure as it cannot read outside the specified directories.
Yes, everything is written in Deno. Zero dependency on Node.js, including the build process.
Install and Run
# deno install - brew install deno
# See https://deno.land/#installation
$ deno install -qAf --unstable https://deno.land/x/swdev/swdev.ts
$ swdev init myapp
$ cd myapp
$ swdev serve
# production build
$ swdev build #=> main.bundle.js
# Release this directory on Netlify
# netlify deploy --prod -d .
Since it's actively under development, many things might change, but the steps to init, serve, and build will likely remain the same.
Hot reload
The entry point main.tsx introduces the following convention:
import App from "./App.svelte";
export default async () => {
// on start
const app = new App({ target: document.querySelector(".svelte-root") });
return async () => {
// on dispose
app.$destroy();
};
};
A function is provided as the default export, and the disposal logic is written as its return value. Every time a file changes, this disposal process is called before the function is re-executed.
Why I made it
Recently, "No bundle" tools like Snowpack and Vite have been trending. These pre-bundle dependencies for ESM and reference them from the browser as Native ESM. For production, they are built and distributed using tools like Rollup.
Using modern browser features only during development is the key, and I think this is why they are becoming accepted by developers.
Reference: Trends in Frontend Build Tools in the Native ESM Era
My concern with these tools was that they still require pre-setup, and in a true sense, they couldn't be called "No bundle." I wanted something that works with truly zero configuration.
The idea of dynamically rewriting files via a Service Worker has been in my mind for a long time. I implemented it three years ago. I felt that the time had finally caught up.
mizchi/trans-loader: webpack-less frontend with service-worker
This was sufficient, but looking at Vite and others, I realized that to execute a large number of transformations efficiently, I needed an implementation that caches while monitoring files and discards the cache upon changes. This is where Deno came in handy, allowing for file monitoring with restricted read permissions.
A current limitation is that Service Workers must be served from the root of the same host. Therefore, a Service Worker script is generated during initialization. In the future, I might embed this directly into the development server.
Regarding Service Worker Scope and Page Control - Qiita
Production build
Previously, I created uniroll, a Rollup extension that runs inside the browser. As part of that, I developed rollup-plugin-http-resolve, a plugin that can bundle imports targeting a network, such as import React from "https://cdn.esm.sh/react".
mizchi/uniroll: Opinionated universal frontend bundler in browser
uniroll/packages/rollup-plugin-http-resolve at master · mizchi/uniroll
During development, I use Skypack for the esm.sh ESM CDN, while for production, I use rollup-plugin-http-resolve for bundling. I also wrote a Rollup plugin for a file loader via Deno's FS API. I will extract this and publish it as a Deno plugin later.
Challenges: ESM Cache bursting
Service Workers cache targets imported with the same URL. When I tried to implement hot reload, the second import did not even reach the Service Worker's onfetch.
I couldn't find a way to discard this cache within the same process even after researching, and when I asked Chrome developers, they said there currently isn't a way.
To address this issue, I rewrote everything to add a ?${Math.random()} hash when returning requests for relative paths.
(Since this is currently a rough implementation using regular expressions, I will make it support other patterns like import "..." or export {...} from "..." later.)
It might look like the cache would be bypassed for all requests this way, but since the Service Worker internally queries the cache using the part excluding the ?..., it correctly retrieves the pre-built cache.
If I were to really put in more serious effort, I think calculating the dependency graph and rewriting by sending a hash like sha256 when a file changes would improve memory efficiency in the browser, but I've put that off for now. Currently, I'm relying on Chrome's GC.
Extension Idea: Inline Browser Editor
I'm considering implementing file writing via a WebSocket server, taking advantage of Deno's permission controls.
By launching an inline browser like monaco-editor, when a file is modified, it will issue a cache invalidation command to the SW while rewriting the file. This will allow control over host environment files from within the browser.
In this case, Deno's sandbox mechanism provides some protection against security concerns like disk traversal by malicious scripts. It might also be a good idea to restrict allowed request destinations with a whitelist at the SW level.
Furthermore, I'm thinking that through tools like WebRTC P2P or ngrok, it might be possible to set up a server where others can edit your FS simultaneously. Of course, this assumes use only among trusted individuals, as someone could potentially launch a DDoS from another person's environment.
An editor that completes entirely within the browser is something I've envisioned for a long time, so I definitely want to try it.
Extension Idea: Cloudflare Workers
As you can tell from the fact that it currently requires Deno installation, this isn't yet completely zero-configuration for "running TypeScript in the browser." To create a truly No Bundle environment where you don't even have to think about a compiler, I thought about not even requiring the installation of Deno scripts.
That's where Cloudflare Workers comes in, which has an interface similar to Service Workers and can proxy HTTP requests.
Although some kind of backend needs to be implemented to resolve the actual files, running swdev on Cloudflare Workers without even placing a Service Worker would realize a completely zero-configuration No Bundle Frontend. If production builds are generated at the time of the request, it could even support production.
Through this, you can make it appear as a "browser where TypeScript runs natively" under a specific host.
So, I've been thinking about what to use for the backend; while the built-in Workers KV has excellent performance, it's eventually consistent and not suited for high-frequency rewriting like an editor.
Therefore, I'm looking forward to the upcoming Durable Objects feature, thinking it might even allow for collaborative editing.
Workers Durable Objects Beta: A New Approach to Stateful Serverless
About Durable Objects in Cloudflare Workers
I applied for the beta, and received an email saying it would be out in 2021 Q1, but it hasn't come out yet... I'll think about this once it's released.
Conclusion
It can be used with simple commands, so please give it a try.
If you have any feature requests or ideas, please let me know via an Issue.
Discussion