复制资源作为 Angular 9 库构建的一部分 [英] Copy assets as part of Angular 9 Library build

查看:20
本文介绍了复制资源作为 Angular 9 库构建的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读:https://angular.io/guide/creating-libraries 并指出:

I am reading this: https://angular.io/guide/creating-libraries and it states:

从 ng-packagr 工具的 9.x 版开始,您可以将该工具配置为在构建过程中自动将资产复制到您的库包中.当您的库需要发布可选的主题文件、Sass 混合或文档(如变更日志)时,您可以使用此功能.

Starting with version 9.x of the ng-packagr tool, you can configure the tool to automatically copy assets into your library package as part of the build process. You can use this feature when your library needs to publish optional theming files, Sass mixins, or documentation (like a changelog).

链接将带您到这里:https://github.com/ng-packagr/ng-packagr/blob/master/docs/copy-assets.md

对我来说,这太模棱两可了.它没有告诉您将该选项放在哪里,但从描述来看,它似乎是 options 的一部分:

To me, that is so ambiguous. It doesn't tell you where to put that option, but from the description it sounds like it's part of options:

"options": {
  "tsConfig": "projects/situ-angular-components/tsconfig.lib.json",
  "project": "projects/situ-angular-components/ng-package.json",

  "assets": [
    "CHANGELOG.md",
    "./styles/**/*.theme.scss"
  ]
},

如果我这样做,我会收到此错误:

If I do that, I get this error:

架构验证失败,出现以下错误:数据路径"不应有额外的属性(资产).

Schema validation failed with the following errors: Data path "" should NOT have additional properties(assets).

我的完整 angular.json 如下所示:

My full angular.json looks like this:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "situ-angular-components": {
      "projectType": "library",
      "root": "projects/situ-angular-components",
      "sourceRoot": "projects/situ-angular-components/src",
      "prefix": "situ",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-ng-packagr:build",
          "options": {
            "tsConfig": "projects/situ-angular-components/tsconfig.lib.json",
            "project": "projects/situ-angular-components/ng-package.json",

            "assets": [
              "CHANGELOG.md",
              "./styles/**/*.theme.scss"
            ]
          },
          "configurations": {
            "production": {
              "tsConfig": "projects/situ-angular-components/tsconfig.lib.prod.json"
            }
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "projects/situ-angular-components/src/test.ts",
            "tsConfig": "projects/situ-angular-components/tsconfig.spec.json",
            "karmaConfig": "projects/situ-angular-components/karma.conf.js"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "projects/situ-angular-components/tsconfig.lib.json",
              "projects/situ-angular-components/tsconfig.spec.json"
            ],
            "exclude": ["**/node_modules/**"]
          }
        }
      }
    }
  },
  "cli": {
    "analytics": "8db81fee-b0bd-41b0-a453-27aa6be6cc88"
  },
  "defaultProject": "situ-angular-components"
}

如果我将资产移动到下面的级别,我不会出错,但不会复制任何资产:

If I move the assets to the level below, I get no errors, but no assets get copied:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "situ-angular-components": {
      "projectType": "library",
      "root": "projects/situ-angular-components",
      "sourceRoot": "projects/situ-angular-components/src",
      "prefix": "situ",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-ng-packagr:build",
          "options": {
            "tsConfig": "projects/situ-angular-components/tsconfig.lib.json",
            "project": "projects/situ-angular-components/ng-package.json"
          },

          "assets": [
            "CHANGELOG.md",
            "./styles/**/*.theme.scss"
          ],
          "configurations": {
            "production": {
              "tsConfig": "projects/situ-angular-components/tsconfig.lib.prod.json"
            }
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "projects/situ-angular-components/src/test.ts",
            "tsConfig": "projects/situ-angular-components/tsconfig.spec.json",
            "karmaConfig": "projects/situ-angular-components/karma.conf.js"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "projects/situ-angular-components/tsconfig.lib.json",
              "projects/situ-angular-components/tsconfig.spec.json"
            ],
            "exclude": ["**/node_modules/**"]
          }
        }
      }
    }
  },
  "cli": {
    "analytics": "8db81fee-b0bd-41b0-a453-27aa6be6cc88"
  },
  "defaultProject": "situ-angular-components"
}

有人知道我做错了什么吗?

Does anyone know what I am doing wrong?

推荐答案

我想通了.您只需要编辑您的 ng-package.json 文件并添加您的资产:

I figured this out. You just need to edit your ng-package.json file and add your assets:

"assets": [
  ".npmrc"
],

这将复制您的文件.

这篇关于复制资源作为 Angular 9 库构建的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