angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Can't use relative paths in url() in scss files

Open yentheo opened this issue 8 years ago • 41 comments

Bug Report or Feature Request (mark with an x)

- [ ] bug report -> please search issues before submitting
- [x] feature request

Command (mark with an x)

- [x] new
- [x] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Versions

npm: 6.4.1 node: v10.7.0 ng cli: 6.2.6 macOS High Sierra

Repro steps

  1. Create new project

ng new ng-sass-test--style=scss

  1. Add following files:
  - src
    - sprite
      - _sprite.scss
      - sprite.svg

With the following content:

# _sprite.scss
.sprite-common {
    background-image: url('sprite.svg');
}

Add the following line to src/styles.scss. @import '~src/sprite/sprite'; Add the same line to src/app/app.component.scss.

The log given by the failure

ERROR in ./src/styles.scss (./node_modules/raw-loader!./node_modules/postcss-loader/lib??embedded!./node_modules/sass-loader/lib/loader.js??ref--15-3!./src/styles.scss)
Module Error (from ./node_modules/postcss-loader/lib/index.js):
(Emitted value instead of an instance of Error) CssSyntaxError: /***/ng-sass-test/src/sprite/_sprite.scss:2:22: Can't resolve 'sprite.svg' in '/***/ng-sass-test/src'

  1 | .sprite-common {
> 2 |     background-image: url('sprite.svg');
    |                      ^
  3 | }

ERROR in ./src/app/app.component.scss
Module Error (from ./node_modules/postcss-loader/lib/index.js):
(Emitted value instead of an instance of Error) CssSyntaxError: /***/ng-sass-test/src/sprite/_sprite.scss:2:22: Can't resolve 'sprite.svg' in '/***/ng-sass-test/src/app'

  1 | .sprite-common {
> 2 |     background-image: url('sprite.svg');
    |                      ^
  3 | }

Desired functionality

I would like to be able to use the relative file path in the scss file. I know this is solved by using an absolute path for the image. But I'm using the package svg-sprite, I'm not an expert in the package, but it looks like it doesn't support generation of an absolute path. Maybe I'm missing something in the angular-cli setup, so I can use this way of path-handling.

Mention any other details that might be useful

yentheo avatar Oct 29 '18 17:10 yentheo

Just had a quick look at this, at this is tricky one.

Since in ./src/app/app.component.scss if you want to use relative url, the image src, should be ../sprite/sprite.svg, while forstyle.scss it should be ./sprite/sprite.svg.

alan-agius4 avatar Oct 29 '18 18:10 alan-agius4

Well yes, if I use a relative path in the one sass-file, it uses it as relative from every other file where I import it. Isn't there a way to just have it relative to the single file with the url()?

yentheo avatar Oct 30 '18 10:10 yentheo

I think the recommended approach here is to put these sprite file in the assets folder, and use root level paths (starting with /) for urls in sass files that are imported by other sass files.

In scss files at an absolute path;

background-image: url('/sprite/sprite.svg');

in your angular.json add the following so that the sprite is copied, under architect -> build -> options

"assets": [
    "src/sprite/sprite.svg"
],

From svg-sprite package point of view, there will be no changes.

alan-agius4 avatar Oct 30 '18 10:10 alan-agius4

The problem with that is that I can't change the sprite.scss, it is generated by the svg-sprite package. For as far as I know, it does not have the option to generate with an absolute path.

yentheo avatar Oct 30 '18 10:10 yentheo

The simplest way to do it is to copy https://github.com/jkphl/svg-sprite/blob/6966592489dfcf2cd9ed2bba500cb8ee31c00b9c/tmpl/css/sprite.scss and add a lead slashes / and when using svg-sprite you provide this template via configuration example

config            = {
	mode: {
        css: { // Create a «css» sprite
            render: {
                scss : {
                    template: 'my-custom.scss',
                }
            },
        },
    },
},

alan-agius4 avatar Oct 30 '18 11:10 alan-agius4

Tried it out, works perfect. Thanks for the idea @alan-agius4 !

yentheo avatar Nov 05 '18 09:11 yentheo

Doesn't work same issue with absolute paths (at least behaviour is inconsistent with angular6 ). Happened after upgrade to 7. I think what changed is automatically adding resolved scss assets to assets option/public folder. Hence people reporting having to add each file to assets (At least in the past we didn't need to).

agalazis avatar Jan 11 '19 14:01 agalazis

btw I am not getting any errors for entirely wrong paths either as I used to. seeing this: https://github.com/angular/angular-cli/issues/12746 makes me think that updating angular +sass is a nightmare are there any instructions for updating? could this be caused by upstream projects as well?

agalazis avatar Jan 11 '19 15:01 agalazis

is sass loader what angular uses internally? https://github.com/webpack-contrib/sass-loader#problems-with-url

agalazis avatar Jan 11 '19 17:01 agalazis

any update on this issue ?

salilgaurav avatar Jan 17 '19 15:01 salilgaurav

I’m also interested on this, since Stylus and LESS work fine with relative URLs and SASS doesn’t. For ejected projects resolve-url-loader does the trick.

jussikinnula avatar Feb 04 '19 06:02 jussikinnula

I get the following, rocking Angular CLI: 7.3.0, Angular: 7.2.3.

// angular.json
"assets": [
    "src/assets"
]
background-image: url('/src/assets/images/icon.png');

// GET http://localhost:3000/src/assets/images/icon.png 404 (Not Found)
background-image: url('../../../../assets/images/icon.png');

// GET http://localhost:3000/icon.png 200 OK

ruimarques avatar Feb 07 '19 13:02 ruimarques

As @agalazis, correctly pointed out the issue. Sass itself doesn't provide Url rewiring. While indeed resolve-url-loader might work in some cases, this relies on a non standard behaviour as it uses sourcemaps to rewire the paths.

alan-agius4 avatar Mar 13 '19 07:03 alan-agius4

@ruimarques, I suggest you open a separate issue with a reproduction for the issue you are encountering.

alan-agius4 avatar Mar 13 '19 08:03 alan-agius4

@alan-agius4 another point to mention in the past svgs in my css but after giving up and setting assets path now they are loaded as links after build

agalazis avatar Mar 13 '19 19:03 agalazis

Please consider not removing rebaseRootRelativeCssUrls build option (https://github.com/angular/angular-cli/issues/14587) until this issue is fixed. Since this issue prevents using relative URLs, the rebaseRootRelativeCssUrls option is the only workaround when needing to specify a base-href when building.

acohenOT avatar Jun 04 '19 16:06 acohenOT

background-image: url('~/assets/images/logo.svg');

works for me (version 8.0.1)

iwnow avatar Jun 14 '19 11:06 iwnow

@iwnow This has been said to cause the app to be deployed with images copies to the dist folder (out of the assets folder) meaning you have multiple copies for no reason.

Angular Team: Please fix this, it's very sad that it's now "The recommended method to transition is to use relative paths within the source stylesheet".

This ticket has been here for well over a year. What was the plan? Was there one? It seems the idea was to just break AngularCLI and say use this method that doesn't work and hope for the best.

michael-letcher avatar Jun 19 '19 23:06 michael-letcher

@michael-letcher yes I understand but I have no multiple copies, all works as expected this is a simple workaround, but I still want this to be fixed

iwnow avatar Jun 26 '19 17:06 iwnow

background-image: url('../../assets/img/bg-masthead.jpg'); worked for me. Very annoying.

Dada-Tech avatar Aug 05 '19 03:08 Dada-Tech