Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): write exports for defineCustomElement typedefs #4194

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,22 @@ const generateCustomElementsTypesOutput = async (
// - get the relative path to the component's source file from the source directory
// - join that relative path to the relative path from the `index.d.ts` file to the
// directory where typedefs are saved
const componentSourceRelPath = relative(config.srcDir, component.sourceFilePath).replace('.tsx', '');
const componentSourceRelPath = relative(config.srcDir, component.sourceFilePath).replace(/\.tsx$/, '');
const componentDTSPath = join(componentsTypeDirectoryRelPath, componentSourceRelPath);

return `export { ${importName} as ${exportName} } from '${componentDTSPath}';`;
const defineFunctionExportName = `defineCustomElement${exportName}`;
// Get the path to the sibling typedef file for the current component
// When we bundle the code to generate the component JS files it generates
// the JS and typedef files based on the component tag name. So, we can
// just use the tagname to create the relative path
const localComponentTypeDefFilePath = `./${component.tagName}`;

return [
`export { ${importName} as ${exportName} } from '${componentDTSPath}';`,
// We need to alias each `defineCustomElement` function typedef to match the aliased name given to the
// function in the `index.js`
`export { defineCustomElement as ${defineFunctionExportName} } from '${localComponentTypeDefFilePath}';`,
].join('\n');
}),
``,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ describe('Custom Elements Typedef generation', () => {
const expectedTypedefOutput = [
'/* TestApp custom elements */',
`export { StubCmp as MyComponent } from '${join(componentsTypeDirectoryPath, 'my-component', 'my-component')}';`,
`export { defineCustomElement as defineCustomElementMyComponent } from './my-component';`,
`export { MyBestComponent as MyBestComponent } from '${join(
componentsTypeDirectoryPath,
'the-other-component',
'my-real-best-component'
)}';`,
`export { defineCustomElement as defineCustomElementMyBestComponent } from './my-best-component';`,
'',
'/**',
' * Used to manually set the base path where assets can be found.',
Expand Down