-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Clean up dynamic code imports, types
- Loading branch information
Showing
10 changed files
with
237 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { SvelteComponent } from 'svelte'; | ||
import { derived, readable, writable } from 'svelte/store'; | ||
|
||
import type { ComponentType } from 'svelte'; | ||
import type { | ||
HighlightEvents, | ||
HighlightProps, | ||
HighlightSlots | ||
} from 'svelte-highlight/Highlight.svelte'; | ||
import type { | ||
HighlightSvelteEvents, | ||
HighlightSvelteProps, | ||
HighlightSvelteSlots | ||
} from 'svelte-highlight/HighlightSvelte.svelte'; | ||
import type { | ||
LineNumbersEvents, | ||
LineNumbersProps, | ||
LineNumbersSlots | ||
} from 'svelte-highlight/LineNumbers.svelte'; | ||
|
||
class _LineNumbers extends SvelteComponent< | ||
LineNumbersProps, | ||
LineNumbersEvents, | ||
LineNumbersSlots | ||
> {} | ||
class _HighlightSvelte extends SvelteComponent< | ||
HighlightSvelteProps, | ||
HighlightSvelteEvents, | ||
HighlightSvelteSlots | ||
> {} | ||
class _Highlight extends SvelteComponent< | ||
HighlightProps, | ||
HighlightEvents, | ||
HighlightSlots | ||
> {} | ||
|
||
export type LineNumbers = ComponentType<_LineNumbers>; | ||
export type HighlightSvelte = ComponentType<_HighlightSvelte>; | ||
export type Highlight = ComponentType<_Highlight>; | ||
|
||
const stores = { | ||
LineNumbers: writable<Promise<LineNumbers> | undefined>(undefined), | ||
HighlightSvelte: writable<Promise<HighlightSvelte> | undefined>(undefined), | ||
Highlight: writable<Promise<Highlight> | undefined>(undefined) | ||
} as const; | ||
|
||
const genericDerivedAsyncImport = <K extends keyof typeof stores>( | ||
name: K, | ||
target: (typeof stores)[K] | ||
) => | ||
derived(target, (value: Parameters<(typeof target)['set']>[0]) => { | ||
if (value === undefined) { | ||
const promise = import( | ||
`../../../node_modules/svelte-highlight/${name}.svelte` | ||
).then((module) => module.default) as unknown as NonNullable<typeof value>; | ||
// @ts-expect-error - TS infers target['set'] here as an Intersection of Writable ReturnTypes rather than Unions | ||
target.set(promise); | ||
return promise; | ||
} | ||
return value; | ||
}), | ||
empty = () => | ||
readable<Promise<undefined>>(new Promise((resolve) => resolve(undefined))); | ||
|
||
const lineNumbers = (useLineNumbers: boolean) => { | ||
return useLineNumbers | ||
? genericDerivedAsyncImport('LineNumbers', stores.LineNumbers) | ||
: empty(); | ||
}, | ||
highlightSvelte = (useHlSvelte: boolean) => { | ||
return useHlSvelte | ||
? genericDerivedAsyncImport('HighlightSvelte', stores.HighlightSvelte) | ||
: empty(); | ||
}, | ||
highlight = () => genericDerivedAsyncImport('Highlight', stores.Highlight); | ||
|
||
export { | ||
highlight as Highlight, | ||
highlightSvelte as HighlightSvelte, | ||
lineNumbers as LineNumbers | ||
}; |
Oops, something went wrong.