vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
67.18k stars 6.04k forks source link

Config "build.target" option documentation is confusing #16334

Open Lenni009 opened 5 months ago

Lenni009 commented 5 months ago

Documentation is

Explain in Detail

I'm currently investigating why Vite doesn't use the esnext target for the vite build command, even though it is set as target and module in tsconfig.json.

I had to set build.target to esnext in order for the build to succeed.

I expected that this would be pulled from tsconfig.json if it wasn't explicitly given in the vite.config.ts.

Vite's docs are not really helpful here.

https://vitejs.dev/guide/features#target:

Vite does not transpile TypeScript with the configured target value by default, following the same behaviour as esbuild.

The esbuild.target option can be used instead, which defaults to esnext for minimal transpilation. In builds, the build.target option takes higher priority and can also be set if needed.

In the warning box, there's this:

If target is not ESNext or ES2022 or newer, or if there's no tsconfig.json file [...]

Tl;dr: What I expect: If I set target in my tsconfig.json, build.target should follow that if build.target is undefined. What is actually happening: target in tsconfig.json has no effect on build.target. build.target must be explicitly set in order for the build to succeed.

Your Suggestion for Changes

- Vite does not transpile TypeScript with the configured target value by default
+ Vite does not transpile TypeScript to older ES standards by default

Something I'm not quite sure on: Does esbuild.target always use esnext if not specified otherwise, regardless of environment?

- which defaults to esnext for minimal transpilation.
+ which defaults to esnext during development for minimal transpilation.

The target environment is clearly different between dev and prod (dev targeting esnext, while prod uses es2020) -> should also be specified in that section (it's already more or less written down in https://vitejs.dev/guide/#browser-support and https://vitejs.dev/guide/build.html#browser-compatibility)

It should also be noted that build.target is not taken from tsconfig.json if build.target is undefined and target is defined in tsconfig.json.

It was also noted in #13756 that there should be only one single point to define target, which should be in the tsconfig.json. So maybe this should be made into a feature issue, since there's clearly demand for this feature.

Reproduction

https://github.com/Lenni009/vite-build-target-issue

Steps to reproduce

-> build fails

-> Build succeeds -> default build.target does not match target in tsconfig.json

oleksandr-danylchenko commented 4 months ago

What I expect: If I set target in my tsconfig.json, build.target should follow that if build.target is undefined. What is actually happening: target in tsconfig.json has no effect on build.target. build.target must be explicitly set in order for the build to succeed.

Thanks for pointing it out! I stumbled into the same issue myself today!

BearKid commented 2 months ago

I use create-vue to generate a sample project, which contains vite and typescript configuration files. I feel like I'm completely lost in the front-end build tools. Vite config file has build.target, and the tsconfig file has configuration items such as module and target., I can't figure out how the two work together.