Skip to content

Commit 3f4cf82

Browse files
authored
fix(plugin-vue): template src isn't working when script setup (#5418)
1 parent fa9ee58 commit 3f4cf82

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

packages/plugin-vue/src/main.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from './utils/descriptorCache'
1111
import { PluginContext, SourceMap, TransformPluginContext } from 'rollup'
1212
import { normalizePath } from '@rollup/pluginutils'
13-
import { resolveScript } from './script'
13+
import { resolveScript, isUseInlineTemplate } from './script'
1414
import { transformTemplateInMain } from './template'
1515
import { isOnlyTemplateChanged, isEqualBlock } from './handleHotUpdate'
1616
import { RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map'
@@ -53,14 +53,7 @@ export async function transformMain(
5353
)
5454

5555
// template
56-
// Check if we can use compile template as inlined render function
57-
// inside <script setup>. This can only be done for build because
58-
// inlined template cannot be individually hot updated.
59-
const useInlineTemplate =
60-
!devServer &&
61-
descriptor.scriptSetup &&
62-
!(descriptor.template && descriptor.template.src)
63-
const hasTemplateImport = descriptor.template && !useInlineTemplate
56+
const hasTemplateImport = descriptor.template && !isUseInlineTemplate(descriptor, !devServer)
6457

6558
let templateCode = ''
6659
let templateMap: RawSourceMap | undefined

packages/plugin-vue/src/script.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ export function setResolvedScript(
2222
;(ssr ? ssrCache : clientCache).set(descriptor, script)
2323
}
2424

25+
// Check if we can use compile template as inlined render function
26+
// inside <script setup>. This can only be done for build because
27+
// inlined template cannot be individually hot updated.
28+
export function isUseInlineTemplate(descriptor: SFCDescriptor, isProd: boolean): boolean {
29+
return (
30+
isProd &&
31+
!!descriptor.scriptSetup &&
32+
!descriptor.template?.src
33+
)
34+
}
35+
2536
export function resolveScript(
2637
descriptor: SFCDescriptor,
2738
options: ResolvedOptions,
@@ -43,7 +54,7 @@ export function resolveScript(
4354
...options.script,
4455
id: descriptor.id,
4556
isProd: options.isProduction,
46-
inlineTemplate: !options.devServer,
57+
inlineTemplate: isUseInlineTemplate(descriptor, !options.devServer),
4758
refTransform: options.refTransform !== false,
4859
templateOptions: resolveTemplateCompilerOptions(descriptor, options, ssr),
4960
// @ts-ignore TODO remove ignore when we support this in @vue/compiler-sfc

0 commit comments

Comments
 (0)