Skip to content

Commit

Permalink
fix(react): wrap with React.lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
Aykut Saraç committed Sep 28, 2023
1 parent 813e50f commit b91753f
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions scripts/generate-react-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ for (const module of customElementsModules) {
const { events, name: componentName, tagName: fileName, jsDoc } = componentDeclaration;

const eventNames = events?.reduce((acc, curr) => {
acc[getReactEventName(curr.name)] = curr.name;
return acc;
}, {}) || {};
acc[getReactEventName(curr.name)] = curr.name;
return acc;
}, {}) || {};

const eventTypes = events?.map(event => {
const eventName = getReactEventName(event.name);
const eventType = cleanGenericTypes(componentDeclaration.typeParameters, event.type.text);
const predefinedEventName = `${componentName}${eventName.split("onBl")[1]}`;
const eventName = getReactEventName(event.name);
const eventType = cleanGenericTypes(componentDeclaration.typeParameters, event.type.text);
const predefinedEventName = `${componentName}${eventName.split("onBl")[1]}`;

eventStatements.push(`export declare type ${predefinedEventName} = ${eventType};`);
return `${eventName}: EventName<${predefinedEventName}>`;
}) || [];
eventStatements.push(`export declare type ${predefinedEventName} = ${eventType};`);
return `${eventName}: EventName<${predefinedEventName}>`;
}) || [];

const importPath = path.replace(/^src\//, "").replace(/\.ts$/, "");
const typeName = componentName + "Type";
Expand All @@ -52,13 +52,15 @@ for (const module of customElementsModules) {
const source = `
${jsDoc}
export const ${componentName}: React.LazyExoticComponent<ReactWebComponent<${componentType}>> =
customElements.whenDefined('${fileName}').then(() => createComponent<${componentType}>({
react: React,
displayName: "${componentName}",
tagName: "${fileName}",
elementClass: customElements.get("${fileName}"),
events: ${JSON.stringify(eventNames)},
}));
React.lazy(() => customElements.whenDefined('${fileName}').then(() =>
createComponent<${componentType}>({
react: React,
displayName: "${componentName}",
tagName: "${fileName}",
elementClass: customElements.get("${fileName}"),
events: ${JSON.stringify(eventNames)},
}))
);
`;

baklavaReactFileParts.push(source);
Expand Down

0 comments on commit b91753f

Please sign in to comment.