[go: up one dir, main page]

Skip to content

Commit

Permalink
fix: svelte types generation during build (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: mattcroat <mattcroat.dev@gmail.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
3 people authored May 9, 2024
1 parent 625019f commit c135e0a
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 131 deletions.
1 change: 1 addition & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default defineBuildConfig({
await fs.writeFile('dist/svelte.mjs', 'export * from "./svelte/index.mjs"\n', 'utf-8')
await fs.writeFile('dist/svelte.d.ts', 'export * from "./svelte/index.mjs"\n', 'utf-8')
await fs.writeFile('dist/svelte.d.mts', 'export * from "./svelte/index.mjs"\n', 'utf-8')
await fs.copyFile('dist/svelte/index.d.ts', 'dist/svelte/index.d.mts')
},
},
})
10 changes: 9 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// @ts-check
import antfu from '@antfu/eslint-config'

export default antfu()
export default antfu({
svelte: true,
})
.append({
files: ['**/*.svelte'],
rules: {
'no-use-before-define': 'off',
},
})
.overrideRules({
// Not compatible with ESLint 9 yet
'react-hooks/exhaustive-deps': 'off',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
"eslint": "^9.2.0",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.6",
"eslint-plugin-svelte": "^2.38.0",
"esno": "^4.7.0",
"lint-staged": "^15.2.2",
"pnpm": "^9.0.6",
Expand Down
11 changes: 9 additions & 2 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import UnoCSS from 'unocss/vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import { svelte as Svelte } from '@sveltejs/vite-plugin-svelte'

export default defineConfig({
resolve: {
alias: {
'shiki-magic-move/core': fileURLToPath(new URL('../src/core.ts', import.meta.url)),
'shiki-magic-move/renderer': fileURLToPath(new URL('../src/renderer.ts', import.meta.url)),
},
},
plugins: [
Vue(),
UnoCSS(),
svelte(),
Svelte(),
],
})
129 changes: 125 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 28 additions & 28 deletions src/svelte/ShikiMagicMove.svelte
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
<script lang="ts">
import { codeToKeyedTokens, createMagicMoveMachine } from '../core'
import ShikiMagicMoveRenderer from './ShikiMagicMoveRenderer.svelte'
import type { HighlighterCore } from 'shiki/core'
import type { MagicMoveRenderOptions, MagicMoveDifferOptions } from '../types'
interface ShikiMagicMoveProps {
highlighter: HighlighterCore
lang: string
theme: string
code: string
options?: MagicMoveRenderOptions & MagicMoveDifferOptions
onStart?: () => void
onEnd?: () => void
}
<script lang='ts'>
import { codeToKeyedTokens, createMagicMoveMachine } from 'shiki-magic-move/core'
import type { HighlighterCore } from 'shiki/core'
import type { MagicMoveDifferOptions, MagicMoveRenderOptions } from 'shiki-magic-move/types'
import ShikiMagicMoveRenderer from './ShikiMagicMoveRenderer.svelte'
const { highlighter, lang, theme, code, options, onStart, onEnd, ...props }: ShikiMagicMoveProps = $props()
const machine = createMagicMoveMachine(
(code) => codeToKeyedTokens(highlighter, code, { lang, theme }, options?.lineNumbers),
options
)
const result = $derived(machine.commit(code))
interface ShikiMagicMoveProps {
highlighter: HighlighterCore
lang: string
theme: string
code: string
options?: MagicMoveRenderOptions & MagicMoveDifferOptions
onStart?: () => void
onEnd?: () => void
}
const { highlighter, lang, theme, code, options, onStart, onEnd, ...props }: ShikiMagicMoveProps = $props()
const machine = createMagicMoveMachine(
code => codeToKeyedTokens(highlighter, code, { lang, theme }, options?.lineNumbers),
options,
)
const result = $derived(machine.commit(code))
</script>

<ShikiMagicMoveRenderer
tokens={result.current}
previous={result.previous}
{options}
{onStart}
{onEnd}
{...props}
/>
tokens={result.current}
previous={result.previous}
{options}
{onStart}
{onEnd}
{...props}
/>
Loading

0 comments on commit c135e0a

Please sign in to comment.