CSS images with relative path not included in production build
Please provide us with the following information:
OS?
Windows 10
Versions.
1.0.0-beta.32.3
Repro steps.
- Create new app with
ng new.- Add an image anywhere outside the
appdirectory, e.g. in theassetsdirectory.- Reference this image from the
app.component.css, using a relative path. Example:background-image: url(../assets/some_image.png)- Do the following:
ng build --prod.- Verify that the image is not copied along with the CSS in the build output, meaning the image reference is now broken.
The log given by the failure.
N/A
Mention any other details that might be useful.
The image is correctly copied if doing
ng buildwithout the--prodswitch.
Thanks! We'll be in touch soon.
@filipesilva -- I experienced the issue where the images filenames in CSS (actually SCSS if that matters) were being hashed, but the actual image file names were not being hashed on build.
To work around this issue I used absolute file names in CSS, example /assets/logo.png and built with the -oh=bundles flag, rather than --no-aot, to prevent webpack from adding the hash to the path in CSS.
This workaround works for me, though I don't know the internals of Angular-CLI or the webpack config that is used -- so I understand I may be mistaken on the specifics.
@martinmcwhorter I see now, yeah. Your workaround would work, since you're taking the resource extraction out of the equation. You shouldn't have to do that though... next release it should be ok.
Yep, I'm looking forward to hashed images :)
On Wed, 22 Feb 2017, 11:05 Filipe Silva, [email protected] wrote:
@martinmcwhorter https://github.com/martinmcwhorter I see now, yeah. Your workaround would work, since you're taking the resource extraction out of the equation. You shouldn't have to do that though... next release it should be ok.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/4806#issuecomment-281638319, or mute the thread https://github.com/notifications/unsubscribe-auth/ABI5-pZE-2PvKnpnrgKk6EA0bSAO06tLks5rfBZcgaJpZM4MFDL4 .
So is there a way to configure the cli to end up with relative paths in the CSS, e.g. url('assets/...')?
Hey guys! I'm new to angular and I'm facing the same problem in my app where the local version works perfectly great whereas the github deployment breaks all the URLs for images. My images are placed in src/assets/xyz.jpg directory and I have tried changing the base href as well as my image url and still the image appears to be broken.
I'm using angular 6. Would be really thankful if I get my issue sorted out.
Thanks in advance.
Hey guys! I'm new to angular and I'm facing the same problem in my app where the local version works perfectly great whereas the github deployment breaks all the URLs for images. My images are placed in src/assets/xyz.jpg directory and I have tried changing the base href as well as my image url and still the image appears to be broken.
I'm using angular 6. Would be really thankful if I get my issue sorted out.
Thanks in advance.
i have this issue too
No news on this issue? I am seeing the same thing on Angular 6. Building on Unix seems to be broken, but strangely, I never have the issue when building on Windows.
Oddly enough, when I reference assets in this fashion:
url("/assets/myimage.jpg")
instead of
url("../../../assets/myimage.jpg")
it works fine locally, and during my unix production build.
Similar to @frostbytedata but I'm on Angular 7 and my url still differed slightly to get it working.
url("assets/myimage.png")
Having that preceding forward slash before assets was breaking for me, along with the other method of the url("../../assets/myimage.png")
Im having to hardwire with an appRootPrefix in environment.ts in order to serve up internationalized language files beneath my assets folder and various sporadic app images off the template or from within the component fed to the template.
export const environment = {
appRootPrefix: '/api',
}
and then
ngOnInit() {
this.image.nativeElement.setAttribute(
'src',
`${environment.appRootPrefix}/assets/logo-pp.png`
)
}
or
this.image.nativeElement.setAttribute(
'src',
`${environment.appRootPrefix}/assets/avatar.png`
)
or
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(
http,
`${environment.appRootPrefix}/assets/i18n/`,
'.json'
)
}
not good for production
C:\apache-tomcat-9.0.14\api>ng --version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ ? \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 7.2.3
Node: 8.12.0
OS: win32 x64
Angular: 7.2.2
... animations, cdk, common, compiler, compiler-cli, core, forms
... http, language-service, material, platform-browser
... platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.12.3
@angular-devkit/build-angular 0.12.3
@angular-devkit/build-optimizer 0.12.3
@angular-devkit/build-webpack 0.12.3
@angular-devkit/core 7.2.3
@angular-devkit/schematics 7.2.3
@angular/cli 7.2.3
@angular/flex-layout 7.0.0-beta.19
@ngtools/webpack 7.2.3
@schematics/angular 7.2.3
@schematics/update 0.12.3
rxjs 6.3.3
typescript 3.2.4
webpack 4.28.4
C:\apache-tomcat-9.0.14\api>
No news on this issue? I am seeing the same thing on Angular 6. Building on Unix seems to be broken, but strangely, I never have the issue when building on Windows.
Oddly enough, when I reference assets in this fashion:
url("/assets/myimage.jpg")instead of
url("../../../assets/myimage.jpg")it works fine locally, and during my unix production build.
It worked in my case too. After removing ellipsis, angular7 found this asset in no time. Amazing! Thank you @frostbytedata for sharing this.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.