0

我创建了一个具有以下配置的自定义角度库。打包并发布到 npm。

库名.module.ts

  @NgModule({
  declarations: [LibraryComponent],
  imports: [
    CommonModule,
    MaterialModuleSet,
  ],
  exports: [LibraryComponent],
  providers: [LibraryComponent],
  schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]
})
export class LibraryModule { }

公共api.ts

export * from './lib/library.service';
export * from './lib/library.component';
export * from './lib/library.module';

包.json

{
  "name": "library",
  "version": "0.0.1",
  "peerDependencies": {
    "@angular/common": "^8.2.13",
    "@angular/core": "^8.2.13",
    "@angular/platform-browser": "^8.2.14",
    "@angular/platform-browser-dynamic": "~8.2.0",
    "@angular/compiler": "^8.2.14",
    "rxjs": "^6.5.4",
    "zone.js": "^0.9.1",
    "@angular/material": "^8.2.0"
  }
}

tsconfig.lib.json

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/lib",
    "target": "es2015",
    "declaration": true,
    "inlineSources": true,
    "types": [],
    "lib": [
      "dom",
      "es2018"
    ]
  },
  "angularCompilerOptions": {
    "annotateForClosureCompiler": true,
    "skipTemplateCodegen": true,
    "strictMetadataEmit": true,
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
    "enableResourceInlining": true
  },
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

我在我的主机应用程序中使用该库作为 host.component.html

<lib-library></lib-library>

主机的tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "library": [
        "node_modules/@package/library-folder-name/dist/library/",
      ],
      "library/*": [
        "node_modules/@package/library-folder-name/dist/library/*"
      ]
    },
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es5",
    "lib": [
      "es2018",
      "dom"
    ],
    "skipLibCheck": true,
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
    "disableTypeScriptVersionCheck": true
  }
}

当我运行/构建主机应用程序而不在 tsconfig.json 中提供库路径时,它给了我以下错误

Module not found: Error: Can't resolve 'library' in 'node_modules/@package/library-folder-name/dist/library/'
ERROR in ./src/app/app.module.ngfactory.js

我必须在我使用的每个主机应用程序中明确提供路径。我怎样才能在不明确提供相同内容的情况下完成这项工作?

4

0 回答 0