@@ -17,210 +17,54 @@ export default function ({ sourceRoot }) {
17
17
18
18
mkdirSync ( virtualDir , { recursive : true } ) ;
19
19
20
- const usageVirtualDir = join ( cwd ( ) , ".resolid" , "code-demos" ) ;
20
+ return ( tree ) => {
21
+ visit ( tree , { type : "leafDirective" , name : "PropsTable" } , ( node , index , parent ) => {
22
+ if ( parent === undefined || index === undefined ) {
23
+ return ;
24
+ }
21
25
22
- mkdirSync ( usageVirtualDir , { recursive : true } ) ;
26
+ const componentFile = node . attributes [ "file" ] ;
23
27
24
- return ( tree , vfile ) => {
25
- const pageName = vfile . basename . replace ( vfile . extname , "" ) ;
28
+ if ( ! componentFile ) {
29
+ throw new Error ( `Invalid componentFile prop for ${ node . name } .` ) ;
30
+ }
26
31
27
- let usageIndex = 1 ;
32
+ const componentName = ( parse ( componentFile ) . name . match ( kebabCaseRegex ) || [ ] )
33
+ . map ( ( w ) => `${ w . charAt ( 0 ) . toUpperCase ( ) } ${ w . slice ( 1 ) } ` )
34
+ . join ( "" ) ;
28
35
29
- const usageMdx = [ ] ;
36
+ if ( ! propsTables [ componentName ] ) {
37
+ const componentProps = getComponentProps ( virtualDir , join ( sourceRoot , componentFile ) , componentName ) ;
30
38
31
- visit (
32
- tree ,
33
- [
34
- { type : "leafDirective" , name : "PropsTable" } ,
35
- { type : "containerDirective" , name : "UsageBlock" } ,
36
- ] ,
37
- ( node , index , parent ) => {
38
- const componentFile = node . attributes [ "file" ] ;
39
-
40
- if ( ! componentFile ) {
41
- throw new Error ( `Invalid componentFile prop for ${ node . name } .` ) ;
39
+ if ( componentProps ) {
40
+ propsTables [ componentName ] = componentProps ;
42
41
}
43
-
44
- const componentName = ( parse ( componentFile ) . name . match ( kebabCaseRegex ) || [ ] )
45
- . map ( ( w ) => `${ w . charAt ( 0 ) . toUpperCase ( ) } ${ w . slice ( 1 ) } ` )
46
- . join ( "" ) ;
47
-
48
- if ( ! propsTables [ componentName ] ) {
49
- const componentProps = getComponentProps ( virtualDir , join ( sourceRoot , componentFile ) , componentName ) ;
50
-
51
- if ( componentProps ) {
52
- propsTables [ componentName ] = componentProps ;
53
- }
54
- }
55
-
56
- if ( propsTables [ componentName ] ) {
57
- const propsJSON = JSON . stringify ( propsTables [ componentName ] ) ;
58
-
59
- if ( parent === undefined || index === undefined ) {
60
- return ;
61
- }
62
-
63
- if ( node . name === "UsageBlock" ) {
64
- const usageId = `_${ pageName } _u_${ usageIndex ++ } ` ;
65
- const usageName = `U_${ usageId . replace ( "-" , "_" ) } ` ;
66
- const virtualModulePath = join ( usageVirtualDir , `${ usageId } .tsx` ) ;
67
-
68
- usageMdx . push ( {
69
- type : "mdxjsEsm" ,
70
- value : `import ${ usageName } from ${ JSON . stringify ( virtualModulePath ) } ` ,
71
- data : {
72
- estree : {
73
- type : "Program" ,
74
- sourceType : "module" ,
75
- body : [
76
- {
77
- type : "ImportDeclaration" ,
78
- specifiers : [
79
- {
80
- type : "ImportDefaultSpecifier" ,
81
- local : { type : "Identifier" , name : usageName } ,
82
- } ,
83
- ] ,
84
- source : {
85
- type : "Literal" ,
86
- value : virtualModulePath ,
87
- raw : `${ JSON . stringify ( virtualModulePath ) } ` ,
88
- } ,
89
- } ,
90
- ] ,
42
+ }
43
+
44
+ if ( propsTables [ componentName ] ) {
45
+ const propsJSON = JSON . stringify ( propsTables [ componentName ] ) ;
46
+
47
+ parent . children [ index ] = {
48
+ type : "mdxJsxFlowElement" ,
49
+ name : "PropsTable" ,
50
+ attributes : [
51
+ {
52
+ type : "mdxJsxAttribute" ,
53
+ name : "props" ,
54
+ value : {
55
+ type : "mdxJsxAttributeValueExpression" ,
56
+ value : `(${ propsJSON } )` ,
57
+ data : {
58
+ estree : fromJs ( `(${ propsJSON } )` , {
59
+ module : true ,
60
+ } ) ,
91
61
} ,
92
62
} ,
93
- } ) ;
94
-
95
- const ignoresJson = JSON . stringify ( node . attributes [ "ignores" ] ?. split ( "," ) ?? [ ] ) ;
96
- const code = node . children . map ( ( child ) => child . value ) . join ( "\n" ) ;
97
-
98
- parent . children [ index ] = {
99
- type : "mdxJsxFlowElement" ,
100
- name : "UsageBlock" ,
101
- attributes : [
102
- {
103
- type : "mdxJsxAttribute" ,
104
- name : "props" ,
105
- value : {
106
- type : "mdxJsxAttributeValueExpression" ,
107
- value : `(${ propsJSON } )` ,
108
- data : {
109
- estree : fromJs ( `(${ propsJSON } )` , {
110
- module : true ,
111
- } ) ,
112
- } ,
113
- } ,
114
- } ,
115
- {
116
- type : "mdxJsxAttribute" ,
117
- name : "ignores" ,
118
- value : {
119
- type : "mdxJsxAttributeValueExpression" ,
120
- value : `(${ ignoresJson } )` ,
121
- data : {
122
- estree : fromJs ( `(${ ignoresJson } )` , {
123
- module : true ,
124
- } ) ,
125
- } ,
126
- } ,
127
- } ,
128
- ] ,
129
- children : [
130
- {
131
- type : "mdxFlowExpression" ,
132
- value : `(props) => <${ usageName } {...props} />` ,
133
- data : {
134
- estree : {
135
- type : "Program" ,
136
- body : [
137
- {
138
- type : "ExpressionStatement" ,
139
- expression : {
140
- type : "ArrowFunctionExpression" ,
141
- id : null ,
142
- expression : true ,
143
- generator : false ,
144
- async : false ,
145
- params : [
146
- {
147
- type : "Identifier" ,
148
- name : "props" ,
149
- } ,
150
- ] ,
151
- body : {
152
- type : "JSXElement" ,
153
- openingElement : {
154
- type : "JSXOpeningElement" ,
155
- attributes : [
156
- {
157
- type : "JSXSpreadAttribute" ,
158
- argument : {
159
- type : "Identifier" ,
160
- name : "props" ,
161
- } ,
162
- } ,
163
- ] ,
164
- name : {
165
- type : "JSXIdentifier" ,
166
- name : usageName ,
167
- } ,
168
- selfClosing : true ,
169
- } ,
170
- closingElement : null ,
171
- children : [ ] ,
172
- data : {
173
- _mdxExplicitJsx : true ,
174
- } ,
175
- } ,
176
- } ,
177
- } ,
178
- ] ,
179
- sourceType : "module" ,
180
- comments : [ ] ,
181
- } ,
182
- } ,
183
- } ,
184
- ] ,
185
- } ;
186
-
187
- if ( existsSync ( virtualModulePath ) ) {
188
- const content = readFileSync ( virtualModulePath , "utf8" ) ;
189
-
190
- if ( content === code ) {
191
- return ;
192
- }
193
- }
194
-
195
- writeFileSync ( virtualModulePath , code , "utf8" ) ;
196
- }
197
-
198
- if ( node . name === "PropsTable" ) {
199
- parent . children [ index ] = {
200
- type : "mdxJsxFlowElement" ,
201
- name : "PropsTable" ,
202
- attributes : [
203
- {
204
- type : "mdxJsxAttribute" ,
205
- name : "props" ,
206
- value : {
207
- type : "mdxJsxAttributeValueExpression" ,
208
- value : `(${ propsJSON } )` ,
209
- data : {
210
- estree : fromJs ( `(${ propsJSON } )` , {
211
- module : true ,
212
- } ) ,
213
- } ,
214
- } ,
215
- } ,
216
- ] ,
217
- } ;
218
- }
219
- }
220
- } ,
221
- ) ;
222
-
223
- tree . children . unshift ( ...usageMdx ) ;
63
+ } ,
64
+ ] ,
65
+ } ;
66
+ }
67
+ } ) ;
224
68
} ;
225
69
}
226
70
0 commit comments