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

Remove TS types from exports when they come from node_modules #2191

Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `13.1.1`.
**Bug fixes**

- Remove exported TypeScript type and interface exports from built artifacts when they originate from `node_modules` ([#2191](https://github.com/elastic/eui/pull/2191))

## [`13.1.1`](https://github.com/elastic/eui/tree/v13.1.1)

Expand Down
24 changes: 21 additions & 3 deletions scripts/babel/proptypes-from-ts-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1100,15 +1100,33 @@ module.exports = function propTypesFromTypeScript({ types }) {
// this prevents TS-only identifiers from leaking into ES code
programPath.traverse({
ExportNamedDeclaration: (path) => {
debugger;
const specifiers = path.get('specifiers');
const source = path.get('source');
specifiers.forEach(specifierPath => {
if (types.isExportSpecifier(specifierPath)) {
const { node: { local } } = specifierPath;
if (types.isIdentifier(local)) {
const { name } = local;
const def = typeDefinitions[name];
if (isTSType(def)) {
specifierPath.remove();
if (typeDefinitions.hasOwnProperty(name)) {
// this is a locally-known value
const def = typeDefinitions[name];
if (isTSType(def)) {
specifierPath.remove();
}
} else if (types.isStringLiteral(source)) {
const libraryName = source.get('value').node;
const isRelativeSource = libraryName.startsWith('.');
if (isRelativeSource === false) {
// comes from a 3rd-party library
// best way to reliably check if this is
// a type or value is to require the
// library and check it's exports
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I didn't notice any increase in build time, but I'd expect this method to take longer, I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only executes for export {} from '' statements, which we don't really do with node_modules libraries.

const library = require(libraryName);
if (library.hasOwnProperty(name) === false) {
specifierPath.remove();
}
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions scripts/babel/proptypes-from-ts-props/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2485,6 +2485,19 @@ export type Foo = string;

expect(result.code).toBe('');
});

it('removes 3rd-party type exports', () => {
const result = transform(
`
export { DraggableLocation, Draggable, DragDropContextProps } from 'react-beautiful-dnd';
`,
babelOptions
);

expect(result.code).toBe(
`export { Draggable } from 'react-beautiful-dnd';`
);
});
});

});