Skip to content

Commit 2dc4d76

Browse files
committed
chore: update barrel files to avoid "export *"
1 parent 7649e34 commit 2dc4d76

4 files changed

Lines changed: 73 additions & 13 deletions

File tree

packages/eslint-plugin/src/configs/base.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ module.exports = {
2121
{
2222
files: '**/src/index.{ts,tsx,js}',
2323
rules: {
24-
// TODO: propagate to `error` once all packages barrel files have been fixed
25-
'@rnx-kit/no-export-all': ['warn', { expand: 'all' }],
24+
'@rnx-kit/no-export-all': ['error', { expand: 'all' }],
2625
},
2726
},
2827
],
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export * from './<%= componentName %>';
2-
export * from './<%= componentName %>.types';
3-
export * from './render<%= componentName %>';
4-
export * from './use<%= componentName %>';
5-
export * from './use<%= componentName %>Styles.styles';
1+
export { <%= componentName %> } from './<%= componentName %>';
2+
export type { <%= componentName %>Props, <%= componentName %>State } from './<%= componentName %>.types';
3+
export { render<%= componentName %>_unstable } from './render<%= componentName %>';
4+
export { use<%= componentName %>_unstable } from './use<%= componentName %>';
5+
export { <%= propertyName %>ClassNames, use<%= componentName %>Styles_unstable } from './use<%= componentName %>Styles.styles';

tools/workspace-plugin/src/generators/react-component/index.spec.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,14 @@ describe('react-component generator', () => {
5959
const rootOffset = offsetFromRoot(componentRootPath);
6060

6161
expect(tree.read(joinPathFragments(projectSourceRootPath, 'MyOne.ts'), 'utf-8')).toMatchInlineSnapshot(`
62-
"export * from './components/MyOne/index';
62+
"export {
63+
myOneClassNames,
64+
MyOne,
65+
renderMyOne_unstable,
66+
useMyOne_unstable,
67+
useMyOneStyles_unstable,
68+
} from './components/MyOne/index';
69+
export type { MyOneProps, MyOneState } from './components/MyOne/index';
6370
"
6471
`);
6572

@@ -75,6 +82,18 @@ describe('react-component generator', () => {
7582
]
7683
`);
7784

85+
expect(tree.read(joinPathFragments(componentRootPath, 'index.ts'), 'utf-8')).toMatchInlineSnapshot(`
86+
"export { MyOne } from './MyOne';
87+
export type { MyOneProps, MyOneState } from './MyOne.types';
88+
export { renderMyOne_unstable } from './renderMyOne';
89+
export { useMyOne_unstable } from './useMyOne';
90+
export {
91+
myOneClassNames,
92+
useMyOneStyles_unstable,
93+
} from './useMyOneStyles.styles';
94+
"
95+
`);
96+
7897
expect(tree.read(joinPathFragments(componentRootPath, 'MyOne.tsx'), 'utf-8')).toMatchInlineSnapshot(`
7998
"import * as React from 'react';
8099
import type { ForwardRefComponent } from '@fluentui/react-utilities';
@@ -174,15 +193,36 @@ describe('react-component generator', () => {
174193
const barrelPath = joinPathFragments(projectSourceRootPath, 'index.ts');
175194

176195
expect(tree.read(barrelPath, 'utf-8')).toMatchInlineSnapshot(`
177-
"export * from './MyOne';
196+
"export {
197+
myOneClassNames,
198+
MyOne,
199+
renderMyOne_unstable,
200+
useMyOne_unstable,
201+
useMyOneStyles_unstable,
202+
} from './MyOne';
203+
export type { MyOneProps, MyOneState } from './MyOne';
178204
"
179205
`);
180206

181207
await generator(tree, { project: 'react-one', name: 'MyTwo' });
182208

183209
expect(tree.read(barrelPath, 'utf-8')).toMatchInlineSnapshot(`
184-
"export * from './MyOne';
185-
export * from './MyTwo';
210+
"export {
211+
myOneClassNames,
212+
MyOne,
213+
renderMyOne_unstable,
214+
useMyOne_unstable,
215+
useMyOneStyles_unstable,
216+
} from './MyOne';
217+
export type { MyOneProps, MyOneState } from './MyOne';
218+
export {
219+
myTwoClassNames,
220+
MyTwo,
221+
renderMyTwo_unstable,
222+
useMyTwo_unstable,
223+
useMyTwoStyles_unstable,
224+
} from './MyTwo';
225+
export type { MyTwoProps, MyTwoState } from './MyTwo';
186226
"
187227
`);
188228
});

tools/workspace-plugin/src/generators/react-component/index.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ function createStoriesTitle(options: NormalizedSchema) {
8181
return storiesTitle;
8282
}
8383

84+
function createExportsForComponent(options: NormalizedSchema) {
85+
const exports = [
86+
`${options.propertyName}ClassNames`,
87+
options.componentName,
88+
`render${options.componentName}_unstable`,
89+
`use${options.componentName}_unstable`,
90+
`use${options.componentName}Styles_unstable`,
91+
];
92+
const typeExports = [`${options.componentName}Props`, `${options.componentName}State`];
93+
94+
return { exports, typeExports };
95+
}
96+
8497
function addFiles(tree: Tree, options: NormalizedSchema) {
8598
const sourceRoot = options.projectConfig.sourceRoot as string;
8699

@@ -99,9 +112,12 @@ function addFiles(tree: Tree, options: NormalizedSchema) {
99112
templateOptions,
100113
);
101114

115+
const { exports, typeExports } = createExportsForComponent(options);
116+
102117
tree.write(
103118
joinPathFragments(sourceRoot, options.componentName + '.ts'),
104-
`export * from './${options.directory}/${options.componentName}/index';`,
119+
`export { ${exports.join(', ')} } from './${options.directory}/${options.componentName}/index';
120+
export type { ${typeExports.join(', ')} } from './${options.directory}/${options.componentName}/index';`,
105121
);
106122

107123
// story
@@ -121,8 +137,13 @@ function addFiles(tree: Tree, options: NormalizedSchema) {
121137
function updateBarrel(tree: Tree, options: NormalizedSchema) {
122138
const indexPath = joinPathFragments(options.paths.sourceRoot, 'index.ts');
123139
const content = tree.read(indexPath, 'utf-8') as string;
140+
141+
const { exports, typeExports } = createExportsForComponent(options);
142+
124143
let newContent = content.replace('export {}', '');
125-
newContent = newContent + `export * from './${options.componentName}';` + '\n';
144+
145+
newContent += `export { ${exports.join(', ')} } from './${options.componentName}';`;
146+
newContent += `export type { ${typeExports.join(', ')} } from './${options.componentName}';`;
126147

127148
tree.write(indexPath, newContent);
128149
}

0 commit comments

Comments
 (0)