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.
Heya @arntj, can you do me a favor and verify the image really is broken within you app? For images smaller than 10kb we actually inline them, which is why it wouldn't be copied over.
Yes, I loaded it in an HTTP server and verified that the image was broken in the web browser. The image I tested it with is 60 kb.
The production output CSS links to a hashed image file that doesn't exist in the output dir.
I can verify this happens:
touch ./src/assets/img.svg(using svg here because it always loads as a file regardless of size)- add
h1 { background-image: url(../assets/img.svg) }to./src/app/app.component.css ng build- verify
./dist/img.svgexists and it's referenced in./dist/main.bundle.js ng build --prod- verify
./dist/img.[hash].svgdoes NOT exist but it's referenced in./dist/main.[hash].bundle.js
Will investigate.
This seems to be related with AoT.
Workarounds for now:
- use
ng build --prod --no-aot - use an absolute path for the image (starting with
/)
In my case, cause I use base-href './' the workaround is as follows:
- use
ng build --prod --no-aot --base-href ./ - use a relative path to the images folder (i.e.
../assets/images/img.*)
This is a little different but maybe related?
OS?
Windows 10
Versions.
1.0.0-beta.32.3
Upgrading project from beta-31 - my issue showing up in dev when running ng serve
Under my src directory I have a theme file: ui-framework-theme.css It is referenced in my index.html as <link href="ui-framework-theme.css" rel="stylesheet" />
(apologies if this is bad practice - I'm learning)
Was getting a 404 error on the file until I changed it to
<link href="/src/ui-framework-theme.css" rel="stylesheet" />
I referenced my images in html like <img src="../../someimage.svg"> and webpack is also not emitting this file.
@filipesilva Does this affect images in html as well?
@janwo no, images in html aren't processed at all.
@filipesilva It would be nice to have the same processing of images referred to from CSS versus HTML. Ideally with the typical webpack processing for both, alternatively disabling asset URL processing for both would be more consistent and easier to understand and explain than the current feature set. Is a move toward one of those things on the roadmap?
@kylecordes it would be nice if we could have HTML processing on part with CSS processing but I haven't looked into it much.
I had a short look at https://github.com/webpack-contrib/html-loader but wasn't too sure of the interactions between image urls and interpolated properties.
If someone knows how safe it is, I'd be happy to add it in.
So if I understand right, the obstacle is: we need to be confident that HTMLLoader (or a fork thereof) applies its usual processing to plain URLs, but leaves any attribute values that look like an Angular expression, unchanged.
Here are its test cases:
https://github.com/webpack-contrib/html-loader/blob/master/test/loaderTest.js
It appears as though there is already a way to pass a config which tells it to ignore expressions that match a pattern:
https://github.com/webpack-contrib/html-loader/blob/master/test/loaderTest.js#L68
The example used in the test case tells it to leave expressions that include "{{" untouched. I wonder if that bit of config would be enough for what CLI needs?
The HTML templates are inherently dynamic, as a result static analysis to determine asset usage is exceedingly error prone. Combine that will the vast multitude of methods to reference assets (and their varying foms) and attempts at HTML template static analysis becomes far more trouble than it's worth.
@clydin Certainly there will always be cases where a template can be made to referenced an asset that could not be determined statically. But I have applications that have highly numerous assets that are always mentioned statically from HTML templates. Handling those the same way as assets mentioned from CSS would be a great improvement to ergonomics.
Also related, this person suggests a set of HtmlLoader settings to work with Angular templates:
https://github.com/webpack-contrib/html-loader/issues/67#issuecomment-277502048
Rather than --no-aot shouldn't -oh bundles be a sufficient workaround with absolute image paths?
@kylecordes I had a preliminary look at html-loader and it seems to be ok with Angular templates overall.
I couldn't find how to rewrite absolute urls without processing them though. I had to do that for absolute styles in this bit of code: https://github.com/angular/angular-cli/blob/master/packages/@angular/cli/models/webpack-configs/styles.ts#L50-L60
Do you know of a similar way to do it with html? The root query parameter won't work since afterwards it will process the url.
@martinmcwhorter I don't see how that would work. Can you elaborate?
Something to note from an app design standpoint is that outside actual page content (which will be overwhelming dynamic anyway), css background images and icon fonts (SVG in particular for custom design elements) are a far more flexible alternative to static HTML img elements.
Yeah it wouldn't be a big problem in image urls on html would always need to be absolute.