@kubb/react-fabric 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/App-2SatilGX.d.ts +174 -0
- package/dist/App-DwUtyfk9.d.cts +174 -0
- package/dist/{globals-BQ_tJj2b.d.ts → KubbFile-D4gyyfL-.d.ts} +1 -52
- package/dist/{globals-C-9ezLk9.d.cts → KubbFile-FGXV713i.d.cts} +1 -52
- package/dist/Runtime-DWlDUeLZ.js +15482 -0
- package/dist/Runtime-DWlDUeLZ.js.map +1 -0
- package/dist/Runtime-JI640uK4.cjs +15520 -0
- package/dist/Runtime-JI640uK4.cjs.map +1 -0
- package/dist/globals-BRG2DOJd.d.cts +52 -0
- package/dist/globals-D_1Lfe9-.d.ts +52 -0
- package/dist/globals.d.cts +3 -2
- package/dist/globals.d.ts +3 -2
- package/dist/index.cjs +150 -15635
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +50 -202
- package/dist/index.d.ts +50 -202
- package/dist/index.js +6 -15494
- package/dist/index.js.map +1 -1
- package/dist/{jsx-runtime-BPQkRAg2.js → jsx-runtime-Dg-pJBHd.js} +1 -1
- package/dist/{jsx-runtime-BPQkRAg2.js.map → jsx-runtime-Dg-pJBHd.js.map} +1 -1
- package/dist/{jsx-runtime-B3MMb3PL.cjs → jsx-runtime-d2dmYscH.cjs} +1 -1
- package/dist/{jsx-runtime-B3MMb3PL.cjs.map → jsx-runtime-d2dmYscH.cjs.map} +1 -1
- package/dist/jsx-runtime.cjs +1 -1
- package/dist/jsx-runtime.d.cts +3 -2
- package/dist/jsx-runtime.d.ts +3 -2
- package/dist/jsx-runtime.js +1 -1
- package/dist/parsers.cjs +9 -0
- package/dist/parsers.d.cts +18 -0
- package/dist/parsers.d.ts +18 -0
- package/dist/parsers.js +3 -0
- package/dist/plugins.cjs +40 -0
- package/dist/plugins.cjs.map +1 -0
- package/dist/plugins.d.cts +55 -0
- package/dist/plugins.d.ts +55 -0
- package/dist/plugins.js +34 -0
- package/dist/plugins.js.map +1 -0
- package/dist/{types-D9OfSq91.d.ts → types-Brnyan9B.d.ts} +1 -1
- package/dist/{types-CUKR3KZn.d.cts → types-C3p0Ljxf.d.cts} +1 -1
- package/dist/types.d.cts +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +16 -2
- package/src/{kubbRenderer.ts → Renderer.ts} +1 -1
- package/src/{ReactTemplate.tsx → Runtime.tsx} +36 -30
- package/src/index.ts +9 -1
- package/src/parsers/index.ts +1 -0
- package/src/plugins/index.ts +3 -0
- package/src/plugins/reactPlugin.ts +47 -0
- package/src/utils/processFiles.ts +5 -4
- package/src/createApp.ts +0 -19
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import process from 'node:process'
|
|
2
|
-
import type { AppContext } from '@kubb/fabric-core/types'
|
|
3
2
|
import type { ReactNode } from 'react'
|
|
4
3
|
import { ConcurrentRoot } from 'react-reconciler/constants'
|
|
5
4
|
import { onExit } from 'signal-exit'
|
|
6
5
|
import { Root } from './components/Root.tsx'
|
|
7
6
|
import { createNode } from './dom.ts'
|
|
8
|
-
import type { FiberRoot } from './
|
|
9
|
-
import {
|
|
7
|
+
import type { FiberRoot } from './Renderer.ts'
|
|
8
|
+
import { Renderer } from './Renderer.ts'
|
|
10
9
|
import type { DOMElement } from './types.ts'
|
|
11
10
|
import { squashTextNodes } from './utils/squashTextNodes.ts'
|
|
12
11
|
import { processFiles } from './utils/processFiles.ts'
|
|
12
|
+
import type { FileManager } from '@kubb/fabric-core'
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
type Options = {
|
|
15
|
+
fileManager: FileManager
|
|
15
16
|
stdout?: NodeJS.WriteStream
|
|
16
17
|
stdin?: NodeJS.ReadStream
|
|
17
18
|
stderr?: NodeJS.WriteStream
|
|
@@ -19,10 +20,10 @@ export type ReactAppContext = AppContext<{
|
|
|
19
20
|
* Set this to true to always see the result of the render in the console(line per render)
|
|
20
21
|
*/
|
|
21
22
|
debug?: boolean
|
|
22
|
-
}
|
|
23
|
+
}
|
|
23
24
|
|
|
24
|
-
export class
|
|
25
|
-
readonly #
|
|
25
|
+
export class Runtime {
|
|
26
|
+
readonly #options: Options
|
|
26
27
|
// Ignore last render after unmounting a tree to prevent empty output before exit
|
|
27
28
|
#isUnmounted: boolean
|
|
28
29
|
|
|
@@ -30,8 +31,8 @@ export class ReactTemplate {
|
|
|
30
31
|
readonly #container: FiberRoot
|
|
31
32
|
readonly #rootNode: DOMElement
|
|
32
33
|
|
|
33
|
-
constructor(
|
|
34
|
-
this.#
|
|
34
|
+
constructor(options: Options) {
|
|
35
|
+
this.#options = options
|
|
35
36
|
|
|
36
37
|
this.#rootNode = createNode('kubb-root')
|
|
37
38
|
this.#rootNode.onRender = this.onRender
|
|
@@ -85,7 +86,7 @@ export class ReactTemplate {
|
|
|
85
86
|
const onRecoverableError = logRecoverableError
|
|
86
87
|
const transitionCallbacks = null
|
|
87
88
|
|
|
88
|
-
this.#container =
|
|
89
|
+
this.#container = Renderer.createContainer(
|
|
89
90
|
this.#rootNode,
|
|
90
91
|
rootTag,
|
|
91
92
|
hydrationCallbacks,
|
|
@@ -106,12 +107,17 @@ export class ReactTemplate {
|
|
|
106
107
|
{ alwaysLast: false },
|
|
107
108
|
).bind(this)
|
|
108
109
|
|
|
109
|
-
|
|
110
|
+
Renderer.injectIntoDevTools({
|
|
110
111
|
bundleType: 0, // 0 for PROD, 1 for DEV
|
|
111
112
|
version: '19.1.1', // should be React version and not Kubb's custom version
|
|
112
113
|
rendererPackageName: 'kubb', // package name
|
|
113
114
|
})
|
|
114
115
|
}
|
|
116
|
+
|
|
117
|
+
get fileManager() {
|
|
118
|
+
return this.#options.fileManager
|
|
119
|
+
}
|
|
120
|
+
|
|
115
121
|
resolveExitPromise: () => void = () => {}
|
|
116
122
|
rejectExitPromise: (reason?: Error) => void = () => {}
|
|
117
123
|
unsubscribeExit: () => void = () => {}
|
|
@@ -120,25 +126,25 @@ export class ReactTemplate {
|
|
|
120
126
|
return
|
|
121
127
|
}
|
|
122
128
|
|
|
123
|
-
this
|
|
129
|
+
this.fileManager.clear()
|
|
124
130
|
|
|
125
|
-
processFiles(this.#rootNode, this
|
|
131
|
+
processFiles(this.#rootNode, this.fileManager)
|
|
126
132
|
|
|
127
|
-
if (!this.#
|
|
133
|
+
if (!this.#options?.debug && !this.#options?.stdout) {
|
|
128
134
|
return
|
|
129
135
|
}
|
|
130
136
|
|
|
131
|
-
const output = await this.#getOutput(this.#rootNode
|
|
137
|
+
const output = await this.#getOutput(this.#rootNode)
|
|
132
138
|
|
|
133
|
-
if (this.#
|
|
139
|
+
if (this.#options?.debug) {
|
|
134
140
|
console.log('Rendering: \n')
|
|
135
141
|
console.log(output)
|
|
136
142
|
}
|
|
137
143
|
|
|
138
|
-
if (this.#
|
|
139
|
-
this.#
|
|
140
|
-
this.#
|
|
141
|
-
this.#
|
|
144
|
+
if (this.#options?.stdout && process.env.NODE_ENV !== 'test') {
|
|
145
|
+
this.#options.stdout.clearLine(0)
|
|
146
|
+
this.#options.stdout.cursorTo(0)
|
|
147
|
+
this.#options.stdout.write(output)
|
|
142
148
|
}
|
|
143
149
|
}
|
|
144
150
|
onError(error: Error): void {
|
|
@@ -152,9 +158,9 @@ export class ReactTemplate {
|
|
|
152
158
|
this.unmount(error)
|
|
153
159
|
}
|
|
154
160
|
|
|
155
|
-
async #getOutput(node: DOMElement
|
|
161
|
+
async #getOutput(node: DOMElement): Promise<string> {
|
|
156
162
|
const text = squashTextNodes(node)
|
|
157
|
-
const files =
|
|
163
|
+
const files = this.fileManager.files
|
|
158
164
|
|
|
159
165
|
return files.length
|
|
160
166
|
? [...files]
|
|
@@ -171,8 +177,8 @@ export class ReactTemplate {
|
|
|
171
177
|
</Root>
|
|
172
178
|
)
|
|
173
179
|
|
|
174
|
-
|
|
175
|
-
|
|
180
|
+
Renderer.updateContainerSync(element, this.#container, null, null)
|
|
181
|
+
Renderer.flushSyncWork()
|
|
176
182
|
}
|
|
177
183
|
|
|
178
184
|
async renderToString(node: ReactNode): Promise<string> {
|
|
@@ -182,12 +188,12 @@ export class ReactTemplate {
|
|
|
182
188
|
</Root>
|
|
183
189
|
)
|
|
184
190
|
|
|
185
|
-
|
|
186
|
-
|
|
191
|
+
Renderer.updateContainerSync(element, this.#container, null, null)
|
|
192
|
+
Renderer.flushSyncWork()
|
|
187
193
|
|
|
188
|
-
this
|
|
194
|
+
this.fileManager.clear()
|
|
189
195
|
|
|
190
|
-
return this.#getOutput(this.#rootNode
|
|
196
|
+
return this.#getOutput(this.#rootNode)
|
|
191
197
|
}
|
|
192
198
|
|
|
193
199
|
unmount(error?: Error | number | null): void {
|
|
@@ -195,7 +201,7 @@ export class ReactTemplate {
|
|
|
195
201
|
return
|
|
196
202
|
}
|
|
197
203
|
|
|
198
|
-
if (this.#
|
|
204
|
+
if (this.#options?.debug) {
|
|
199
205
|
console.log('Unmount', error)
|
|
200
206
|
}
|
|
201
207
|
|
|
@@ -204,7 +210,7 @@ export class ReactTemplate {
|
|
|
204
210
|
|
|
205
211
|
this.#isUnmounted = true
|
|
206
212
|
|
|
207
|
-
|
|
213
|
+
Renderer.updateContainerSync(null, this.#container, null, null)
|
|
208
214
|
|
|
209
215
|
if (error instanceof Error) {
|
|
210
216
|
this.rejectExitPromise(error)
|
package/src/index.ts
CHANGED
|
@@ -2,18 +2,26 @@ import './globals.ts'
|
|
|
2
2
|
|
|
3
3
|
import * as React from 'react'
|
|
4
4
|
|
|
5
|
+
// components
|
|
5
6
|
export { App } from './components/App.tsx'
|
|
6
7
|
export { Const } from './components/Const.tsx'
|
|
7
8
|
export { File } from './components/File.tsx'
|
|
8
9
|
export { Function } from './components/Function.tsx'
|
|
9
10
|
export { Indent } from './components/Indent.tsx'
|
|
10
11
|
export { Type } from './components/Type.tsx'
|
|
11
|
-
|
|
12
|
+
|
|
12
13
|
export { useApp } from './composables/useApp.ts'
|
|
13
14
|
export { useFile } from './composables/useFile.ts'
|
|
14
15
|
export { useLifecycle } from './composables/useLifecycle.tsx'
|
|
16
|
+
|
|
17
|
+
// expose fabric core helpers
|
|
18
|
+
export { createApp } from '@kubb/fabric-core'
|
|
19
|
+
|
|
20
|
+
// utils
|
|
15
21
|
export { createFunctionParams, FunctionParams } from './utils/getFunctionParams.ts'
|
|
22
|
+
export { Runtime } from './Runtime.tsx'
|
|
16
23
|
|
|
24
|
+
// react helpers
|
|
17
25
|
export const createContext = React.createContext
|
|
18
26
|
export const createElement = React.createElement
|
|
19
27
|
export const useContext = React.useContext
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@kubb/fabric-core/parsers'
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createPlugin } from '@kubb/fabric-core/plugins'
|
|
2
|
+
import { Runtime } from '../Runtime.tsx'
|
|
3
|
+
import { createElement, type ElementType } from 'react'
|
|
4
|
+
|
|
5
|
+
type Options = {
|
|
6
|
+
stdout?: NodeJS.WriteStream
|
|
7
|
+
stdin?: NodeJS.ReadStream
|
|
8
|
+
stderr?: NodeJS.WriteStream
|
|
9
|
+
/**
|
|
10
|
+
* Set this to true to always see the result of the render in the console(line per render)
|
|
11
|
+
*/
|
|
12
|
+
debug?: boolean
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type ExtendOptions = {
|
|
16
|
+
render(App: ElementType): Promise<void> | void
|
|
17
|
+
renderToString(App: ElementType): Promise<string> | string
|
|
18
|
+
waitUntilExit(): Promise<void>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare module '@kubb/fabric-core' {
|
|
22
|
+
interface App {
|
|
23
|
+
render(App: ElementType): Promise<void> | void
|
|
24
|
+
renderToString(App: ElementType): Promise<string> | string
|
|
25
|
+
waitUntilExit(): Promise<void>
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const reactPlugin = createPlugin<Options, ExtendOptions>({
|
|
30
|
+
name: 'react',
|
|
31
|
+
install() {},
|
|
32
|
+
inject(app, options: Options = {}) {
|
|
33
|
+
const runtime = new Runtime({ fileManager: app.context.fileManager, ...options })
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
async render(App) {
|
|
37
|
+
runtime.render(createElement(App))
|
|
38
|
+
},
|
|
39
|
+
async renderToString(App) {
|
|
40
|
+
return runtime.renderToString(createElement(App))
|
|
41
|
+
},
|
|
42
|
+
waitUntilExit() {
|
|
43
|
+
return runtime.waitUntilExit()
|
|
44
|
+
},
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
})
|
|
@@ -3,12 +3,13 @@ import { squashExportNodes } from './squashExportNodes.ts'
|
|
|
3
3
|
import { squashImportNodes } from './squashImportNodes.ts'
|
|
4
4
|
import { squashSourceNodes } from './squashSourceNodes.ts'
|
|
5
5
|
|
|
6
|
-
import type {
|
|
6
|
+
import type { KubbFile } from '@kubb/fabric-core/types'
|
|
7
7
|
import type React from 'react'
|
|
8
8
|
import type { File } from '../components/File.tsx'
|
|
9
9
|
import type { DOMElement } from '../types.ts'
|
|
10
|
+
import type { FileManager } from '@kubb/fabric-core'
|
|
10
11
|
|
|
11
|
-
export function processFiles(node: DOMElement,
|
|
12
|
+
export function processFiles(node: DOMElement, fileManager: FileManager) {
|
|
12
13
|
for (let index = 0; index < node.childNodes.length; index++) {
|
|
13
14
|
const childNode = node.childNodes[index]
|
|
14
15
|
|
|
@@ -17,7 +18,7 @@ export function processFiles(node: DOMElement, context: AppContext) {
|
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
if (childNode.nodeName !== '#text' && childNode.nodeName !== 'kubb-file' && nodeNames.includes(childNode.nodeName)) {
|
|
20
|
-
processFiles(childNode,
|
|
21
|
+
processFiles(childNode, fileManager)
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
if (childNode.nodeName === 'kubb-file') {
|
|
@@ -37,7 +38,7 @@ export function processFiles(node: DOMElement, context: AppContext) {
|
|
|
37
38
|
banner: attributes.banner,
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
fileManager.add(file)
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
}
|
package/src/createApp.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { defineApp } from '@kubb/fabric-core'
|
|
2
|
-
import { type ElementType, createElement } from 'react'
|
|
3
|
-
import { type ReactAppContext, ReactTemplate } from './ReactTemplate.tsx'
|
|
4
|
-
|
|
5
|
-
export const createApp = defineApp<ElementType, ReactAppContext>((App, context) => {
|
|
6
|
-
const template = new ReactTemplate(context)
|
|
7
|
-
|
|
8
|
-
return {
|
|
9
|
-
render() {
|
|
10
|
-
template.render(createElement(App))
|
|
11
|
-
},
|
|
12
|
-
async renderToString() {
|
|
13
|
-
return template.renderToString(createElement(App))
|
|
14
|
-
},
|
|
15
|
-
waitUntilExit() {
|
|
16
|
-
return template.waitUntilExit()
|
|
17
|
-
},
|
|
18
|
-
}
|
|
19
|
-
})
|