Skip to content

Commit

Permalink
fix(svelte): handle lang="ts"
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Nov 26, 2021
1 parent da9011f commit ab5829d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
19 changes: 17 additions & 2 deletions 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) =>
Expand Down Expand Up @@ -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,
})
Expand Down
17 changes: 17 additions & 0 deletions test/index.test.ts
Expand Up @@ -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': `
<script lang="ts">
let msg: string = 'hello svelte'
</script>
<span>{msg}</span>
`,
})

t.deepEqual(outFiles, ['input.js'])
})

test('onSuccess', async (t) => {
const randomNumber = Math.random() + ''
const { logs } = await run(
Expand Down

0 comments on commit ab5829d

Please sign in to comment.