Skip to content

Implement splitting types per file per namespace #105

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

gvlasov
Copy link

@gvlasov gvlasov commented Jun 7, 2025

Problem: When you have to use ModuleWriter (e.g. to get usable enums instead of only type definitions), you can't have types with the same base name but from different PHP namespaces.

Solution: Group code per namespace and output it in a corresponding directory structure, where the last part of the namespace is used to name the output .ts file for the namespace.

Example output structure:

src/app/resources/ts/dto/App/Data/Products.ts
src/app/resources/ts/dto/App/Data/Products/Attributes.ts
src/app/resources/ts/dto/App/Data/Users.ts

Introduces new config option:

    'split_modules_base_dir' => resource_path('ts/dto/'),

If it is set, TypeScriptTransformer ignores output_file config option and uses an alternative strategy for bundling the code that puts types from every namespace into their own file, with directory structure mimicking the namespace parts.

Otherwise puts all code into output_file as usual.

Notes

  • Works well with Introduce Compactor to shorten output namespaces and type names #104 producing very simple output directory structure.
  • This implementation is good enough for my use case, but if you give me specific missing test cases and other changes/redesign that need to be implemented for this to be mergeable, I'm open to finishing it according to your standards.

gvlasov added 6 commits June 6, 2025 05:55
# Conflicts:
#	src/Structures/TransformedType.php
#	src/Transformers/DtoTransformer.php
#	src/Transformers/EnumTransformer.php
#	src/Transformers/MyclabsEnumTransformer.php
#	src/Transformers/SpatieEnumTransformer.php
#	tests/Fakes/FakeTransformedType.php
#	tests/Fakes/FakeTypeScriptCollector.php
@gvlasov
Copy link
Author

gvlasov commented Jun 10, 2025

Had to integrate #104 here because locating module paths requires logic from there in order to work with compacted namespaces, these two are a single PR now.

@gvlasov gvlasov force-pushed the file-per-namespace branch from aa31690 to b16ce5d Compare June 11, 2025 00:41
@gvlasov gvlasov force-pushed the file-per-namespace branch from b16ce5d to 5dfc1b4 Compare June 11, 2025 00:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant