-
Notifications
You must be signed in to change notification settings - Fork 12k
[Feature] Ability to reference files from assets directory from CSS/Templates OR use relative links without rewriting #5491
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
Comments
This is related to the closed issue #4858 |
I'm having a hard time understanding the exact conditions where our current options are insufficient. Can you give me a concrete example? Please include all relevant paths. |
OK to boil it down to the most basic concrete example with a single image (we will probably have more than one image in this situation): Angular appI have an application which is basically a search engine for locations our clients will be intersted in. A picture of a map pin is used in multiple places. I will use %pinurl% to refer to a picture of the map pin at 'assets/icons/map-pin.svg' (in the output). The image is located in src/assets/map-pin.svg
Hosting EnvironmentThe application is hosted in two different sites for two different sets of clients (there will be some non-trivial differences in what is presented to each). A JSP in each storefront will load the web application and set the base URL to /%contextroot%/finder where %contextroot% is 'merchants' or 'customers' depending on which storefront the user is accessing. The problemThe image is being accessed from multiple locations in the application, some of which don't support resource resolving (AFAIK URIs in HTML templates and TS files cannot be resolved, correct?) and some of which have mandatory resource resolving for relative paths.
Using file system path results in two copies of the file and requires brittle relative paths. Using root relative URLs is undesirable as it will make changing the context root difficult in the future (we intend to host multiple angular applications in a similar manner, all of them would need to be updated) and because it doubles (or triples, etc. if we add more context roots) build time. |
Writing all of that out has made me realise that potentially this could also be resolved by allowing resources to be resolved from HTML templates and TS files. This would allow consistent referencing of resources from HTML, TS and CSS removing the file duplication. Preferably this would go along with adding the ability to specify such paths relative to .angular-cli.jsons' apps.root, probably using some kind of prefix. For example my src/tsconfig.json defines a "paths" entry with "@me/" mapped to "/" so I don't need to use relative paths in my import declarations, making refactoring easier. Being able to use something like this consistently across CSS, HTML and TS would be helpful for maintainability. As such the problem can be restated as
|
It is true that resources are resolved differently across HTML, CSS and TS. CSS fully resolves and fingerprints resources, while TS and HTML don't. The HTML bit is being tracked in #3415. I don't think there's any way to truly solve this problem at the moment though. Regarding relative/absolute paths... that's also a though cookie, because it's really not about Keeping the issue open for discussion. |
We have a similar problem here. We've converted all links of form Maybe the best solution to this problem would be that (NB: when using |
@ffdybuster what was the ng command that you ran? I updated my links to use ../../ but when i ran |
@thienedits: I ended up with I forgot to mention that I was only looking at images in SCSS, since we don't have images in templates (yet). |
This is something that was fixed on webback years ago with alias paths. What makes it impossible to have alias paths relative to (compile time) app root with angular-cli, as that was the commonplace with webpack aliases? Having to use fully relative paths (../../../../../../../assets/img/xy.svg) is a MAJOR PITA. Also paths absolute to web server root (/assets/img/xy.svg) do not work on many virtual test server environments. Funnily enough, when using absolute paths + run time modified base url in index.html we get assets referred from less files working IFF they are from a lazy loaded module, but NOT when they are from non-lazy-loaded module. That is when app is run in "www.somehost.com/non-root-url/#/" of course. |
@LyraelRayne this is out of our scope for now. Please, take a look at ngx-build-plus. I believe it may help you to achieve this. If I'm missing something, and there are a lot of folks who'd want to see this feature in the CLI, please open a new issue. |
This is the second issue like with this topic that got closed. I am talking about: #3415. In fairness, you close them at the same day, but anyway. |
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. |
Bug Report or Feature Request (mark with an
x
)Versions.
Future version
Description of problem being solved
Now that CSS path rewriting works with component CSS there is no way to make plain old CSS relative links. This effectively means that in order to reference assets/ files (or files in other configured directories to be copied to dist/ on build) a root relative path must be used. This works well for standard deployments where the app will be deployed to a single base URL which is known at compile time as angular-cli will rewrite URLs.
Some deployments will either deploy to a base URL unknown at compile time or will deploy to multiple base URLs. For example J2EE applications will have a "context root" which is configurable by the server administrator. Applications built for such environments should use relative URLs only in order to work in this environment.
Desired functionality.
It is desirable that a CSS/LESS/SASS file be able to refer to assets from angular-cli.json's apps.assets (i.e. assets which will be copied to dist/ ) and have those be represented as relative links (which the browser will load from the base href at runtime).
Current workarounds
All of the below assume that the asset is in the assets folder and that it will be referenced by both HTML and CSS (which is what I currently do).
Potential solutions (somewhat in order of preference)
The text was updated successfully, but these errors were encountered: