From ab5829d44c126769ddba43255d819bc32dfa246a Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Fri, 26 Nov 2021 19:13:11 +0800 Subject: [PATCH] fix(svelte): handle `lang="ts"` --- src/esbuild/svelte.ts | 19 +++++++++++++++++-- test/index.test.ts | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/esbuild/svelte.ts b/src/esbuild/svelte.ts index 2ebe347d..9a7dd0c5 100644 --- a/src/esbuild/svelte.ts +++ b/src/esbuild/svelte.ts @@ -1,6 +1,6 @@ import fs from 'fs' import path from 'path' -import { Plugin } from 'esbuild' +import { Plugin, transform } from 'esbuild' import { localRequire } from '../utils' const useSvelteCssExtension = (p: string) => @@ -59,7 +59,22 @@ export const sveltePlugin = ({ // Convert Svelte syntax to JavaScript try { - const result = svelte.compile(source, { + const preprocess = await svelte.preprocess(source, { + async script({ content, attributes }) { + if (attributes.lang !== 'ts') return { code: content } + + const { code, map } = await transform(content, { + sourcefile: args.path, + loader: 'ts', + sourcemap: true, + }) + return { + code, + map, + } + }, + }) + const result = svelte.compile(preprocess.code, { filename, css: false, }) diff --git a/test/index.test.ts b/test/index.test.ts index 48bae6a9..f62f36f8 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -428,6 +428,23 @@ test('bundle svelte without styles', async (t) => { t.deepEqual(outFiles, ['input.js']) }) +test('svelte: typescript support', async (t) => { + const { outFiles } = await run(t.title, { + 'input.ts': `import App from './App.svelte' + export { App } + `, + 'App.svelte': ` + + + {msg} + `, + }) + + t.deepEqual(outFiles, ['input.js']) +}) + test('onSuccess', async (t) => { const randomNumber = Math.random() + '' const { logs } = await run(