Angular创建自己的库发布到npm上使用

@Angular创建自己的库发布到npm上使用

1.创建Angular Library

1.1创建项目工程

	ng new test-commonlib-ui --no-create-application
	cd test-commonlib-ui
	ng g application test-commonlib-app-ui

1.2配置angular.json

  1. 在projects文件下新建lx-custom-ui文件夹
  2. 修改newProjectRoot
"newProjectRoot": "projects/lx-custom-ui",
  1. 配置以@开头的库名字
"@lx-custom-ui/test-commonlib-ui": {
      "projectType": "library",
      "root": "src",
      "prefix": "lx",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:ng-packagr",
          "options": {
            "project": "projects/lx-custom-ui/ng-package.json"
          },
          "configurations": {
            "production": {
              "tsConfig": "projects/lx-custom-ui/tsconfig.lib.json"
            },
            "development": {
              "tsConfig": "projects/lx-custom-ui/tsconfig.lib.json"
            }
          },
          "defaultConfiguration": "production"
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "projects/lx-custom-ui/src/test.ts",
            "tsConfig": "projects/lx-custom-ui/tsconfig.spec.json",
            "karmaConfig": "projects/lx-custom-ui/karma.conf.js"
          }
        }
      }
    }
  1. 执行后的文件结构
    在这里插入图片描述
    在这里插入图片描述

1.3配置Library工程

  1. 添加4个文件ng-package.json, package.json,public-api.ts,tsconfig.lib.json
  2. 文件结构
    在这里插入图片描述
  3. 具体代码
    a). ng-package.json
{
  "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
  "dest": "../../dist/@lx-custom-ui/test-commonlib-ui",
  "deleteDestPath": true,
  "lib": {
    "entryFile": "public-api.ts"
  }
}

b). package.json

{
    "name": "@lx-custom-ui/test-commonlib-ui",
    "version": "1.0.0",
    "peerDependencies": {
        "@angular/core": "^12.0.0 || ^13.0.0 || ^14.0.0 || ^15.0.0",
        "@angular/common": "^12.0.0 || ^13.0.0 || ^14.0.0 || ^15.0.0",
        "@angular/forms": "^12.0.0 || ^13.0.0 || ^14.0.0 || ^15.0.0",
        "rxjs": "^6.0.0",
        "zone.js": "^0.10.2"
    }
}

c). public-api.ts

export * from '@lx-custom-ui/test-commonlib-ui/my-first-table';

d). tsconfig.lib.json

{
  "compilerOptions": {
    "declaration": true,
    "target": "es2015",
    "module": "es2015",
    "baseUrl": ".",
    "stripInternal": true,
    "emitDecoratorMetadata": false,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "outDir": "../build",
    "rootDir": ".",
    "lib": [
      "es2015",
      "dom",
      "es2017"
    ],
    "skipLibCheck": true,
    "types": [],
    "paths": {
      "@lx-custom-ui/test-commonlib-ui/*": [
        "projects/lx-custom-ui/*/src/public-api"
      ]
    }
  },
  "angularCompilerOptions": {
    "annotateForClosureCompiler": true,
    "skipTemplateCodegen": true,
    "strictMetadataEmit": true,
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
    "flatModuleId": "AUTOGENERATED",
    "flatModuleOutFile": "AUTOGENERATED"
  },
  "files": [
    "./public-api.ts"
  ]
}

1.4修改tsconfig.json配置

 "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "ES2022",
    "module": "ES2022",
    "useDefineForClassFields": false,
    "lib": [
      "ES2022",
      "dom"
    ],
    "paths": {
      "@lx-custom-ui/test-commonlib-ui/*": [
        "projects/lx-custom-ui/*/src/public-api"
      ]
    }
  },

注意,一定要修改paths,否则模块引入会报错的。
在这里插入图片描述

1.5 执行ng g library命令

  1. 创建自己的库
