Skip to content

Commit

Permalink
fix(tools): don't export react wrapper for non-exported class
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Jun 16, 2024
1 parent 83a433b commit bf6357a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-clocks-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@patternfly/pfe-tools": patch
---

**React**: ensure that only classes which are exported get wrapped
11 changes: 9 additions & 2 deletions tools/pfe-tools/react/generate-wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ function isCustomElementDeclaration(
return !!(declaration as CEM.CustomElementDeclaration).customElement;
}

function isExported(exports: CEM.Export[] | undefined) {
return function(declaration: CEM.Declaration): boolean {
return !!exports?.some(exp => exp.kind === 'js' && exp.declaration.name === declaration.name);
};
}

/** Remove a prefix from a class name */
function getDeprefixedClassName(className: string, prefix: string) {
const [fst, ...tail] = className.replace(prefix, '');
Expand Down Expand Up @@ -80,10 +86,10 @@ function genJavascriptModule(module: CEM.Module, pkgName: string, data: ReactWra
return javascript`// ${module.path}
import { createComponent } from '@lit/react';
import react from 'react';${data.map(x => javascript`
import { ${x.Class} as elementClass } from '${pkgName}/${module.path}';`).join('')}${data.map(x => javascript`
import { ${x.Class} } from '${pkgName}/${module.path}';`).join('')}${data.map(x => javascript`
export const ${x.reactComponentName} = createComponent({
tagName: '${x.tagName}',
elementClass,
elementClass: ${x.Class},
react,
events: ${x.eventsMap},
});`).join('\n')}
Expand All @@ -106,6 +112,7 @@ function genWrapperModules(
) {
const data: ReactWrapperData[] = (module.declarations ?? [])
.filter(isCustomElementDeclaration)
.filter(isExported(module.exports))
.map(getReactWrapperData(module, classPrefix, elPrefix));
const js = genJavascriptModule(module, pkgName, data);
const ts = genTypescriptModule(module, pkgName, data);
Expand Down

0 comments on commit bf6357a

Please sign in to comment.