vitejs / vite-plugin-vue

Vite Vue Plugins
MIT License
474 stars 150 forks source link

TypeScript types in pug templates get not removed #18

Open sschneider-ihre-pvs opened 2 years ago

sschneider-ihre-pvs commented 2 years ago

Describe the bug

Having something like

:filter-by="[keyName as string ?? 'key', valueName as string ?? 'value']"

in a pug template is fine with volar, but vite doesn't seem to expect that kind of thing and compiles it to js as is

"filter-by": [$props.keyName as string ?? 'key', $props.valueName as string ?? 'value'],

which leads to some cryptic error messages like

SyntaxError: missing ] after element list

Reproduction

Hello World

System Info

System:
    OS: Windows 10 10.0.19042
    CPU: (16) x64 Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
    Memory: 1.32 GB / 15.75 GB
  Binaries:
    Node: 16.14.2
    Yarn: 1.22.4
    npm: 8.5.0
  Browsers:
    Edge: Spartan (44.19041.1023.0), Chromium (98.0.1108.62)
    Internet Explorer: 11.0.19041.1
  npmPackages:
    @vitejs/plugin-vue: ^2.3.1 => 2.3.1

Used Package Manager

npm

Logs

No response

Validations

Shinigami92 commented 2 years ago

Is this a bug in Vite or in @vue/compiler-sfc?

sapphi-red commented 2 years ago

It seems a bug in Vite. When this condition is met, it is transpiled here. https://github.com/vitejs/vite/blob/f3d15f106f378c3850b62fbebd69fc8f7c7f944b/packages/plugin-vue/src/main.ts#L229 https://github.com/vitejs/vite/blob/f3d15f106f378c3850b62fbebd69fc8f7c7f944b/packages/plugin-vue/src/main.ts#L193-L208

But if it does not meet, it is not transpiled. https://github.com/vitejs/vite/blob/f3d15f106f378c3850b62fbebd69fc8f7c7f944b/packages/plugin-vue/src/main.ts#L238-L250

So in theory, this does not work. (I did not test)

<template src="foo.html"></template> <!-- foo.html includes typescript syntax -->
<script lang="ts" setup>
export const foo = 'foo'
</script>
Shinigami92 commented 2 years ago

Oh, so you would like to remove types from templates using src? I'm not sure if this is possible right now. Also that would not be a bug but a feature request, would it? 🤔 Feel free to open a PR and try to enhance this behavior.

sschneider-ihre-pvs commented 2 years ago

Well, currently it is assumed that every piece of js code in the template is vanilla js. I think that if you have script lang="ts" the probability is high that you have ts syntax in the template. It seems that the possibility is currently not checked, and the statements get copied to js code. Another way could be to indicate that you can only use vanilla js in html templates.

Shinigami92 commented 2 years ago

Well, currently it is assumed that every piece of js code in the template is vanilla js. I think that if you have script lang="ts" the probability is high that you have ts syntax in the template. It seems that the possibility is currently not checked, and the statements get copied to js code. Another way could be to indicate that you can only use vanilla js in html templates.

I specifically talk about template with using src I assume TypeScript types get removed if you use SFC with <script lang="ts"> together with <template lang="pug"> but without src for template

sapphi-red commented 2 years ago

I'm not the issuer (just in case).

When a <template> is written in html and is not using external src, it gets directly in the main module. Typescript support inside <template> is supported with intention. https://github.com/vitejs/vite/blob/f3d15f106f378c3850b62fbebd69fc8f7c7f944b/packages/plugin-vue/src/template.ts#L156-L162 Typescript support for <template> written in html and not using external src relies on this part. https://github.com/vitejs/vite/blob/f3d15f106f378c3850b62fbebd69fc8f7c7f944b/packages/plugin-vue/src/main.ts#L193-L208 But when this condition is not met, the code above does not run. https://github.com/vitejs/vite/blob/f3d15f106f378c3850b62fbebd69fc8f7c7f944b/packages/plugin-vue/src/main.ts#L229 So if a <template> uses external src or it is not written in html, it does not work with typescript.

Just wanted to say, it also does not work.

sschneider-ihre-pvs commented 2 years ago

Ah ok, then I misunderstood. And in my case I didn't use src and it didn't get removed.

sschneider-ihre-pvs commented 2 years ago

That would match my pug case.

So if a