ng g library my-first-table
  1. 配置lib下的package.json, 注意这里的版本要和整个引用的项目版本一致,不然会有依赖报错。
{
  "name": "my-first-table",
  "version": "0.0.1",
  "peerDependencies": {
    "@angular/forms":"~12.2.0 || ^14.0.0 || ^15.2.0 || ~12.2.18",
    "@angular/common":"~12.2.0 || ^14.0.0 || ^15.2.0 || ~12.2.18",
    "@angular/core":"~12.2.0 || ^14.0.0 || ^15.2.0 || ~12.2.18",
    "ng-zorro-antd":"^12.1.0 || ^14.0.0 || ^15.1.0 || ~12.2.18",
    "rxjs": "~6.6.0 || ~7.5.0 || ~7.8.0",
    "zone.js": "~0.11.4 || ~0.11.4 || ~0.12.0"
  },
  "dependencies": {
    "tslib": "^2.3.0",
    "@angular/forms":"~12.2.0 || ^14.0.0 || ^15.2.0 || ~12.2.18",
    "@angular/common":"~12.2.0 || ^14.0.0 || ^15.2.0 || ~12.2.18",
    "@angular/core":"~12.2.0 || ^14.0.0 || ^15.2.0 || ~12.2.18",
    "ng-zorro-antd":"^12.1.0 || ^14.0.0 || ^15.1.0 || ~12.2.18",
    "rxjs": "~6.6.0 || ~7.5.0 || ~7.8.0",
    "zone.js": "~0.11.4 || ~0.11.4 || ~0.12.0"
  },
  "sideEffects": false
}
  1. 自动生成文件结构
    在这里插入图片描述

  2. 具体代码(有第三方组件一定要应用module)
    my-first-table.module.ts

import { NgModule } from '@angular/core';
import { MyFirstTableComponent } from './my-first-table.component';
import { NzTableModule } from 'ng-zorro-antd/table';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';

@NgModule({
  declarations: [
    MyFirstTableComponent
  ],
  imports: [
    BrowserModule,
    NzTableModule,
    FormsModule,
    CommonModule,
  ],
  exports: [
    MyFirstTableComponent
  ]
})
export class MyFirstTableModule { }

在这里插入图片描述

2.与Library同一个项目文件下测试

2.1angular.json结构

在这里插入图片描述

2.2测试项目使用

  1. 引用模块
    在这里插入图片描述

  2. 可以修改html的前缀
    在这里插入图片描述
    在这里插入图片描述

  3. 使用自己定义的html
    在这里插入图片描述

  4. 运行命令,结果如下
    在这里插入图片描述
    在这里插入图片描述

3. 发布Angular Library

1.1build包的命令

ng build @lx-custom-ui/test-commonlib-ui

1.2发布dist文件夹

  1. 登录npm(先到官网上注册一个自己的npm包, 注意发布@开头的,要与npm用户名一致,你也可以创建组织)
npm login
  1. 发布dist/@lx-custom-ui/test-commonlib-ui
npm publish --access public
  1. 每次发布版本要更新
    在这里插入图片描述
  2. 发布成功
    在这里插入图片描述

1.3npm官网上有自己发布的包

在这里插入图片描述
在这里插入图片描述

4.使用自己的Angular Library

1.1tgz方式

  1. 打包成tgz,手动复制到其他项目中
"npm-pack": "cd dist/@lx-custom-ui/test-commonlib-ui && npm pack",
"npm-lib-install": "npm install ./dist/@lx-custom-ui/test-commonlib-ui/lx-custom-ui-test-commonlib-ui-3.0.3.tgz"
  1. 配置好后, 在根文件下,执行
npm run build-lib
npm run npm-pack
npm run npm-lib-install
  1. 生成tgz在dist下
    在这里插入图片描述
    4.复制tgz到任意项目,任意目录下,只要路径正确,都能本地安装(本地安装命令"npm-lib-install": “npm install ./dist/@lx-custom-ui/test-commonlib-ui/lx-custom-ui-test-commonlib-ui-1.0.0.tgz”), 执行
 "dependencies": {
    "@lx-custom-ui/test-commonlib-ui": "file:dist/@lx-custom-ui/test-commonlib-ui/lx-custom-ui-test-commonlib-ui-3.0.3.tgz",
  },
  1. 结果如下:在测试项目下安装本地的tgz文件在这里插入图片描述

1.1npm方式

  1. 任意项目下,前提angular版本要对应,执行命令
npm i @lx-custom-ui/test-commonlib-ui

在这里插入图片描述

  1. 引入模块
    在这里插入图片描述

  2. 使用自己的html
    在这里插入图片描述

5.github代码仓库

  1. library github code
  2. angular project use 自己的library

6.学习地址

angular create library

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值