2018 TypeScript Update(5)TypeScript Module/Package

本文介绍如何使用TypeScript创建和发布Node.js库,包括设置项目结构、编译配置及库的发布流程。通过实例演示了从创建项目到最终发布的全过程。
2018 TypeScript Update(5)TypeScript Module/Package

Understand the Difference
https://docs.npmjs.com/getting-started/packages


Check the starter Project
https://github.com/alexjoverm/typescript-library-starter

Try the first library starter
> git clone https://github.com/alexjoverm/typescript-library-starter.git services.export

Go to that directory and execute install, it will prompt the name to you
> npm install

https://blog.tonysneed.com/2017/09/15/introducing-typescript-library-starter-lite/
https://github.com/bersling/typescript-library-starter
https://www.tsmean.com/articles/how-to-write-a-typescript-library/
https://www.tsmean.com/articles/how-to-write-a-typescript-library/local-consumer/

It seems not that easy.

I follow the example, did a sample Library as follow:
services.library.helloworld/package.json
{
"name": "services.library.helloworld",
"version": "1.0.0",
"description": "Can log \"hello world\" and \"goodbye world\" to the console!",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"typescript",
"library",
"hello",
"world"
],
"author": "Carl Luo<luohuazju@gmail.com>",
"license": "MIT"
}

services.library.helloworld/tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"declaration": true,
"outDir": "./dist"
},
"include": [
"src/**/*"
]
}

sevices.library.helloworld/.npmignore
tsconfig.json
src

The sample source is very simple as well, services.library.helloworld/src/hello-world.ts
export namespace HelloWorld {
export function sayHello() {
console.log('hi, sillycat.')
}
export function sayGoodbye() {
console.log('good night, sillycat.')
}
}

services.library.helloworld/src/index.ts
export {HelloWorld} from './hello-world'

Run the compile for this library
>tsc

In the same directly, run the command for the consumer project
> npm link ../services.library.helloworld

This command will link the library to the module directory
> ls -l node_modules/services*
lrwxr-xr-x 1 hluo staff 74 Mar 29 13:41 node_modules/services.library.helloworld -> ../../../../tool/node-v8.10.0/lib/node_modules/services.library.helloworld

In the consumer project, we will do this to use that library
import {HelloWorld} from 'services.library.helloworld';
HelloWorld.sayHello();


References:
https://docs.npmjs.com/getting-started/creating-node-modules
https://github.com/RobinBuschmann/soap-typescript
https://docs.npmjs.com/getting-started/packages

https://codeburst.io/https-chidume-nnamdi-com-npm-module-in-typescript-12b3b22f0724
https://www.npmjs.com/package/create-typescript-package
https://www.tsmean.com/articles/how-to-write-a-typescript-library/
https://medium.com/french-make-beautiful/5-things-to-know-when-writing-a-typescript-npm-module-e9a334efd544
https://www.twilio.com/blog/2017/06/writing-a-node-module-in-typescript.html
https://github.com/alexjoverm/typescript-library-starter
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值