はこです。こんにちわ。
何か小さなnpmパッケージを作って公開したい時、TypeScriptを使えば型安全が手に入るし、Decorator等のイケてる機能も手に入るます。
typescriptのパッケージ作成
# npm 初期化
npm init パッケージ名
# git初期化
git init パッケージ名
# TypeScript導入
npm install -S typescript
# tsconfig.json生成
npx tsc --init
あとはtsconfig.jsonを好み合わせて変更すればOKです。
なお、ビルドした結果をgitに含めたくないので、postinstallでインストール後にユーザ環境でビルドを行うようにしています。
package.json
{
"name": "small-npm-pkg",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"postinstall": "postinstall-build dist"
},
"devDependencies": {
"typescript": "^2.6.1"
},
"dependencies": {
"postinstall-build": "^5.0.1"
}
}
通常、postinstallではdevDependenciesを取得してくれません。
コレを回避する方法として、 postinstall-build のnpmパッケージを利用しています。
postinstall-build
一言で言えば「devDependenciesのファイルもインストールしてくれるやつ」です。
"postinstall-build 出力先ディレクトリ名(必須) 実行コマンド(省略時はnpm buildを実行)"
コレのお陰で、devDependenciesを分離できます。