Skip to content

Commit

Permalink
chore: fix JsonEditor type export
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Oct 24, 2024
1 parent e640046 commit 1996002
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 26 deletions.
20 changes: 1 addition & 19 deletions rollup.config.vanilla-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,5 @@ export default {
format: 'es'
}
],
plugins: [dts(), exportJsonEditorClass()]
}

// The DTS plugin doesn't export the JsonEditor class that is generated by Svelte whilst compiling,
// so we append an export for it manually. Similarly, the CreateJSONEditorProps interface is not
// exported by DTS, so we add an export for it too.
function exportJsonEditorClass() {
return {
name: 'export-json-editor',
transform: (code, id) => {
if (id.endsWith('index-vanilla.d.ts')) {
const customExports = 'export type { JsonEditor, CreateJSONEditorProps }'

return `${code}\n${customExports}\n`
}

return code
}
}
plugins: [dts()]
}
14 changes: 7 additions & 7 deletions src/lib/index-vanilla.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import JSONEditorComponent from './components/JSONEditor.svelte'
import JsonEditor from './components/JSONEditor.svelte'
import type { JSONEditorPropsOptional } from '$lib/types'
import { mount, unmount } from 'svelte'

// Note: index.ts exports `JSONEditor`, but we will override this on purpose
// since we cannot use it in the vanilla environment starting in Svelte 5.
export * from './index'

interface CreateJSONEditorProps {
export interface CreateJSONEditorProps {
target: HTMLDivElement
props: JSONEditorPropsOptional
}

export function createJSONEditor({ target, props }: Parameters<typeof mount>[1]) {
const editor = mount(JSONEditorComponent, { target, props })
export { JsonEditor }

export function createJSONEditor({ target, props }: Parameters<typeof mount>[1]): JsonEditor {
const editor = mount(JsonEditor, { target, props })

editor.destroy = async () => {
unmount(editor)

return new Promise((resolve) => setTimeout(resolve))
}

return editor
return editor as JsonEditor
}

/**
* JSONEditor class
* @constructor
* @deprecated The constructor "new JSONEditor(...)" is deprecated. Please use "createJSONEditor(...)" instead.
*/
export function JSONEditor({ target, props }: CreateJSONEditorProps) {
Expand Down

0 comments on commit 1996002

Please sign in to comment.