-
-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript types in pug templates get not removed #18
Comments
Is this a bug in Vite or in |
It seems a bug in Vite. But if it does not meet, it is not transpiled. 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> |
Oh, so you would like to remove types from |
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. |
I specifically talk about |
I'm not the issuer (just in case). When a Just wanted to say, it also does not work. |
Ah ok, then I misunderstood. And in my case I didn't use src and it didn't get removed. |
That would match my pug case.
|
This is the repro with pug and without external |
thanks for the repo, I added that to the report. |
can confirm I'm experiencing the same bug without |
In my project, when I use the combination of API options, Pug, and Vite for building, I encounter an unexpected token error due to TypeScript type assertion in the Pug template. However, when I switch to the Composition API, the error disappears. For someone in need, I've created a Vite plugin to remove type assertions. This may help some Pug users. I'm uncertain if there are other TypeScript syntax will break Vite, but removing type assertions will solve my problem.
import fs from "fs"
import type { Plugin } from "vite"
const vitePluginPugBuild = (): Plugin => {
return {
name: "vite-plugin-remove-pug-type-assertion",
enforce: "pre",
apply: "build",
load(id: string) {
if (!id.endsWith(".vue")) return
let content = fs.readFileSync(id, "utf-8")
// Remove type assertion in pug block for vue files.
content = content.replace(/(?<==".+)( as [\w<>|', ]+)/g, '')
return content
},
}
}
export default function() {
return vitePluginPugBuild()
} Config your import vue from '@vitejs/plugin-vue'
import removePugAssertion from '[some path]/vite-plugin-remove-pug-type-assertion'
export default defineConfig({
plugins: [
removePugAssertion(),
vue(),
],
... This is only for building Vue, not for dev server. It loads all Vue SFCs and replaces assertions only in the template block by checking if |
Would it be possible to extend this for optional brackets? |
|
import { readFileSync } from 'node:fs';
export default function () {
return {
name: 'vite-plugin-remove-pug-type-assertion',
enforce: 'pre',
load(id: string) {
if (!id.endsWith('.vue')) return;
let content = readFileSync(id, 'utf-8');
const regex = new RegExp(/(?<==")(.+)( as [\w<>|', ]+)/, 'g');
while (regex.test(content)) {
content = content.replace(regex, '$1');
}
return content;
},
};
} I just worked on the former suggestion for a vite solution with regard to build/serve and multiple assertions in a line |
Describe the bug
Having something like
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
which leads to some cryptic error messages like
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
The text was updated successfully, but these errors were encountered: