Communitygithub.com

onmax/ts-library / TypeScriptライブラリ開発スキル

TypeScriptライブラリやnpmパッケージを作成する際に使用します。プロジェクト設定、package.jsonのエクスポート設定、ビルドツール(tsdown/unbuild)、API設計パターン、型推論のテクニック、テスト、npm公開までを網羅します。バンドル、CJS/ESMのデュアル出力の設定、リリースワークフローの構築時に使用してください。

ts-library / TypeScriptライブラリ開発スキル とは?

このスキルは、TypeScriptライブラリやnpmパッケージの開発に特化した包括的なガイドです。プロジェクトの初期設定から、package.jsonのエクスポート設定、tsdownやunbuildなどのビルドツールの設定、API設計パターン、型推論のテクニック、テスト戦略、そしてnpmへの公開とリリースワークフローまでをカバーします。CJSとESMのデュアル出力の設定を簡単に行えるため、モダンなパッケージ開発に最適です。また、Claude CodeやCursorなどのAIエージェンツールと連携して、これらのタスクを効率的に自動化・支援できるように設計されています。主にNode.jsエコシステムで動作し、CLIを通じてプロジェクトのセットアップやコード生成を実行します。

対応~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/onmax/nuxt-skills/tree/main/skills/ts-library

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

ts-library / TypeScriptライブラリ開発スキル は何をしますか?

TypeScriptライブラリやnpmパッケージを作成する際に使用します。プロジェクト設定、package.jsonのエクスポート設定、ビルドツール(tsdown/unbuild)、API設計パターン、型推論のテクニック、テスト、npm公開までを網羅します。バンドル、CJS/ESMのデュアル出力の設定、リリースワークフローの構築時に使用してください。

はじめに

このスキルは、TypeScriptライブラリの開発を効率化するためのものです。以下の主要なトピックをカバーします:

  • プロジェクトの初期化と設定
  • package.json のエクスポート設定
  • ビルドツール(tsdown/unbuild)の設定
  • API設計パターン
  • 型推論のテクニック
  • テスト戦略
  • npmへの公開
  • リリースワークフロー

プロジェクト設定

1. プロジェクトの初期化

npm init -y

2. TypeScriptの設定

tsconfig.json の例:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "strict": true,
    "esModuleInterop": true,
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "skipLibCheck": true
  },
  "include": ["src"],
  "exclude": ["node_modules", "dist"]
}

package.jsonのエクスポート設定

CJS/ESMのデュアル出力をサポートするための package.json の例:

{
  "type": "module",
  "main": "./dist/index.cjs",
  "module": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "exports": {
    ".": {
      "import": "./dist/index.js",
      "require": "./dist/index.cjs",
      "types": "./dist/index.d.ts"
    }
  },
  "files": ["dist"]
}

ビルドツール(tsdown/unbuild)

tsdown を使用する場合

tsdown.config.ts の例:

import { defineConfig } from "tsdown";

export default defineConfig({
  entry: ["src/index.ts"],
  format: ["esm", "cjs"],
  dts: true,
  clean: true,
});

unbuild を使用する場合

build.config.ts の例:

import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
  entries: ["./src/index"],
  declaration: true,
  clean: true,
  rollup: {
    emitCJS: true,
  },
});

API設計パターン

関数ベースのAPI

export function createUser(name: string, age: number): User {
  return { name, age };
}

オプションオブジェクトパターン

export interface Config {
  baseURL: string;
  timeout?: number;
  headers?: Record<string, string>;
}

export function createClient(options: Config): Client {
  // 実装
}

型推論のテクニック

ジェネリック制約

export function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key];
}

条件付き型

export type IsString<T> = T extends string ? true : false;

テスト

テストフレームワーク(vitest)の設定

npm install -D vitest

vitest.config.ts の例:

import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    globals: true,
    environment: "node",
  },
});

テストの例:

import { describe, it, expect } from "vitest";
import { add } from "./math";

describe("add", () => {
  it("should add two numbers", () => {
    expect(add(1, 2)).toBe(3);
  });
});

npmへの公開

公開前の確認

npm run build
npm test

公開コマンド

npm publish

リリースワークフロー

semantic-release を使用する場合

package.json への追加設定:

{
  "release": {
    "branches": ["main"]
  },
  "publishConfig": {
    "access": "public"
  }
}

