-
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
Using templateUrl
with a .svg
file
#10567
Comments
Personally, I would definitely love to use |
When working with svg files it seems mandatory. You can not copy your file in the template each time it is modified... |
I am not understanding the use case, why an HTML template should have an |
@alan-agius4 I'm assuming the primary use case here is to create a component that is just and SVG and nothing else. Then again, nothing's stopping us from simply using a HTML file and embedding SVG into that, so I'm also not sure why this change is required. |
It’s mainly just so you don’t have to spend your time renaming files and copying stuff around. Also, it can be good to just using raw .svg files for the extra tooling you get with them. |
Having an SVG image instead of a template, might increase the final bundle size if a SVGs are large. You will lose the advantage of using HTTP2, fingerprinting etc.. which would mean that with every release you do the users will need to re-download all the svgs again. Also, by definition an SVG is a graphic since doesn't seem correct to treat it as a template. What if you than need to add an |
I’ve made this change for some tests and as far as I can tell, nothing here is really different if you use it as a .html file or a .svg? Webpack still treats it as “raw”, it is bundled the same way, you can still use bindings and Inputs, etc. The main difference is clearer intent of what the template actually is, and easier workflows when consuming assets. Using Angular bindings with SVG tags is really powerful. Yes, you can end up with big bundles, but’s that’s indenpendent of the file extension. I don’t think this change would encourage that anymore than the current situation. Are you suggesting they shouldn’t be used together just because you could end up with big bundles? A developer can do lots of things that cause large bundles, it doesn’t seem practical to stop all possible foot guns. |
My use case of importing SVG as a component template was that in our application the logo of the app has an animation while the app is loading something using REST (so I have an animated-logo component), and the same SVG image is used somewhere else as a background. Right now I inline the SVG in the component because I dont want to change the background to html, its an ugly solution. I dont see why Angular care which text file extension I use. |
Heya, I checked with the core team about SVG support in templates and the angular compiler should support it. Would someone be interested in adding the feature? I ask that you test it in JIT and AOT. A good test to use as an example is https://github.com/angular/angular-cli/blob/master/packages/angular_devkit/build_angular/test/browser/allow-js_spec_large.ts. You can copy it, add |
Yeah I’ll have a crack! |
@alan-agius4 brought up a problematic issue: how to differentiate between SVG being loaded as a templateUrl (inlined as raw) or as a resource in CSS (loaded as an extra asset file). I'm not sure there's a good way of doing that. It might need a fair bit of work even. |
This SO answer looks like it solves that issue: https://stackoverflow.com/questions/36281354/use-different-loader-in-webpack-depending-on-from-where-im-importing |
Differentiating between .svg used as component templates and other .svg is pretty easy if you follow the convention to name component files like somename.component.(.ts|.html|.svg|.css|.less|...). Just add a new webpack rule that tests for *.component.svg. |
To give an example, real world use case: We have an application that is used to create process documentation for how a company processes its invoices. It does that by asking the user multiple choice questions which are then used to create a process documentation (a word file). We use an .svg image to show a process diagram for processing invoices. In the .svg, we show/hide icons to indicate for which part of the process the user has already answered questions. You can even click on parts of the .svg to jump to the questions for that part of the invoice process (we did this by applying angular directives inside the .svg). The .svg was created by our marketing department by simply using the grarphics tools they are used to work with. Our developer could then use that .svg with some minor modifications. We created the app with a manual webpack configuration, I think Angular cli did not yet exist back then or was not mature enough (we started to use Angular around the time when the first version of Angular 2.0 was released). |
Components can now use .svg files as templates. To distinguish .svg files in templates from .svg files used for other purposes, a new webpack loader rule was added that tests for .component.svg and loads these files with raw-loader. All other .svg files are loaded as before with file-loader. For use cases and alternative solutions, see the discussion in angular#10567 Closes angular#10567
Components can now use .svg files as templates. To distinguish .svg files in templates from .svg files used for other purposes, a new webpack loader rule was added that tests for .component.svg and loads these files with raw-loader. All other .svg files are loaded as before with file-loader. For use cases and alternative solutions, see the discussion in angular#10567 Closes angular#10567
Components can now use .svg files as templates. If directTemplateLoading is enabled, the .svg is loaded directly by the Angular compiler host, otherwise it uses the webpack raw-loader to load the .svg file. Closes angular#10567
With directTemplateLoading enabled, components can now use .svg files as templates. For AOT builds, the Angular compiler host now reads .svg files directly when reading component templates. For JIT builds, replaceResources creates a require call that directly uses raw-loader instead of using the loader provided by the current webpack configuration. Closes angular#10567
With directTemplateLoading enabled, components can now use .svg files as templates. For AOT builds, the Angular compiler host now reads .svg files directly when reading component templates. For JIT builds, replaceResources creates a require call that directly uses raw-loader instead of using the loader provided by the current webpack configuration. Closes angular#10567
With directTemplateLoading enabled, components can now use .svg files as templates. For AOT builds, the Angular compiler host now reads .svg files directly when reading component templates. For JIT builds, replaceResources creates a require call that directly uses raw-loader instead of using the loader provided by the current webpack configuration. Closes angular#10567
With directTemplateLoading enabled, components can now use .svg files as templates. For AOT builds, the Angular compiler host now reads .svg files directly when reading component templates. For JIT builds, replaceResources creates a require call that directly uses raw-loader instead of using the loader provided by the current webpack configuration. Closes angular#10567
With directTemplateLoading enabled, components can now use .svg files as templates. For AOT builds, the Angular compiler host now reads .svg files directly when reading component templates. For JIT builds, replaceResources creates a require call that directly uses raw-loader instead of using the loader provided by the current webpack configuration. Closes #10567
With Angular CLI 8.0.0-beta.5, you can now use .svg files as templates. You can read more about the new feature in my blog on medium: https://medium.com/@oocx/using-svg-files-as-component-templates-with-angular-cli-ea58fe79b6c1 |
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. |
With directTemplateLoading enabled, components can now use .svg files as templates. For AOT builds, the Angular compiler host now reads .svg files directly when reading component templates. For JIT builds, replaceResources creates a require call that directly uses raw-loader instead of using the loader provided by the current webpack configuration. Closes angular#10567
Hey friendly @angular/cli team!
Would there be any appetite to enable using a
.svg
file as a component template?As far as I can tell it would be a small change in
webpack-config/common.js
from:to
Are there any downsides to this? Are there any other places that the change would need to be handled? It seems to work when I change it in my node_modules.
Let me know if this is something that would be useful/wanted, and I'll make a PR.
Thanks!
The text was updated successfully, but these errors were encountered: