LoginSignup
14
13

More than 5 years have passed since last update.

TypeScriptでnpmパッケージを記述する(最小構成)

Last updated at Posted at 2017-11-01

はこです。こんにちわ。

何か小さな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を分離できます。

14
13
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13