CI設定(GitHub Actions)の例:

name: Release
on:
  push:
    branches: [main]
jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm ci
      - run: npm run build
      - run: npx semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

まとめ

このスキルを使うことで、TypeScriptライブラリの開発から公開までの全プロセスを効率的に進めることができます。プロジェクトの設定、ビルド、テスト、リリースワークフローを一貫して管理しましょう。

Individual skills in this repo

This repo contains 15 individual skills — each has its own dedicated page.

onmax/document-writer

Use when writing blog posts or documentation markdown files - provides writing style guide (active voice, present tense), content structure patterns, and MDC component usage. Overrides brevity rules for proper grammar. Use nuxt-content for MDC syntax, nuxt-ui for component props.

onmax/motion

Use when adding animations with Motion Vue (motion-v) - provides motion component API, gesture animations, scroll-linked effects, layout transitions, and composables for Vue 3/Nuxt

onmax/nuxt

Use when working on Nuxt 4+ projects - provides server routes, file-based routing, middleware patterns, Nuxt-specific composables, and configuration with latest docs. Covers h3 v1 helpers (validation, WebSocket, SSE) and nitropack v2 patterns. Updated for Nuxt 4.3+.

onmax/nuxt-better-auth

Use when implementing auth in Nuxt apps with @onmax/nuxt-better-auth - provides useUserSession composable, server auth helpers, route protection, and Better Auth plugins integration.

onmax/nuxt-content

Use when working with Nuxt Content v3, markdown content, or CMS features in Nuxt - provides collections (local/remote/API sources), queryCollection API, MDC rendering, database configuration, NuxtStudio integration, hooks, i18n patterns, and LLMs integration

onmax/nuxthub

Use when building NuxtHub v0.10.6 applications - provides database (Drizzle ORM with sqlite/postgresql/mysql), KV storage, blob storage, and cache APIs. Covers configuration, schema definition, migrations, multi-cloud deployment (Cloudflare, Vercel), and the new hub:db, hub:kv, hub:blob virtual module imports.

onmax/nuxt-modules

Use when creating Nuxt modules: (1) Published npm modules (@nuxtjs/, nuxt-), (2) Local project modules (modules/ directory), (3) Runtime extensions (components, composables, plugins), (4) Server extensions (API routes, middleware), (5) Releasing/publishing modules to npm, (6) Setting up CI/CD workflows for modules. Provides defineNuxtModule patterns, Kit utilities, hooks, E2E testing, and release automation.

onmax/nuxt-seo

Nuxt SEO meta-module with robots, sitemap, og-image, schema-org. Use when configuring SEO, generating sitemaps, creating OG images, or adding structured data.

onmax/nuxt-ui

Use when building styled UI with @nuxt/ui v4 components - create forms with validation, implement data tables with sorting, build modal dialogs and overlays, configure Tailwind Variants theming. Use vue skill for raw component patterns, reka-ui for headless primitives.

onmax/pnpm

Use when managing Node.js dependencies with pnpm - install packages, configure monorepo workspaces, set up pnpm catalogs, resolve dependency conflicts with overrides, patch third-party packages, and configure CI pipelines for pnpm projects

onmax/reka-ui

Use when building with Reka UI (headless Vue components) - provides component API, accessibility patterns, composition (asChild), controlled/uncontrolled state, virtualization, and styling integration. Formerly Radix Vue.

onmax/vite

Vite build tool configuration, plugin API, SSR, and Vite 8 Rolldown migration. Use when working with Vite projects, vite.config.ts, Vite plugins, or building libraries/SSR apps with Vite.

onmax/vitest

Use when writing unit/integration tests for Vite projects - configure vitest.config.ts, write test suites with describe/it, create mock implementations with vi.fn and vi.mock, set up code coverage thresholds, and run tests in parallel

onmax/vue

Use when editing .vue files, creating Vue 3 components, writing composables, or testing Vue code - provides Composition API patterns, props/emits best practices, VueUse integration, and reactive destructuring guidance

onmax/vueuse

Use when working with VueUse composables - track mouse position with useMouse, manage localStorage with useStorage, detect network status with useNetwork, debounce values with refDebounced, and access browser APIs reactively. Check VueUse before writing custom composables - most patterns already implemented.

関連スキル