es4x 引用外部jar 包以及集成typescrip

以前写过一个通过修改jar 包处理自定义jar 的引入的,如下是一种使用官方推荐的方法package.json 添加依赖配置
同时为了方便使用添加typescript define 文件方便使用(只是demo,实际上如果看了官方发布的包为了这种方法)

项目准备

  • 项目结构
 
├── README.md
├── dist
├── id.js
└── index.js
├── id.d.ts
├── id.js
├── index.ts
├── package-lock.json
├── package.json
├── tsconfig.json
└── yarn.lock
  • 代码说明
    package.json:
 
{
  "version": "1.0.0",
  "description": "This is a ES4X empty project.",
  "main": "dist/index.js",
  "scripts": {
    "test": "es4x test index.test.js",
    "postinstall": "es4x install",
    "start": "es4x run dist/index.js",
    "build": "tsc -w"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "name": "newapp",
  "devDependencies": {
    "@vertx/unit": "^3.8.4",
    "typescript": "^3.7.2"
  },
// jar 依赖声明
  "maven": {
    "groupId": "com.cedarsoft.commons",
    "artifactId": "id",
    "version": "8.9.2"
  },
  "dependencies": {
    "@vertx/core": "^3.8.4"
  }
}

tsconfig.json:
typescritp 配置,主要是关于编译以及特性配置的,具体参考https://github.com/rongfengliang/es4x-jar-deps-learning
index.ts:
入口

/// <reference path="node_modules/@types/es4x.d.ts" />
// @ts-check
import {id} from "./id"
vertx
  .createHttpServer()
  .requestHandler(function (req) {
    req.response().end(id.createNameWithSpaces("DalongRongAppDemo"));
  })
  .listen(8080);
console.log('Server listening at: http://localhost:8080/');
 

id.js:
包装依赖的jar 包方便基于模块的开发

 
const id = Java.type("com.cedarsoft.id.NameSpaceSupport")
export {
   id
}
export default id
 

id.d.ts:
typescript 类型定义文件,文件定义参考jar class 定义

declare class id {
    static createNameWithSpaces(name:string):string
}
export {
    id
}

运行

  • 安装依赖以及生成运行入口的jar 文件
yarn
  • ts 编译
yarn build
  • 启动
yarn start 
  • 效果
yarn start 
yarn run v1.19.1
$ es4x run dist/index.js
Server listening at: http://localhost:8080/
Succeeded in deploying verticle
  • 访问

说明

以上是一个简单的项目引用三方jar 的方式,同时为了方便也集成了typescript 的包装处理,目前有一个不太好的地方对于包的安装使用的
是repo1.maven.org/maven2 地址进行下载的,对于私服方法的支持不太好(目前看着是硬编码),此方法同样可以应用到独立npm 包的
开发

参考资料

https://github.com/rongfengliang/es4x-jar-deps-learning
https://github.com/reactiverse/es4x
https://reactiverse.io/es4x/
https://www.cnblogs.com/rongfengliang/p/11906794.html

猜你喜欢

转载自www.cnblogs.com/rongfengliang/p/11924533.html