Skip to content

Commit 89cd845

Browse files
authored
fix(compiler): write exports for defineCustomElement typedefs (#4194)
* write exports for defineCustomElement typedefs * update tests * fix SNC violation * update relative typedef path generation to use tag name
1 parent 0193a14 commit 89cd845

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/compiler/output-targets/dist-custom-elements/custom-elements-types.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,22 @@ const generateCustomElementsTypesOutput = async (
7272
// - get the relative path to the component's source file from the source directory
7373
// - join that relative path to the relative path from the `index.d.ts` file to the
7474
// directory where typedefs are saved
75-
const componentSourceRelPath = relative(config.srcDir, component.sourceFilePath).replace('.tsx', '');
75+
const componentSourceRelPath = relative(config.srcDir, component.sourceFilePath).replace(/\.tsx$/, '');
7676
const componentDTSPath = join(componentsTypeDirectoryRelPath, componentSourceRelPath);
7777

78-
return `export { ${importName} as ${exportName} } from '${componentDTSPath}';`;
78+
const defineFunctionExportName = `defineCustomElement${exportName}`;
79+
// Get the path to the sibling typedef file for the current component
80+
// When we bundle the code to generate the component JS files it generates
81+
// the JS and typedef files based on the component tag name. So, we can
82+
// just use the tagname to create the relative path
83+
const localComponentTypeDefFilePath = `./${component.tagName}`;
84+
85+
return [
86+
`export { ${importName} as ${exportName} } from '${componentDTSPath}';`,
87+
// We need to alias each `defineCustomElement` function typedef to match the aliased name given to the
88+
// function in the `index.js`
89+
`export { defineCustomElement as ${defineFunctionExportName} } from '${localComponentTypeDefFilePath}';`,
90+
].join('\n');
7991
}),
8092
``,
8193
]

src/compiler/output-targets/test/custom-elements-types.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ describe('Custom Elements Typedef generation', () => {
6767
const expectedTypedefOutput = [
6868
'/* TestApp custom elements */',
6969
`export { StubCmp as MyComponent } from '${join(componentsTypeDirectoryPath, 'my-component', 'my-component')}';`,
70+
`export { defineCustomElement as defineCustomElementMyComponent } from './my-component';`,
7071
`export { MyBestComponent as MyBestComponent } from '${join(
7172
componentsTypeDirectoryPath,
7273
'the-other-component',
7374
'my-real-best-component'
7475
)}';`,
76+
`export { defineCustomElement as defineCustomElementMyBestComponent } from './my-best-component';`,
7577
'',
7678
'/**',
7779
' * Used to manually set the base path where assets can be found.',

0 commit comments

Comments
 (0)