-
Notifications
You must be signed in to change notification settings - Fork 12k
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
Feature: add default export option when using a schematic to generate a component #25023
Comments
//cc @dgp1130 as he had some strong opinions about default exports in general as they go against G3 guidelines. |
The google3 TypeScript style guide generally disallows default exports and provides some justification. I think all of those points generally apply outside of Google as well. Angular style guide doesn't seem to provide any guidance here, but it does include the line "Do define one thing, such as a service or component, per file" with one reasoning for it being "A single component can be the default export for its file which facilitates lazy loading with the router", which at least tells me that default exports aren't disallowed. Additionally external to google3 we have CommonJS interop, which is a major challenge for default exports. These days, Angular is all-in on ESM so that isn't a major issue, but if devs choose to use default exports in their application, it easily bleeds over into other CommonJS libraries they might be using and introduce those incompatibilities, so I still generally would not recommend default exports, solely for that reason. That said, personally I'm ok with default exports when they avoid magic names. For example, if we had an API like Recently we did add support for default exports in the router, so I can understand wanting to take advantage of that out of the box when generating a component. I can see some value to the idea that "all route components should use default exports". However the core philosophical concern I have is that defining a component is independent of defining a route. The component decides whether or not it is the default export, yet the route is the one which is simplified by consuming a default exported component. It is entirely possible to have a component used as a route in one location, but then also used as a sub-component in another location, yet this presents a contradiction between "all route components should use default exports" and "all non-routes should use named exports", since the component is used in both contexts. This makes me think that We've discussed adding I'm curious to hear @atscott, @alxhub, and @jelbourn's opinions on this. I think a lot of it comes down to:
Ultimately, like most things with schematics this comes down to a style question, and the answer to that question is what should inform what we support in schematics. |
I think this is mostly a CLI question (aka a Doug question) since it most heavily impacts the build and bundling aspect of Angular rather than any framework concepts. My inclination is to not introduce any restrictions without a strong technical reason to do so.
I don't know about encourage. I don't love 3P guidance diverging from Google internal guidance (over which we have limited sway). My inclination is to take no formal stance from a style guide perspective.
I think adding an option is reasonable. I don't love the idea of |
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list. You can find more details about the feature request process in our documentation. |
Hi @alan-agius4, |
We had some more discussion today based on recent community interest in this. We're ok with adding a form of Longer term, it might be worth adding some kind of If anyone would like to contribute an |
Awesome. Thanks for the update and consideration! |
Hi @dgp1130, Thanks. |
@aparzi, you should definitely add some tests to https://github.com/angular/angular-cli/blob/e40384e637bc6f92c28f4e572f986ca902938ba2/packages/schematics/angular/component/index_spec.ts, which should provide some reasonable confidence. If you want to test it out in your own application, More details: https://github.com/angular/angular-cli/blob/main/docs/DEVELOPER.md |
@dgp1130 ok thanks. |
added option for export class in default mode Closes angular#25023
Introduces option `--export-default` to control whether the generated component uses a default export instead of a named export. Closes: angular#25023
Introduces option `--export-default` to control whether the generated component uses a default export instead of a named export. Closes: angular#25023
Introduces option `--export-default` to control whether the generated component uses a default export instead of a named export. Closes: angular#25023
Introduces option `--export-default` to control whether the generated component uses a default export instead of a named export. Closes: angular#25023
Hi @alan-agius4, Thanks |
The option will automatically be updated. |
Awesome |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Command
generate
Description
When I want to generate a component to use as a lazy loaded component with
loadComponent
I can use the schematicThen when I go to use the component in the route config, I immediately get an error because its not a default export
Describe the solution you'd like
Add an option to generate a component with the class as a default export
Describe alternatives you've considered
Manually update the component after generation
The text was updated successfully, but these errors were encountered: