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

Allow to import specific file from library (#4562) #4573

Closed

Conversation

ershov-konst
Copy link

Related issue

#4562

Description

This PR provides a more selective way to import files from libraries in TypeSpec. The current mechanism only allows importing the main.tsp file from a library, resulting in unnecessary entities being bundled into the output schema, leading to bloated schemas and reduced efficiency in language services.

Developers can now import specific files from libraries, allowing for greater granularity and flexibility, reducing build complexity and improving performance. The new file import mechanism uses a new syntax for import statements, allowing developers to specify the path of the file they want to import.

⚠️ Note that this PR does not contradict the approach of using the exports field in package.json for node.js libraries, which will be implemented in a separate PR. This PR only enhances the file import mechanism for TypeSpec.

Example

import "@org/typespec-library/lib/entities/particular-file.tsp";
import "@org/typespec-library/lib/version.tsp";

@ershov-konst
Copy link
Author

@microsoft-github-policy-service agree

@timotheeguerin
Copy link
Member

Hi thanks for the contribution! However I am not sure this is the best idea. As said in the issue we do want to add support for explicit exports by respecting node js package.json exports field and this goes against that behavior and allow anyone to import anything from a library easily with no control from the author.

@bterlson
Copy link
Member

Yeah, my experience with arbitrary file imports in the node ecosystem was not so good, I don't think we want to duplicate that here. As a library author, making all files (including file names) part of your "public surface area" is quite a burden. @ershov-konst do you think using package.json exports would solve your use case?

@ershov-konst
Copy link
Author

Thank you for considering my contribution. I understand your concerns regarding arbitrary file imports.
Just for context, we are working on a large library, which represents a standardized format for defining services and products within the insurance industry. It contains hundreds of models and enums.

We are open to exploring other options, such as using the package.json exports field. Still, we believe that the ability to import specific files from our library would significantly improve consumer experience and reduce build complexity from our side.

@ershov-konst ershov-konst force-pushed the feature/import-files-from-lib branch from 5a459c2 to 12ecfee Compare October 1, 2024 08:17
@timotheeguerin
Copy link
Member

It does contradict the named exports as with esm specification you cannot load local package files as soon as the exports field is provided.

Anyway I have a pr adding the exports field #4606 support and should solve the issue. I'll close this PR in the meantime. Thanks again though for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:core Issues for @typespec/compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants