Skip to content
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

fix: ts expression should work well in the pug template #123

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions packages/plugin-vue/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
SFCTemplateCompileResults,
} from 'vue/compiler-sfc'
import type { PluginContext, TransformPluginContext } from 'rollup'
import { transformWithEsbuild } from 'vite'
import { getResolvedScript } from './script'
import { createRollupError } from './utils/error'
import type { ResolvedOptions } from '.'
Expand All @@ -19,9 +20,14 @@ export async function transformTemplateAsModule(
pluginContext: TransformPluginContext,
ssr: boolean,
) {
const result = compile(code, descriptor, options, pluginContext, ssr)
let { code: returnCode, map: returnMap } = compile(
code,
descriptor,
options,
pluginContext,
ssr,
)

let returnCode = result.code
if (
options.devServer &&
options.devServer.config.server.hmr !== false &&
Expand All @@ -33,9 +39,30 @@ export async function transformTemplateAsModule(
})`
}

const lang = descriptor.scriptSetup?.lang || descriptor.script?.lang

if (
lang &&
/tsx?$/.test(lang) &&
!descriptor.script?.src // only normal script can have src
) {
const { code, map } = await transformWithEsbuild(
returnCode,
descriptor.filename,
{
loader: 'ts',
target: 'esnext',
sourcemap: options.sourceMap,
},
returnMap,
)
returnCode = code
returnMap = returnMap ? (map as any) : returnMap
}

return {
code: returnCode,
map: result.map,
map: returnMap,
}
}

Expand Down
3 changes: 3 additions & 0 deletions playground/vue/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<SetupImportTemplate />
<WorkerTest />
<Url />
<TsExpressionInTemplate />
</template>

<script setup lang="ts">
Expand All @@ -44,6 +45,8 @@ import AsyncComponent from './AsyncComponent.vue'
import ReactivityTransform from './ReactivityTransform.vue'
import SetupImportTemplate from './setup-import-template/SetupImportTemplate.vue'
import WorkerTest from './worker.vue'
import TsExpressionInTemplate from './TsExpressionInPugTemplate.vue'

import { ref } from 'vue'
import Url from './Url.vue'

Expand Down
11 changes: 11 additions & 0 deletions playground/vue/TsExpressionInPugTemplate.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template lang="pug">
h2.ts-expression-title Ts expression in template
button.ts-expression(@click="(foo as number)++")
| {{ foo }}
</template>

<script setup lang="ts">
import { ref } from 'vue'

const foo = ref(0)
</script>
8 changes: 8 additions & 0 deletions playground/vue/__tests__/vue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,11 @@ describe('import with ?url', () => {
)
})
})

describe('ts expression in the pug template', () => {
test('should work', async () => {
expect(await page.textContent('.ts-expression')).toMatch('0')
await page.click('.ts-expression')
expect(await page.textContent('.ts-expression')).toMatch('1')
})
})