【问题标题】:How to add scss of Angular library to bundle using ng-packagr?如何使用 ng-packagr 将 Angular 库的 scss 添加到捆绑中?
【发布时间】:2021-06-04 06:34:34
【问题描述】:

我正在尝试创建基于 scss 主题的 Angular(ng 11) 库,我将通过私有 npm 注册表在单独的项目中使用该库。 现在我正在尝试将一个全局 scss 文件添加到我的库中并将其与我的库捆绑在一起。

我想要什么:

假设我将我的 scss 保存在 projects/my-lib/src/styles/_material.scss 中:

// Import library functions for theme creation.
@import '~@angular/material/theming';

// Include non-theme styles for core.
@include mat-core();

// Define your application's custom theme.
$primary: mat-palette($mat-indigo);
$accent: mat-palette($mat-pink, A200, A100, A400);
$theme: mat-light-theme($primary, $accent);

// Include theme styles for Angular Material components.
@include angular-material-theme($theme);

我希望我的包(库包)包含此 scss,以便在安装此库后,应用程序可以将其导入到其全局 style.scss 文件中。

projects/my-app/src/style.scss

@import 'my-lib/_material.scss'

我做了什么:

我从 angular Managing assets in library 关注了这个文档

library package.json(为了简单起见,我现在将 scss 文件保存在 src 文件夹之外):

{
  "$schema": "./node_modules/ng-packagr/package.schema.json",
  "name": "ui-sdk",
  "version": "0.0.1",
  "ngPackage": {
    "assets": [
      "CHANGELOG.md",
      "./styles/material.scss"
    ],
    "lib": {
      "entryFile": "src/public-api.ts"
    }
  },
  "peerDependencies": {
    "@angular/common": "^9.0.6",
    "@angular/core": "^9.0.6",
    "tslib": "^1.10.0",
    "ng-packagr": "^11.2.4"
  },
  "dependencies": {
  }
}

进行这些更改后,我面临 2 个问题:

  1. ng build 正在“projects/my-lib/dist”下添加库包
  2. 库包不包含库文件夹 (my-lib),因此无法解析路径。

有人能解释一下我们如何创建一个库,我可以在其中添加材料主题和扩展这些主题的应用程序可以使用该主题吗? 我已经尝试过官方文档以及其他来源,但没有找到解决方案。

任何帮助或参考将不胜感激!

【问题讨论】:

    标签: angular angular-material angular-library ng-packagr


    【解决方案1】:

    要从您的导入应用程序中获取您的主题 SCSS 文件,您需要这样做;

    创建SCSS 主题文件(您已经完成)

    使用ng-package.json 文件复制SCSS 资产

    {
      ...
      "assets": [
        "./src/styles/_material.scss"
      ]
      ...
    }
    

    package.json中导出虚拟访问路径(相对于您的库名称)

    {
      ...
      "exports": {
        "./theming": {
          "sass": "./src/styles/_material.scss"
        }
      }
      ...
    }
    

    从导入/消费应用中导入主题文件

    @import "~@my-lib/theming";

    【讨论】:

      【解决方案2】:

      ng-packagr 我也遇到过类似的问题。我还没有找到正确的方法,但解决方法是您可以简单地将 .scss 文件手动复制到最终版本。 您可以使用 ncp - 异步递归文件和目录复制 将文件复制到最终构建。

      在最终构建命令之后添加 && ncp currentpathOfSCSSFile destinationPathOfFinalBuildDistFolder

      只是一种解决方法,但它会解决这个问题;)

      【讨论】:

      • Guruprasad mishra 感谢您分享此解决方案。我试过这个,npm i ncp 然后在构建后运行 "ncp \"./projects/ui-sdk/src/lib/styles/**/*\" \"./dist/ui-sdk/styles\""库但出现错误 sh: ncp: command not found 我在主项目空间中安装了 ncp,并在库项目中安装后尝试。
      • 检查是否在 package.json 中添加了 ncp,检查依赖项/devDependencies。如果存在,请尝试删除 package-lock.json 并再次运行 npm install。并且还给出源到目标“ncp ../projects/ui-sdk/src/lib/styles/style.scss ./dist/ui-sdk/styles/style.scss”的相对路径。让我知道您尝试解决此问题的方法。谢谢你
      • 让我知道解决方案是否有效或您是否遇到任何其他问题。另外,如果您正在实施其他方法,请告诉我。谢谢
      • Guruprasad mishra 嗨,我找到了解决方案,测试后将在此处发布。
      猜你喜欢
      • 2019-05-27
      • 2021-07-17
      • 2018-06-09
      • 2020-10-14
      • 1970-01-01
      • 2021-09-18
      • 2018-11-13
      • 2020-09-21
      • 2021-08-01
      相关资源
      最近更新 更多