Copy assets as part of Angular 9 Library build

萝らか妹 提交于 2021-01-29 09:26:19

问题


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

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).

And the link takes you here: https://github.com/ng-packagr/ng-packagr/blob/master/docs/copy-assets.md

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).

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?


回答1:


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

"assets": [
  ".npmrc"
],

That will copy your files.



来源:https://stackoverflow.com/questions/63357621/copy-assets-as-part-of-angular-9-library-build

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!