Skip to content

Commit 7613534

Browse files
committed
feat: enableTsInTemplate option
1 parent bacc6a9 commit 7613534

6 files changed

+20
-5
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99
- `refSugar: boolean`: enable experimental ref sugar.
1010

1111
- `customElement: boolean | RegExp`: enable custom elements mode. An SFC loaded in custom elements mode inlines its `<style>` tags as strings under the component's `styles` option. When used with `defineCustomElement` from Vue core, the styles will be injected into the custom element's shadow root.
12+
1213
- Default is `/\.ce\.vue$/`
1314
- Setting to `true` will process all `.vue` files in custom element mode.
1415

16+
- `enableTsInTemplate: boolean` (16.8+): allow TS expressions in templates when `<script>` has `lang="ts"`. Defaults to `true`.
17+
18+
- When used with `ts-loader`, due to `ts-loader`'s cache invalidation behavior, it sometimes prevents the template from being hot-reloaded in isolation, causing the component to reload despite only the template being edited. If this is annoying, you can set this option to `false` (and avoid using TS expressions in templates).
19+
20+
- Alternatively, leave this option on (by default) and use [`esbuild-loader`](https://github.com/privatenumber/esbuild-loader) to transpile TS instead, which doesn't suffer from this problem (it's also a lot faster). However, do note you will need to rely on TS type checking from other sources (e.g. IDE or `vue-tsc`).
21+
1522
## What is Vue Loader?
1623

1724
`vue-loader` is a loader for [webpack](https://webpack.js.org/) that allows you to author Vue components in a format called [Single-File Components (SFCs)](./docs/spec.md):

example/TypeScript.vue

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<script setup lang="ts">
2-
let a: number = 12
2+
import { ref, Ref } from 'vue'
3+
const a: Ref<number> = ref(12)
34
</script>
45

56
<template>
6-
<p>From TS: {{ a?.toFixed(2) }}</p>
7+
<p @click="a++">From TSssss: {{ a.toFixed(2) }}</p>
78
</template>

example/webpack.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module.exports = (env = {}) => {
5555
loader: 'vue-loader',
5656
options: {
5757
refSugar: true,
58+
enableTsInTemplate: false,
5859
},
5960
},
6061
{

src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface VueLoaderOptions {
3636
hotReload?: boolean
3737
exposeFilename?: boolean
3838
appendExtension?: boolean
39+
enableTsInTemplate?: boolean
3940

4041
isServerBuild?: boolean
4142
}
@@ -178,7 +179,8 @@ export default function loader(
178179
const idQuery = `&id=${id}`
179180
const scopedQuery = hasScoped ? `&scoped=true` : ``
180181
const attrsQuery = attrsToQuery(descriptor.template.attrs)
181-
const tsQuery = isTS ? `&ts=true` : ``
182+
const tsQuery =
183+
options.enableTsInTemplate !== false && isTS ? `&ts=true` : ``
182184
const query = `?vue&type=template${idQuery}${scopedQuery}${tsQuery}${attrsQuery}${resourceQuery}`
183185
templateRequest = stringifyRequest(src + query)
184186
templateImport = `import { ${renderFnName} } from ${templateRequest}`

src/resolveScript.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ export function resolveScript(
6666
compiler: templateCompiler,
6767
compilerOptions: {
6868
...options.compilerOptions,
69-
...resolveTemplateTSOptions(descriptor, options.compilerOptions),
69+
...(options.enableTsInTemplate
70+
? resolveTemplateTSOptions(descriptor, options.compilerOptions)
71+
: null),
7072
},
7173
transformAssetUrls: options.transformAssetUrls || true,
7274
},

src/templateLoader.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ const TemplateLoader: webpack.loader.Loader = function (source, inMap) {
6363
...options.compilerOptions,
6464
scopeId: query.scoped ? `data-v-${scopeId}` : undefined,
6565
bindingMetadata: script ? script.bindings : undefined,
66-
...resolveTemplateTSOptions(descriptor, options.compilerOptions),
66+
...(options.enableTsInTemplate
67+
? resolveTemplateTSOptions(descriptor, options.compilerOptions)
68+
: null),
6769
},
6870
transformAssetUrls: options.transformAssetUrls || true,
6971
})

0 commit comments

Comments
 (0)