feat(@ngtools/webpack) support of single file component #8145
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Intro
This pr add the possibility to use inline template and styles with any types. So it allows us to effectively use single file component with Angular.
You can for example do the following :
While using markdown to write angular templates doesn't seem like a great idea (but it's fun!) most people use scss for their styles. And you can even drop the type
!scss!
if you set scss as the default for styles.API
I've added
defaultStyleType
option to the cli anddefaultTemplateType
+defaultStyleType
to AOTPlugin and AngularCompilerPlugin to define default types so you don't have to set it every time.The syntax to add type is simple it's the file extension you're expecting (e.g. the one define on your loader rule) surrounded by
!
. It must be at the very start without any spacing or newlines like on the example above.Known bug
If you serve your app on aot and edit the type you"ll get an error. But this is not related to this PR and the same can be done with resources urls. The problem is that we feed the angular compiler with the previous ts program containing the compiled files before the change. So it end up asking for a file that doesn't exist anymore.
Discussion
The idea was to make it work without changing Angular at first. So we can see how this goes and then optimize it with changes to the compiler. For example this requires all components to be compiled by ts twice. That's why I don't think this pr is acceptable to merge like that, maybe we could add an experimental flag to enable it or just use this as a POC to get a feeling before implementing it on the compiler.
The good news here is that all the complex/hacky part of this PR can be easily done by the angular compiler.
So as I just said, for the moment this should be treated as a POC but I think it would be great if we could remove all those .html and .scss files and use clean single file component. The sooner the better :).
Example
I've created a repo with an example at https://github.com/ghetolay/TestAngularSFC
There is both a cli and a custom webpack build so you can play with it using any types you want, you just need to drop a loader to the config.
So what do you guys think ?