Brefer is a preprocessor to shorten the new Svelte 5 syntax for handling reactivity (hence the name "Brefer", made from "Bref" which means "Brief" in French and the suffix "er", which means "more").
npm install -D brefer
For PNPM and YARN, just replace npm install
with pnpm add
or yarn add
in the commands above.
Warning! Brefer is not yet ready for production (well, Svelte 5 neither). Expect bugs and breaking changes, as the syntax is not yet entirely decided.
The configuration is pretty easy:
// svelte.config.js
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";
import { breferPreprocess } from "brefer";
export default {
preprocess: [breferPreprocess(), vitePreprocess()]
};
If you don't want to use a Vite plugin and rather use Svelte's preprocess API, you can stop your configuration here and skip to the next paragraph.
The Brefer's Vite plugin allows you to also preprocess Svelte modules (.svelte.js
files).
// vite.config.js
import { defineConfig } from "vite";
import { brefer } from "brefer";
export default defineConfig({
// If you use other preprocessors, put brefer first
plugins: [brefer()]
});
You can pass options to the plugin. Those options contain 2 properties: include
and exclude
. You might already be familiar with them as a lot of other frameworks also use it.
You can check the documentation on Rollup's website.
// vite.config.js
import { defineConfig } from "vite";
import { brefer } from "brefer";
export default defineConfig({
plugins: [
brefer({
include: [
// Files to preprocess
"src/**/*.svelte",
"src/**/*.svelte.js"
],
exclude: [
// Files you don't want preprocessed
"tests/**/*.svelte"
]
})
]
});
What was your reaction when Rich Harris announced that Svelte 4's reactivity, which was as concise as a JS framework reactivity syntax could be, would be abandoned in favor of Vue Runes syntax?
If you were delighted, Brefer is probably not for you. Personally, I didn't want to write $state
and $derived
everytime I defined a new reactive variable. That's the reason I started this project.
With Brefer, I opted for a more straightforward syntax:
Variables defined with
let
are reactive by default
Using the rune
$(...)
creates$derived
values
Using the rune
$$(() => {...})
creates$effect
expressions
All subrunes for
$effect
(like$effect.root
for example) are usable with$$
That is (almost) all you have to know.
With Svelte 5 | With Brefer |
|
|
|
|
Brefer supports typescript out of the box as it uses @babel/parser to parse the script content of .svelte
files and .svelte.[js|ts]
modules.
To define non-reactive variables, you have 2 choices:
Use the var
or const
keywords
This choice is better for everyday use, e.g for temporary variables or loops
NB: if you use the var
keyword, Brefer will preprocess it to use let
instead
Use the $static
rune
This choice if better for when the first one can't be used, e.g for class properties which are defined without any keyword or when defining constants with
const
$derived.by
Brefer takes care of figuring out if you're using a function or an expression inside the $(...)
rune and will preprocess it to $derived
or $derived.by
depending on the result.
For very rare edges cases, this could cause bugs, especially with nested callbacks. As an example, if you do that:
let foo = "bar";
function yeet() {
return () => `Yeet the ${foo}`;
}
let baz = $(yeet());
Brefer will think you're trying to use an expression and will preprocess it to let baz = $derived(yeet())
even if $derived.by
should be used.
Keep that in mind if you don't want to waste hours trying to debug your non-working code.
NB: This bug can also occure with the $untrack
rune, so watch out.
$untrack
runeBrefer exposes an $untrack
rune so you don't have to import { untrack } from "svelte"
everytime. Brefer takes care of it all.
Moreover, you can pass reactive variables to $untrack
as a reference, no need to wrap it inside an arrow function. However, keep the problem mentionned in the previous paragraph about the potential bugs that it could cause.
Svelte 5 | Brefer |
|
|
$state
subrunesTo be able to define variables with $state.raw
or use $state.snapshot
given the shorten syntax for $state
's, Brefer exposes the $raw
and $snapshot
runes.
Use them just as you would use $state.raw
and $svelte.snapshot
.
.svelte.[js|ts]
)If you like the concept and want to contribute, feel free to open an issue or a pull request. Also, if you have any idea to improve or extend the syntax, I'm all ears (you can contact me on discord @kildesu)!