-
Notifications
You must be signed in to change notification settings - Fork 29
/
cem-plugin-reactify.js
295 lines (263 loc) · 8.4 KB
/
cem-plugin-reactify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* eslint-disable */
import fs from 'fs';
import path from 'path';
import prettier from 'prettier';
const packageJsonPath = `${process.cwd()}${path.sep}package.json`;
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
function getDefineCallForElement(cem, tagName) {
let result = undefined;
cem?.modules?.forEach(_module => {
_module?.exports?.forEach(ex => {
if (ex.kind === 'custom-element-definition' && ex.name === tagName) result = _module.path;
});
});
return result;
}
function camelize(str) {
const arr = str.split('-');
const capital = arr.map((item, index) =>
index ? item.charAt(0).toUpperCase() + item.slice(1).toLowerCase() : item.toLowerCase(),
);
return capital.join('');
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
const has = arr => Array.isArray(arr) && arr.length > 0;
const RESERVED_WORDS = [
'children',
'localName',
'ref',
'style',
'className',
'abstract',
'arguments',
'await',
'boolean',
'break',
'byte',
'case',
'catch',
'char',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'double',
'else',
'enum',
'eval',
'export',
'extends',
'false',
'final',
'finally',
'float',
'for',
'function',
'goto',
'if',
'implements',
'import',
'in',
'instanceof',
'int',
'interface',
'let',
'long',
'native',
'new',
'null',
'package',
'private',
'protected',
'public',
'return',
'short',
'static',
'super',
'switch',
'synchronized',
'this',
'throw',
'throws',
'transient',
'true',
'try',
'typeof',
'var',
'void',
'volatile',
'while',
'with',
'yield',
];
/**
* ATTRIBUTE/PROPERTY NAME CLASHES:
* It could be the case that an attr/property are not correctly linked together (e.g.: the attr does not have a `fieldName` pointing
* to the property). In that case, there will be two props passed to the React component function with the same name, which will break things
* Make sure to document components correctly (in most cases, all you have to do is add an @attr jsdoc to the field)
*
* Attrs and properties are distinguished by an attr's `fieldName`. If an attr has a `fieldName`, we ignore it as being an attribute, and
* only use the property (which is whatever the `fieldName` points to). If an attr does not have a `fieldName`, we apply it as an attr
*
* EVENTS:
* `'selected-changed'` event expects a function passed as `onSelectedChanged` (we add the 'on', and we camelize and capitalize the event name)
*/
export default function reactify({ exclude = [], attributeMapping = {}, outdir = 'legacy' }) {
return {
name: 'reactify',
packageLinkPhase({ customElementsManifest }) {
if (!fs.existsSync(outdir)) {
fs.mkdirSync(outdir);
}
const components = [];
customElementsManifest.modules.forEach(mod => {
mod.declarations.forEach(dec => {
if (!exclude.includes(dec.name) && (dec.customElement || dec.tagName)) {
components.push(dec);
}
});
});
components.forEach(component => {
let useEffect = false;
const fields = component?.members?.filter(
member =>
member.kind === 'field' &&
!member.static &&
member.privacy !== 'private' &&
member.privacy !== 'protected',
);
const booleanAttributes = [];
const attributes = [];
component?.attributes
?.filter(attr => !attr.fieldName)
?.forEach(attr => {
/** Handle reserved keyword attributes */
if (RESERVED_WORDS.includes(attr?.name)) {
/** If we have a user-specified mapping, rename */
if (attr.name in attributeMapping) {
const attribute = {
...attr,
originalName: attr.name,
name: attributeMapping[attr.name],
};
if (attr?.type?.text === 'boolean') {
booleanAttributes.push(attribute);
} else {
attributes.push(attribute);
}
return;
}
throw new Error(
`Attribute \`${attr.name}\` in custom element \`${component.name}\` is a reserved keyword and cannot be used. Please provide an \`attributeMapping\` in the plugin options to rename the JavaScript variable that gets passed to the attribute.`,
);
}
if (attr?.type?.text === 'boolean') {
booleanAttributes.push(attr);
} else {
attributes.push(attr);
}
});
let params = [];
component?.events?.forEach(event => {
params.push(`on${capitalizeFirstLetter(camelize(event.name))}`);
});
fields?.forEach(member => {
params.push(member.name);
});
[...(booleanAttributes || []), ...(attributes || [])]?.forEach(attr => {
params.push(camelize(attr.name));
});
params = params?.join(', ');
const createEventName = event => `on${capitalizeFirstLetter(camelize(event.name))}`;
const events = component?.events?.map(
event => `
useEffect(() => {
if(${createEventName(event)} !== undefined) {
ref.current.addEventListener('${event.name}', ${createEventName(event)});
}
}, [])
`,
);
const booleanAttrs = booleanAttributes?.map(
attr => `
useEffect(() => {
if(${attr?.name ?? attr.originalName} !== undefined) {
if(${attr?.name ?? attr.originalName}) {
ref.current.setAttribute('${attr.name}', '');
} else {
ref.current.removeAttribute('${attr.name}');
}
}
}, [${attr?.originalName ?? attr.name}])
`,
);
const attrs = attributes?.map(
attr => `
useEffect(() => {
if(${attr?.name ??
attr.originalName} !== undefined && ref.current.getAttribute('${attr?.originalName ??
attr.name}') !== String(${attr?.name ?? attr.originalName})) {
ref.current.setAttribute('${attr?.originalName ?? attr.name}', ${attr?.name ??
attr.originalName})
}
}, [${attr?.name ?? attr.originalName}])
`,
);
const props = fields?.map(
member => `
useEffect(() => {
if(${member.name} !== undefined && ref.current.${member.name} !== ${member.name}) {
ref.current.${member.name} = ${member.name};
}
}, [${member.name}])
`,
);
if (has(events) || has(props) || has(attrs) || has(booleanAttrs)) {
useEffect = true;
}
const moduleSpecifier = path.join(
packageJson.name,
getDefineCallForElement(customElementsManifest, component.tagName),
);
const result = `
import React${useEffect ? ', {useEffect, useRef}' : ''} from "react";
import '${moduleSpecifier}';
export function ${component.name}({children${params ? ',' : ''} ${params}}) {
${useEffect ? `const ref = useRef(null);` : ''}
${has(events) ? '/** Event listeners - run once */' : ''}
${events?.join('') || ''}
${
has(booleanAttrs)
? '/** Boolean attributes - run whenever an attr has changed */'
: ''
}
${booleanAttrs?.join('') || ''}
${has(attrs) ? '/** Attributes - run whenever an attr has changed */' : ''}
${attrs?.join('') || ''}
${has(props) ? '/** Properties - run whenever a property has changed */' : ''}
${props?.join('') || ''}
return (
<${component.tagName} ${useEffect ? 'ref={ref}' : ''} ${[
...booleanAttributes,
...attributes,
]
.map(attr => `${attr?.originalName ?? attr.name}={${attr?.name ?? attr.originalName}}`)
.join(' ')}>
{children}
</${component.tagName}>
)
}
`;
fs.writeFileSync(
path.join(outdir, `${component.name}.jsx`),
prettier.format(result, { parser: 'babel' }),
);
});
},
};
}