Skip to main content

Command Palette

Search for a command to run...

Create a library - Publish a npm package in Typescript

Published
L

Full stack web 3D developer

To make some reusable codes, create and publish a npm package is a good idea.

Below I'll share base on my repo on github chinese-poker.

Init npm package

  • create a directory mkdir your-package-name cd your-package-name
  • make a initialization (-y means no prompt question and all question to be the defualt value)

npm init -y

Get a file tree like:

.
└── package.json

Write codes down:

below is my code files tree:

src
├── Config.ts
├── Const.ts
├── MetaProcessor.ts
├── Ruler.ts
├── SpecificGetFn.ts
├── __tests__
│   └── ruler_spec.ts
└── utils
    └── utils.ts

Intergrate code entry:

Make a file index.ts which we will assign it's path to the bundle config. Input the stuffs you want to export:

//index.ts
export {Ruler} from "./Ruler";
export {E_TYPE} from "./Config";

Bundle the codes:

It's nessary to make a bundle, for easy consuming. The bundle tool can resolve the module format like Commonjs and ESModule for different usage and help us transpile typescript file and generate d.ts file by some plugins.

Rollup, Webpack, and Parcel are all module bundler tools, but the focus is different. The Rollup we are going to talk about is more suitable for building Library, while Webpack and Precel are more suitable for developing Application

Here We choose Rollup because it's clean and easy to use.

Install rollup

npm i -D rollup

Create file rollup.config.js

import typescript from 'rollup-plugin-typescript2'; 
import babel from 'rollup-plugin-babel'; 
export default {
    input: 'src/index.ts',
    output: [
        {
            file: 'dist/index.esm.js',
            format: 'esm', 
            sourcemap: true
        }
    ],
    plugins: [typescript(),babel()]
}
  • rollup-plugin-babel for compile typescript.
  • rollup-plugin-typescript2 for generate d.ts file.
  • set property input as your entry file.
  • set property output to specify the output path and format.

Set types and module in package.json

//package.json
{
...
"types": "index.d.ts",
"module": "index.esm.js",
...
}

Run rollup

rollup -c

Then you can get the file tree like below:

dist
├── Config.d.ts
├── Const.d.ts
├── MetaProcessor.d.ts
├── README.md
├── Ruler.d.ts
├── SpecificGetFn.d.ts
├── __tests__
│   └── ruler_spec.d.ts
├── index.d.ts
├── index.esm.js
├── index.esm.js.map
└── utils
    └── utils.d.ts

Publish

  • create a README.md file which is welcome for a easy describtion.
  • copy README.md and package.json to dist.
  • npm login(If you have't a npm account, get one npmjs)
  • npm publish ./dist

Consuming the package:

npm install your-package-name

Use it just like other package you used to.

Thanks for your reading.

Have a good day.

More from this blog

如何将静态页面部署到Github Page,并绑定自定义域名

在仓库主页选择setting 点击pages 选择分支branch 输入自定义域名custom domain(设置好后点击save,系统将自动在项目根目录生成CNAME文件,文件内容为设置的当前域名) 在域名提供商处,添加DNS设置(以阿里云为例) 配置CNAME 配置IP,创建一个A类记录指向185.199.108.153 github将根据访问域名路由项目仓库(仓库名称为iddz.fun,并且根目录有CNAME文件) 最后,通过访问自定义域名,完成静态网站的部署...

Oct 4, 2023
如何将静态页面部署到Github Page,并绑定自定义域名

熟悉熟悉官方文档,逐步深入Babylon.js

文档的组织结构 通过前一篇文章的了解,我们会对Babylon.js有了一个大致的了解,整个文档主要是想带你逐步深入掌握这个Babylon.js所提供的所有内容。 内容主要分为一个概览和9个主要部分,这些部分包含章节,还有API详解和强大的文档和playground搜索功能。 - 1.Babylon.js特性 - Babylon.js是一个功能完备的游戏和渲染引擎,具有广泛的特性。这个部分将带你了解这些特性,并帮助你编码和使用它们。- 2.将Babylon.js添加到你的Web项目中 - 有多种...

Sep 25, 2023
熟悉熟悉官方文档,逐步深入Babylon.js

Lizhiyu's Blog

33 posts

Bringing the world closer together through play