DEV Community

Cover image for ECMAScript decorators with Vite
Kiran Mantha
Kiran Mantha

Posted on

6

ECMAScript decorators with Vite

I felt very happy when i saw the progress on bringing typescript decorators to vanillajs. I thought to give it a try in my vanillajs + vite application and bammmmm 💥 it broke 🤯 🙇‍♂️.

ESbuild, which comes with OOB of vite, doesn't support decorators yet 🙄 🤷

That only left me the option to go for babel but i don't want to get away from my love, vite 🥺

Then i found the vite-plugin-babel package and made few changes to vite config. It worked wohooooo 🤩

Here's the config:

import { defineConfig } from 'vite';
import babel from 'vite-plugin-babel';

export default defineConfig({
  base: "./",
  plugins: [
    babel({
      babelConfig: {
        babelrc: false,
        configFile: false,
        plugins: [
          [
            "@babel/plugin-proposal-decorators",
            { loose: true, version: "2022-03" },
          ],
        ],
      },
    }),
  ],
  server: {
    host: true,
    port: 3001,
    open: "/",
  },
});
Enter fullscreen mode Exit fullscreen mode

That's it. This resolved my issue and i happily went on using decorators in javascript.

FYI, decorator proposal is in stage3 and one step behind of getting finalized.

Thanks for the read,
Kiran 👋 👋

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (5)

Collapse
 
przyb profile image
Jakub Przyborowski

Hi @kiranmantha, Kuba here from the team behind this tiny plugin. Thanks for using vite-plugin-babel and spreading a good word about it! :) Cheers! 🙌

Collapse
 
naucode profile image
Al - Naucode

Great article, you got my follow, keep writing!

Collapse
 
kiranmantha profile image
Kiran Mantha

Thanks alot @naubit . 😊

Collapse
 
daria_selezneva_af738b49f profile image
Daria Selezneva

Cannot start nuxt: Invalid plugin. src option is required: {"name":"babel-plugin","enforce":"pre"}

Collapse
 
kiranmantha profile image
Kiran Mantha

Can you provide me ur git repo.. I'll check it out

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay