Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): patch VueFlow slots after build and apply correct types #474

Merged
merged 2 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-candles-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vue-flow/core': patch
---

Fix untyped connection line, node and edge slots props by patching type definition after build
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"scripts": {
"prepare": "ts-patch install -s",
"build": "vite build",
"types": "pnpm prepare && vue-tsc --declaration --emitDeclarationOnly && tsc -p tsconfig.build.json && shx rm -rf tmp && pnpm lint:dist",
"types": "pnpm prepare && vue-tsc --declaration --emitDeclarationOnly && tsc -p tsconfig.build.json && shx rm -rf tmp && pnpm lint:dist && pnpm run patch",
"patch": "node patch/slots.js && pnpm lint:dist",
"theme": "postcss src/style.css -o dist/style.css && postcss src/theme-default.css -o dist/theme-default.css",
"lint": "eslint --ext \".js,.jsx,.ts,.tsx\" --fix --ignore-path ../../.gitignore .",
"lint:dist": "eslint --ext \".ts,.tsx\" -c .eslintrc.js --fix ./dist",
Expand Down
79 changes: 79 additions & 0 deletions packages/core/patch/slots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const { readFile, writeFile } = require('fs/promises')
const { resolve } = require('path')

/**
* This is a workaround until slots can be properly typed from inside the VueFlow component
* It will overwrite `{}` typings with the correct prop types and add slot types for dynamic node and edge slots
*/

async function content(path) {
return readFile(path, 'utf8')
}

const filePath = resolve(__dirname, '../dist/container/VueFlow/VueFlow.vue.d.ts')

const typeImportsString = `import type {
Connection,
EdgeChange,
EdgeMouseEvent,
EdgeUpdateEvent,
GraphEdge,
GraphNode,
NodeChange,
NodeDragEvent,
NodeMouseEvent,
OnConnectStartParams,
ViewpaneTransform,
VueFlowStore,
} from '../../types'`

const patchedTypeImports = `import type {
Connection,
ConnectionLineProps,
EdgeChange,
EdgeMouseEvent,
EdgeProps,
EdgeUpdateEvent,
GraphEdge,
GraphNode,
NodeChange,
NodeDragEvent,
NodeMouseEvent,
NodeProps,
OnConnectStartParams,
ViewpaneTransform,
VueFlowStore,
} from '../../types'`

const unpatchedSlots = `(new () => {
$slots: Record<string, {}> &
Record<string, {}> & {
'connection-line': (_: {}) => any
'zoom-pane': (_: {}) => any
'default': (_: {}) => any
}
})`

const patchedSlots = `(new () => {
$slots: Record<string, any> & {
'connection-line': (connectionLineProps: ConnectionLineProps) => any
'zoom-pane': () => any
'default': () => any
} & {
[key: \`node-\${string}\`]: (nodeProps: NodeProps) => any
} & {
[key: \`edge-\${string}\`]: (edgeProps: EdgeProps) => any
}
})`

const patchSlots = async () => {
const fileContents = await content(filePath)

const patchedFileContents = fileContents.replace(typeImportsString, patchedTypeImports).replace(unpatchedSlots, patchedSlots)

await writeFile(filePath, patchedFileContents)
}

patchSlots()
.then(() => console.log('slots patched'))
.catch(console.log)