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
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
本文介绍如何使用TypeScript创建和发布Node.js库,包括设置项目结构、编译配置及库的发布流程。通过实例演示了从创建项目到最终发布的全过程。
728

被折叠的 条评论
为什么被折叠?



