Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 90209a7

Browse files
committed
feat: apply esbuild transform during serve
1 parent 11ee1c8 commit 90209a7

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
145145
// generate an entry module that imports the actual blocks of the SFC
146146
if (!query.vue && filter(id)) {
147147
debug(`transform SFC entry (${id})`)
148-
const output = genSfcFacade(
148+
const output = await genSfcFacade(
149149
code,
150150
id,
151151
options,

src/sfcFacade.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { resolveScript } from './script'
1515
import { transformTemplateInMain } from './template'
1616
import { isOnlyTemplateChanged } from './handleHotUpdate'
1717

18-
export function genSfcFacade(
18+
export async function genSfcFacade(
1919
code: string,
2020
filename: string,
2121
options: Options,
@@ -52,7 +52,7 @@ export function genSfcFacade(
5252
const hasScoped = descriptor.styles.some((s) => s.scoped)
5353

5454
// script
55-
const { code: scriptCode, map } = genScriptCode(
55+
const { code: scriptCode, map } = await genScriptCode(
5656
descriptor,
5757
scopeId,
5858
isProduction,
@@ -166,7 +166,7 @@ function genTemplateCode(
166166
}
167167
}
168168

169-
function genScriptCode(
169+
async function genScriptCode(
170170
descriptor: SFCDescriptor,
171171
scopeId: string,
172172
isProd: boolean,
@@ -186,9 +186,23 @@ function genScriptCode(
186186
)
187187
if (script) {
188188
// js or ts can be directly placed in the main module
189-
if ((!script.lang || script.lang === 'ts') && !script.src) {
189+
if (
190+
(!script.lang ||
191+
(script.lang === 'ts' && (pluginContext as any).server)) &&
192+
!script.src
193+
) {
190194
scriptCode = rewriteDefault(script.content, `_sfc_main`)
191195
map = script.map
196+
if (script.lang === 'ts') {
197+
const result = await (pluginContext as any).server.transformWithEsbuild(
198+
scriptCode,
199+
descriptor.filename,
200+
{ loader: 'ts' },
201+
map
202+
)
203+
scriptCode = result.code
204+
map = result.map
205+
}
192206
} else {
193207
const src = script.src || descriptor.filename
194208
const attrsQuery = attrsToQuery(script.attrs, 'js')

0 commit comments

Comments
 (0)