使用angular-cli创建的角度库中的scss编译问题

时间:2019-01-02 08:01:16

标签: angular sass angular-cli angular-library

使用全局scss创建(ng build my-lib)一个有角度的lib,然后将其捆绑到一个文件中并复制到lib的文件夹中。

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

现在 my-lib.scss 导入一些资源,例如

@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 */
}

我将资源文件夹复制到了我的lib位置

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

现在,在我应用程序的 styles.scss 文件中,我正在这样导入 my-lib.scss 文件

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

但这显示了错误

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"); 

我想资源相对于文件 src / styles.scss 而不是相对于 dist-lib / my-lib / lib / sass / my-lib.scss < / strong>

对此有什么解决方案?

1 个答案:

答案 0 :(得分:0)

当我通过Angular-Cli构建/服务我的项目时,它用来处理scss的插件/加载器并没有解决相对(相对于库本身)URL(url(../))的问题。

为解决此问题,我创建了库随附的 fonts.css 而不是 fonts.scss 。最终用户也必须导入此 fonts.css

这现在正在工作,尽管这听起来不是一个好的解决方案,但直到现在我才有了这种解决方法。

更新:提供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;
  }

由于我使用了font.scss,它将使用此脚本作为lib构建过程编译为font.css文件

"build-lib-fonts": "node-sass --include-path scss ./projects/project/src/lib/styles/_fonts.scss ./dist-lib/project/styles/fonts.css",