LCB
LCB

Reputation: 1050

How to make vitejs compile class private fields to legacy code?

I am using vitejs and typescript and I want to set the compile target to es2016, but it seems that, the class private fields aren't be compiled.

class Task {
   #prepare() { // here should be compiled.
   }
}

I tried to set build.target in vite.config.js and compilerOptions.target in tsconfig and it doesn't work.

@vitejs/plugin-legacy doesn't transform the code either.

Upvotes: 2

Views: 421

Answers (1)

Ryuujo
Ryuujo

Reputation: 166

As of vite itself, the lowest supported target is es2015, aka es6.

You are expecting private class field in es6 to be transformed to legacy code, i.e. es5;

...and I want to set the compile target to es2016...

I would say no, and you don't want that.

Since 2016 > 2015 > 2009 (year of es5), all #private syntax are preserved, which is expected, per your configuration.

You should either find a proper vite / rollup plugin to transform your es6 code to es5 or lower.
Or you would find yourself hijacked by somewhat "legacy users", which is not important, and most (99%) of the users use chromium >= 84 or equivalent capable browsers today.

Upvotes: 0

Related Questions