简体   繁体   English

Angular Cli 工作区外的 Angular 库

[英]Angular Library outside the Angular Cli Workspace

Is it possible to have a library outside the workspace of angular cli?是否可以在 angular cli 的工作区之外有一个库?

Traditional scheme:传统方案:

my-workspace/
  ...             (workspace-wide config files)
  projects/       (generated applications and libraries)
    my-first-app/ --(an explicitly generated application)
      ...         --(application-specific config)
      e2e/        ----(corresponding e2e tests)
         src/     ----(e2e tests source)
         ...      ----(e2e-specific config)
      src/        --(source and support files for application)
    my-lib/       --(a generated library)
      ...         --(library-specific config)
      src/        --source and support files for library)

Now, I have a library: my-lib2 that reside in another directory, not inside the projects directory .现在,我有一个库: my-lib2 ,它位于另一个目录中,而不是在项目目录中。 I put the configuration of my-lib2 in angular.json file of my-workspace, with relative path, but, when I compile this: ng build my-lib2 , inside the workspace , I have this errors:我将 my-lib2 的配置放在 my-workspace 的 angular.json 文件中,带有相对路径,但是,当我编译这个时: ng build my-lib2 ,在工作空间内,我有这个错误:

Building Angular Package

** It is not recommended to publish Ivy libraries to NPM repositories **

------------------------------------------------------------------------------
Building entry point 'my-lib2'
------------------------------------------------------------------------------
Compiling TypeScript sources through ngc
ERROR: ../my-lib2/src/lib/my-lib2.component.ts(1,35): error TS2307: Cannot find module '@angular/core'.
../my-lib2/src/lib/my-lib2.module.ts(1,26): error TS2307: Cannot find module '@angular/core'.
../my-lib2/src/lib/my-lib2.service.ts(1,28): error TS2307: Cannot find module '@angular/core'.
../my-lib2/src/lib/my-lib2.service.ts(3,1): error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.

An unhandled exception occurred: ../my-lib2/src/lib/my-lib2.component.ts(1,35): error TS2307: Cannot find module '@angular/core'.
../my-lib2/src/lib/my-lib2.module.ts(1,26): error TS2307: Cannot find module '@angular/core'.
../my-lib2/src/lib/my-lib2.service.ts(1,28): error TS2307: Cannot find module '@angular/core'.
../my-lib2/src/lib/my-lib2.service.ts(3,1): error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.

See "/tmp/ng-bt10hE/angular-errors.log" for further details.

Schema架构

my-lib2/       
      ...         
      src/


my-workspace/
  ...             
  projects/       
    my-first-app/ 
      ...         
      e2e/        
         src/     
         ...      
      src/        
    my-lib/       
      ...         
      src/ 

Configuration of angular.json inside of workspace: angular.json在工作区内部的配置:

...
   ...
   "my-lib2": {
      "projectType": "library",
      "root": "projects/../../my-lib2/",
      "sourceRoot": "projects/../../my-lib2/src",
      "prefix": "lib",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-ng-packagr:build",
          "options": {
            "tsConfig": "projects/../../my-lib2/tsconfig.lib.json",
            "project": "projects/../../my-lib2/ng-package.json"
          },
          "configurations": {
            "production": {
              "tsConfig": "projects/../../my-lib2/tsconfig.lib.prod.json"
            }
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "projects/../../my-lib2/src/test.ts",
            "tsConfig": "projects/../../my-lib2/tsconfig.spec.json",
            "karmaConfig": "projects/../../my-lib2/karma.conf.js"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "projects/../../my-lib2/tsconfig.lib.json",
              "projects/../../my-lib2/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }},
    ...
    ...

tsconfig.lib.ts (configuration file of my-lib2) tsconfig.lib.ts(my-lib2的配置文件)

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

As can be seen, all paths are relative...可以看出,所有路径都是相对的......

Any idea how I can configure the entries in angular.json of the workspace to take the library out of the projects path?知道如何配置工作区的angular.json中的条目以将库从项目路径中取出吗?

The solution was the correct configuration of the tsconfig.lib.ts file of the library.解决方案是正确配置库的 tsconfig.lib.ts 文件。

Schema of directory:目录架构:

my-lib2/       
      ...         
      src/


my-workspace/
  ...             
  projects/       
    my-first-app/ 
      ...         
      e2e/        
         src/     
         ...      
      src/        
    my-lib/       
      ...         
      src/ 

my-lib2 entry in the angular.json file in the workspace:工作区中 angular.json 文件中的 my-lib2 条目:

"my-lib2": {
      "projectType": "library",
      "root": "../my-lib2",
      "sourceRoot": "../my-lib2/src",
      "prefix": "lib",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-ng-packagr:build",
          "options": {
            "tsConfig": "../my-lib2/tsconfig.lib.json",
            "project": "../my-lib2/ng-package.json"
          },
          "configurations": {
            "production": {
              "tsConfig": "../my-lib2/tsconfig.lib.prod.json"
            }
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "../my-lib2/src/test.ts",
            "tsConfig": "../my-lib2/tsconfig.spec.json",
            "karmaConfig": "../my-lib2/karma.conf.js"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "../my-lib2/tsconfig.lib.json",
              "../my-lib2/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }},

tsconfig.lib.ts file configuration in my-lib2 directory: my-lib2目录下的tsconfig.lib.ts文件配置:

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

This is the important part in the configuration:这是配置中的重要部分:

 ...
 "baseUrl": "../my-workspace/",
 "paths": {
       "*": ["../my-workspace/node_modules/*"]
 }
 ...

Run compilation:运行编译:

Building Angular Package

** It is not recommended to publish Ivy libraries to NPM repositories **

------------------------------------------------------------------------------
Building entry point 'my-lib2'
------------------------------------------------------------------------------
Compiling TypeScript sources through ngc
Bundling to FESM2015
Bundling to FESM5
Bundling to UMD
Minifying UMD bundle
Copying declaration files
Writing package metadata
Built my-lib2

------------------------------------------------------------------------------
Built Angular Package!
 - from: /home/usuario/my-project/my-lib2
 - to:   /home/usuario/my-project/my-workspace/dist/my-lib2
------------------------------------------------------------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM