question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error with assets loading from scss in Angular

See original GitHub issue

Current behavior

Loading assets from scss with something like this :

<div class="testClass"></div>
.testClass {
  background-image: url("/assets/test.png");
  background-size: contain;
  width: 100px;
  height: 100px;
}

Image is showing using ng serve but not when using cypress component testing

If I remove the ‘/’ in the url, image load in component testing but angular won’t compile with the following error :

./src/app/app.component.scss?ngResource - Error: Module Error (from ./node_modules/postcss-loader/dist/cjs.js):
[...]/theTester/src/app/app.component.scss:2:2: Can't resolve './assets/test.png' in '[...]/theTester/src/app'

Another loading problem comes from loading assets in scss “main” files, I have a main.scss containing :

@import "components/icon";

For simplicity purposes I didn’t use abstracts variables and mixins.

components/icons :

$imagesTest: test "/assets/test.png", testSecond "/assets/test.png";

.icon {
  top: 0px;
  position: relative;

  &.yolo {
    background-image: url("/assets/test.png");
    background-size: contain;
    background-repeat: no-repeat;
  }

  @each $class, $url in $imagesTest {
    &.#{$class} {
      background-image: url($url);
      background-size: contain;
      background-repeat: no-repeat;
    }
  }
}

app.component.html:

  ICON TEST:
  <div
    class="icon test"
    style="display: block; width: 50px; height: 50px"
  ></div>
  YOLO:
  <div
    class="icon yolo"
    style="display: block; width: 50px; height: 50px"
  ></div>

If I remove the / here, images won’t load from assets since it’s using relative path and it will try to load assets/components/test.png and ng serve won’t compile with the same error above.

Desired behavior

I would like to be able to load all my assets, I’ve given a sample app reproducing the issue below, but in my real-world app, I can’t load any of my svg that comes from scss and all the fonts I use aren’t loaded since I’m using @font-face with urls in scss and they can’t be resolved.

Test code to reproduce

Repo with code: https://github.com/theoarmengou/angular-cypress-bug-assets

When doing ng serve, you should see 4 images on localhost:4200 and when going to localhost:4200/dev, there should be 8 images.

When using yarn cypress:open in component testing, not all images are showing for app.component and dev.component.

Cypress Version

10.10.0

Node version

16.16.0

Operating System

macOS 12.5.1

Debug Logs

No response

Other

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
lmiller1990commented, Dec 15, 2022

Ahh I did it!

image

Several hacks, let me write it down before I forget.

  1. I removed the file system check here: https://github.com/bholloway/resolve-url-loader/blob/v5/packages/resolve-url-loader/index.js#L129. Now we can progress.
  2. In node_modules/@angular-devkit/build-angular/src/webpack/configs/styles.js, I used the root option in the styleLanguages array under 'scss' and set it to /__cypress/src (which is devServerPublicPathRoute inside Cypress.
  3. Confusing I still see errors in the console. Ignoring those, I visited the test, and the images are loaded.

Here’s a screenshot showing the file. Line 245 is the trick.

image

Obviously hacking into resolve-url-loader is not really an option, but this isolated this issue: Angular’s config, specifically the resolve-url-loader, does not know about the Cypress dev server url. If we can fix this somehow, it will work.

What we could do is set sourceRoot, which is just ${projectRoot}/${src}. So <CWD> + / + <SRC_ROOT>.

1reaction
warrensplayercommented, Oct 19, 2022

What is working for me is to use relative paths for the images in the SCSS files.

So app.component.scss looks like:

.testClass {
  background-image: url("../assets/test.png");
  background-size: contain;
  width: 100px;
  height: 100px;
}

And _icon.scss looks like:

$imagesTest: test "../../assets/test.png", testSecond "../../assets/test.png";

.icon {
  top: 0px;
  position: relative;

  &.yolo {
    background-image: url("../../assets/test.png");
    background-size: contain;
    background-repeat: no-repeat;
  }

  @debug $images;

  @each $class, $url in $imagesTest {
    &.#{$class} {
      background-image: url($url);
      background-size: contain;
      background-repeat: no-repeat;
    }
  }
}

See if those changes work for you as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to use assets in url() in scss after upgrading ...
Solution 1 - Using relative assets path (Recommended). The solution is to use the relative path (from your .scss file) of your assets....
Read more >
Angular workspace configuration
Each build target configuration can include an assets array that lists files or folders you want to copy as-is when building your project....
Read more >
Error occurs when importing mdb.scss to styles.scss
I'm trying to create a custom skin. Library added via npm. I use: * Angular 6 * WebStorm IDE The code: ```scss /*...
Read more >
Sass: @use
The @use rule loads mixins, functions, and variables from other Sass stylesheets, and combines ... .button { @include corners.rounded; // This is an...
Read more >
sass-loader - webpack - JS.ORG
Loads a Sass/SCSS file and compiles it to CSS. ... Since Sass implementations don't provide url rewriting, all linked assets must be relative...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found