Async Transform
This explainer is meant to show you how the Async Transform
Vite plugin works so that you can be
confident in using it. If you want to know how to setup the Vite plugin please go to the following
link.
What is a Vite plugin?
If you just want to know about what the Async Transform
does, feel free to skip to the next
paragraph. If you’re completely new to Vite plugins or just want to understand more about what they
are; here’s a brief introduction.
Introduction to Vite plugins
A Vite plugin is essentially a small bit of code with a series of methods that Vite will call sequentially during the dev and build script.
Every function is called at a specific moment of the build and can affect it in different ways.
Here’s an example of a simple Vite plugin that is very similar to one in their documentation
In this case we handle our own custom file extension and compile that to JS but you can also use the transform hook to change your JS.
This super simple plugin eliminates every instance of console.log()
from your code.
The gist of it is: you can do a lot of cool things with a Vite plugin and we decided to build the Async Transform to make your life (hopefully) easier!
How does the Async Transform
change my code?
The reason we are writing this explainer is because we firmly believe that if you allow your code to be changed by some script you should fully understand how it changes it and what the final result will look like.
As shown in our Mid run cancellation explainer, this Vite plugin aims to solve a couple of problems with async code in Typescript:
- Promises are not cancelable: once you start them there’s no way to stop the code in the middle of the function from executing.
- Generators can be “stopped” mid execution but Typescript doesn’t play very well with the
yield
keyword.
To fix these problems, we let you write the simple and more familiar async code, and we provide a Vite plugin to transform your async functions into generators!
As you can see we aim to touch your code the bare minimum. But I can still see the worry on your face, so let’s dive a bit more and make a couple of clarifications
What about my other async functions?
If you don’t want to see all of your beloved async functions be turned into generators, worry not,
because we specifically target only the async functions that are arguments of our own task
or
task.modifier
functions. And we were really careful with this so even if you have other functions
that are named task
or if you rename your import everything outside the argument of that function
will be left untouched.
and the same works even for imported functions with the same name.
However sometimes you might want to have a single function to create multiple tasks or maybe you want to have your tasks in a separate ts file. Well good news for you…
The transform
function
If you want to tell @sheepdog/svelte
that a function is meant to be transformed because you will
pass that function to a task you can do so by using the transform
function exported from
@sheepdog/svelte/utils
.
This function doesn’t do anything at runtime but it serves to purpose:
- As a marker for the vite plugin to transform the function you pass to it.
- as a “safety measure”. The function will actually throw in
DEV
if you use it without the Async Transform. This will warn you that what you expect to be a mid-run-cancellable function it is not because you forgot to add the vite plugin.
Here’s how a transformed function will look like
as you can see the function invocation was completely removed. This obviously works the same if you rename your import.