LoginSignup
0
1

More than 3 years have passed since last update.

Node.jsで実行できるスクリプトをTypeScriptで書くテンプレート

Last updated at Posted at 2018-11-28

概要

Node.jsで実行できるスクリプトをTypeScriptで書ける環境を作り,テンプレートにしました.

場所

使い方

git clone https://github.com/kabosu3d-lab/typescript-cli-template.git tool
cd tool
npm install # インストール
npm run build # ビルド
node dist/main.js # 実行

以下,詳細な環境構築手順

検証環境

Macbook pro

環境構築

mkdir tool
cd tool/
git init
curl https://raw.githubusercontent.com/github/gitignore/master/Node.gitignore > .gitignore
npm init # 質問にはお好みに回答
npm install --save-dev webpack webpack-cli typescript ts-loader @types/node
./node_modules/typescript/bin/tsc --init

webpack.config.jsを作成

module.exports = {
  entry: './src/index.ts',
  target: 'node',
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: ['ts-loader'],
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: [ '.tsx', '.ts', '.js' ]
  }
};

package.jsonのscriptsを下記のように変更する.

{
    "scripts": {
        "build": "webpack",
    },
}
mkdir src
touch src/index.ts # ここへコードを書く

ビルドと実行

npm run build # ビルド
node dist/main.js # 実行

参考

TypeScriptでCLIツールを作って、npmパッケージにする

0
1
0

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
0
1