0

Created(ng build my-lib) an angular lib with global scss then bundled into one file and copied to lib's folder.

dist-lib/my-lib/lib/sass/my-lib.scss

Now my-lib.scss imports some resources like

@font-face {
    font-family:"Roboto Regular";
    font-weight:normal;
    font-style:normal;
    src: url("../resources/fonts/roboto-regular/Roboto-Regular.eot?") format("eot"),  /* IE9 Compat Modes */
         url("../resources/fonts/roboto-regular/Roboto-Regular.woff") format("woff"), /* Pretty Modern Browsers */
         url("../resources/fonts/roboto-regular/Roboto-Regular.ttf") format("truetype");  /* Safari, Android, iOS */
}

I copied the resources folder to my lib at location

dist-lib/my-lib/lib/resources
                             /fonts
                                   / roboto-regular/Roboto-Regular.eot

Now in my Application's styles.scss file I am importing my-lib.scss file like this

@import "../dist-lib/my-lib/lib/sass/my-lib";

But this is showing error

ERROR in ./src/styles.scss (./node_modules/@angular-devkit/build-angular/src/angular-cli-files/plugins/raw-css-loader.js!./node_modules/postcss-loader/src??embedded!./node_modules/sass-loader/lib/loader.js??ref--14-3!./src/styles.scss)
Module Error (from ./node_modules/postcss-loader/src/index.js):
(Emitted value instead of an instance of Error) CssSyntaxError: D:\SK\Study\Angular\AngularLib\MyLib\dist-lib\my-lib\lib\sass\my-lib.scss:10:9: 
Can't resolve '../resources/fonts/roboto-regular/Roboto-Regular.woff' in 'D:\SK\Study\Angular\AngularLib\MyLib\src'

   8 |     font-style:normal;
   9 |     src: url("../resources/fonts/roboto-regular/Roboto-Regular.eot?") format("eot"),  /* IE9 Compat Modes */
> 10 |          url("../resources/fonts/roboto-regular/Roboto-Regular.woff") format("woff"), /* Pretty Modern Browsers */
     |         ^
  11 |          url("../resources/fonts/roboto-regular/Roboto-Regular.ttf") format("truetype"); 

I guess the resources are getting resolved relative to file src/styles.scss instead of relative to dist-lib/my-lib/lib/sass/my-lib.scss

What is the solution for this?

1 Answer 1

1

As I am building/serving my project through Angular-Cli, so the plugins/loaders that it uses to deal scss was not resolving the issues of relative (to the library itself) URLs (url(../)).

To solve this I created fonts.css not fonts.scss that I ship with the library. And end user has to import this fonts.css as well.

And this is working now, though this does not sound a good solution, I got this workaround till the time.

Update : Providing font.scss

@font-face {
    font-family: 'Proxima Nova';
    src: url('../assets/fonts/proxima-nova-light/ProximaNova-Light.eot');
    src: url('../assets/fonts/proxima-nova-light/ProximaNova-Light.eot?#iefix') format('embedded-opentype'),
        url('../assets/fonts/proxima-nova-light/ProximaNova-Light.woff2') format('woff2'),
        url('../assets/fonts/proxima-nova-light/ProximaNova-Light.woff') format('woff'),
        url('../assets/fonts/proxima-nova-light/ProximaNova-Light.ttf') format('truetype'),
        url('../assets/fonts/proxima-nova-light/ProximaNova-Light.svg#ProximaNova-Light') format('svg');
    font-weight: 300;
  }

  @font-face {
    font-family: 'Proxima Nova';
    src: url('../assets/fonts/proxima-nova-regular/ProximaNova-Regular.eot');
    src: url('../assets/fonts/proxima-nova-regular/ProximaNova-Regular.eot?#iefix') format('embedded-opentype'),
        url('../assets/fonts/proxima-nova-regular/ProximaNova-Regular.woff2') format('woff2'),
        url('../assets/fonts/proxima-nova-regular/ProximaNova-Regular.woff') format('woff'),
        url('../assets/fonts/proxima-nova-regular/ProximaNova-Regular.ttf') format('truetype'),
        url('../assets/fonts/proxima-nova-regular/ProximaNova-Regular.svg#ProximaNova-Regular') format('svg');
    font-weight: 400;
  }

  @font-face {
    font-family: 'Proxima Nova';
    src: url('../assets/fonts/proxima-nova-medium/ProximaNova-Medium.eot');
    src: url('../assets/fonts/proxima-nova-medium/ProximaNova-Medium.eot?#iefix') format('embedded-opentype'),
    url('../assets/fonts/proxima-nova-medium/ProximaNova-Medium.woff2') format('woff2'),
    url('../assets/fonts/proxima-nova-medium/ProximaNova-Medium.woff') format('woff'),
    url('../assets/fonts/proxima-nova-medium/ProximaNova-Medium.ttf') format('truetype'),
        url('../assets/fonts/proxima-nova-medium/ProximaNova-Medium.svg#ProximaNova-Medium') format('svg');
        font-weight: 500;
  }

  @font-face {
    font-family: 'Proxima Nova';
    src: url('../assets/fonts/proxima-nova-bold/ProximaNova-Bold.eot');
    src: url('../assets/fonts/proxima-nova-bold/ProximaNova-Bold.eot?#iefix') format('embedded-opentype'),
        url('../assets/fonts/proxima-nova-bold/ProximaNova-Bold.woff2') format('woff2'),
        url('../assets/fonts/proxima-nova-bold/ProximaNova-Bold.woff') format('woff'),
        url('../assets/fonts/proxima-nova-bold/ProximaNova-Bold.ttf') format('truetype'),
        url('../assets/fonts/proxima-nova-bold/ProximaNova-Bold.svg#ProximaNova-Bold') format('svg');
    font-weight: 700;
  }

As I have used font.scss that will be compiled to font.css file as the lib build process using this script

"build-lib-fonts": "node-sass --include-path scss ./projects/project/src/lib/styles/_fonts.scss ./dist-lib/project/styles/fonts.css",
Sign up to request clarification or add additional context in comments.

1 Comment

Can you please share the content of your font.css file? I am hitting the same problem, but I don't understand how to fix. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.