@aramassa/ai-rules 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,883 @@
1
+ ---
2
+ type: development-rules
3
+ category: build
4
+ framework: rollup
5
+ language:
6
+ - typescript
7
+ - nodejs
8
+ applyTo:
9
+ - "rollup.config.*"
10
+ - "package.json"
11
+ - "tsconfig*.json"
12
+ ---
13
+
14
+ # npm Package Build with Rollup: 総合ルール
15
+
16
+ このドキュメントは、Rollup を使用した npm パッケージビルドの標準ルールを定義します。
17
+ TypeScript + Rollup + モノレポ構成での CLI ツールやライブラリの開発に適用されます。
18
+
19
+ ## 目次
20
+
21
+ - [概要](#概要)
22
+ - [前提条件と事前調査](#前提条件と事前調査)
23
+ - [依存関係管理](#依存関係管理)
24
+ - [Rollup 設定](#rollup-設定)
25
+ - [TypeScript 設定](#typescript-設定)
26
+ - [package.json 設定](#packagejson-設定)
27
+ - [ビルドプロセス](#ビルドプロセス)
28
+ - [配布物の品質確認](#配布物の品質確認)
29
+ - [トラブルシューティング](#トラブルシューティング)
30
+ - [ベストプラクティス](#ベストプラクティス)
31
+
32
+ ## 概要
33
+
34
+ ### Rollup の特徴と適用場面
35
+
36
+ Rollup は ES モジュール用に設計されたバンドラーで、以下の特徴があります:
37
+
38
+ 1. **優れた Tree-shaking**: 使用されていないコードを精密に除去
39
+ 2. **小さなバンドルサイズ**: 特にライブラリやCLIツールで効果的
40
+ 3. **ES モジュールファースト**: モダンな JavaScript 開発に最適
41
+ 4. **多様な出力フォーマット**: ESM, CJS, UMD, IIFE など
42
+
43
+ ### 適用場面
44
+
45
+ - **CLI ツール開発**: 単一実行ファイルの生成
46
+ - **ライブラリ開発**: 小さなバンドルサイズと複数フォーマット対応
47
+ - **モノレポ**: 複数パッケージの統合バンドル
48
+
49
+ ## 前提条件と事前調査
50
+
51
+ ### 実装前の確認事項
52
+
53
+ 1. **エントリーポイントの特定**
54
+ - CLI: `src/cli.ts`
55
+ - ライブラリ: `src/index.ts`
56
+
57
+ 2. **依存関係の分類**
58
+ - バンドル対象: プロジェクト固有のコード、小さな依存関係
59
+ - 外部化対象: ランタイム依存、大きなライブラリ(yaml, fs, pathなど)
60
+
61
+ 3. **出力フォーマットの決定**
62
+ - CLI: ESM(`format: 'esm'`)
63
+ - ライブラリ: ESM + CJS(複数出力)
64
+
65
+ 4. **モノレポ構成の確認**
66
+ - workspace パッケージの依存関係
67
+ - ビルド順序の決定
68
+
69
+ 5. **配布ファイル構成の決定**
70
+ - `dist/` ディレクトリ構成
71
+ - package.json の `files` フィールド内容
72
+
73
+ ## 依存関係管理
74
+
75
+ ### 必須パッケージ
76
+
77
+ ```bash
78
+ # Rollup 本体とコア機能
79
+ npm install --save-dev rollup
80
+
81
+ # TypeScript サポート
82
+ npm install --save-dev @rollup/plugin-typescript
83
+ npm install --save-dev typescript
84
+ npm install --save-dev tslib
85
+
86
+ # モジュール解決
87
+ npm install --save-dev @rollup/plugin-node-resolve
88
+ npm install --save-dev @rollup/plugin-commonjs
89
+
90
+ # その他の機能
91
+ npm install --save-dev @rollup/plugin-json
92
+ npm install --save-dev @rollup/plugin-alias # モノレポの場合
93
+ ```
94
+
95
+ ### tslib の重要性
96
+
97
+ **必須理由**:
98
+ - `@rollup/plugin-typescript` が依存している
99
+ - TypeScript ヘルパー関数の重複を防ぐ
100
+ - バンドルサイズを大幅に削減
101
+
102
+ **インストール場所**:
103
+ ```bash
104
+ # ルートプロジェクト(必須)
105
+ npm install --save-dev tslib
106
+
107
+ # 各 workspace パッケージ(推奨)
108
+ cd packages/core && npm install --save-dev tslib
109
+ cd packages/common && npm install --save-dev tslib
110
+ ```
111
+
112
+ **効果の例**:
113
+
114
+ ```typescript
115
+ // tslib なし: 各ファイルでヘルパー関数が重複生成
116
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
117
+ // 長いヘルパー関数コード...
118
+ };
119
+
120
+ // tslib あり: 共有ライブラリから import
121
+ import { __awaiter } from 'tslib';
122
+ ```
123
+
124
+ ## Rollup 設定
125
+
126
+ ### 基本設定(rollup.config.mjs)
127
+
128
+ ```javascript
129
+ import { defineConfig } from 'rollup';
130
+ import typescript from '@rollup/plugin-typescript';
131
+ import nodeResolve from '@rollup/plugin-node-resolve';
132
+ import commonjs from '@rollup/plugin-commonjs';
133
+ import json from '@rollup/plugin-json';
134
+
135
+ export default defineConfig({
136
+ input: 'src/index.ts',
137
+ output: {
138
+ file: 'dist/index.js',
139
+ format: 'esm',
140
+ sourcemap: false,
141
+ },
142
+ external: [], // 外部化する依存関係
143
+ plugins: [
144
+ nodeResolve({ preferBuiltins: true }),
145
+ commonjs({ transformMixedEsModules: true }),
146
+ json(),
147
+ typescript({
148
+ tsconfig: './tsconfig.build.json',
149
+ outputToFilesystem: true,
150
+ }),
151
+ ],
152
+ });
153
+ ```
154
+
155
+ ### CLI ツール向け設定
156
+
157
+ ```javascript
158
+ export default defineConfig({
159
+ input: 'src/cli.ts',
160
+ output: {
161
+ file: 'dist/cli.js',
162
+ format: 'esm',
163
+ inlineDynamicImports: true, // 動的インポートをインライン化
164
+ sourcemap: false,
165
+ },
166
+ external: ['yaml', 'fs', 'path'], // ランタイム依存は外部化
167
+ plugins: [
168
+ nodeResolve({ preferBuiltins: true }),
169
+ commonjs({ transformMixedEsModules: true }),
170
+ json(),
171
+ typescript({
172
+ tsconfig: './tsconfig.build.json',
173
+ outputToFilesystem: true,
174
+ }),
175
+ ],
176
+ });
177
+ ```
178
+
179
+ **重要ポイント**:
180
+ - `inlineDynamicImports: true`: すべてを単一ファイルにバンドル
181
+ - `external`: 大きなライブラリやNode.js組み込みモジュールを外部化
182
+ - `sourcemap: false`: 本番ビルドではソースマップを無効化
183
+
184
+ ### モノレポ向け設定
185
+
186
+ ```javascript
187
+ import { defineConfig } from 'rollup';
188
+ import alias from '@rollup/plugin-alias';
189
+ import typescript from '@rollup/plugin-typescript';
190
+ import nodeResolve from '@rollup/plugin-node-resolve';
191
+ import commonjs from '@rollup/plugin-commonjs';
192
+ import json from '@rollup/plugin-json';
193
+ import path from 'path';
194
+
195
+ export default defineConfig({
196
+ input: 'src/cli.ts',
197
+ output: {
198
+ file: 'dist/cli.js',
199
+ format: 'esm',
200
+ inlineDynamicImports: true,
201
+ sourcemap: false,
202
+ },
203
+ external: ['yaml'],
204
+ plugins: [
205
+ // 最初に alias でパス解決
206
+ alias({
207
+ entries: [
208
+ {
209
+ find: '@internal/core',
210
+ replacement: path.resolve('packages/core/dist/index.js')
211
+ },
212
+ {
213
+ find: '@internal/extract',
214
+ replacement: path.resolve('packages/extract/dist/index.js')
215
+ },
216
+ ],
217
+ }),
218
+ nodeResolve({ preferBuiltins: true }),
219
+ commonjs({ transformMixedEsModules: true }),
220
+ json(),
221
+ typescript({
222
+ tsconfig: './tsconfig.build.json',
223
+ outputToFilesystem: true,
224
+ }),
225
+ ],
226
+ });
227
+ ```
228
+
229
+ **重要ポイント**:
230
+ - `alias` プラグインを**最初**に配置
231
+ - workspace パッケージの `dist` を参照
232
+ - `@internal/*` プレフィックスで内部パッケージを識別
233
+
234
+ ### ライブラリ向け設定(複数フォーマット)
235
+
236
+ ```javascript
237
+ export default defineConfig([
238
+ // ESM 出力
239
+ {
240
+ input: 'src/index.ts',
241
+ output: {
242
+ file: 'dist/index.esm.js',
243
+ format: 'esm',
244
+ sourcemap: true,
245
+ },
246
+ external: ['react', 'lodash'], // ピア依存は外部化
247
+ plugins: [
248
+ nodeResolve(),
249
+ commonjs(),
250
+ typescript({
251
+ tsconfig: './tsconfig.build.json',
252
+ }),
253
+ ],
254
+ },
255
+ // CJS 出力
256
+ {
257
+ input: 'src/index.ts',
258
+ output: {
259
+ file: 'dist/index.cjs.js',
260
+ format: 'cjs',
261
+ sourcemap: true,
262
+ },
263
+ external: ['react', 'lodash'],
264
+ plugins: [
265
+ nodeResolve(),
266
+ commonjs(),
267
+ typescript({
268
+ tsconfig: './tsconfig.build.json',
269
+ }),
270
+ ],
271
+ },
272
+ ]);
273
+ ```
274
+
275
+ ### プラグインの順序(重要)
276
+
277
+ ```javascript
278
+ plugins: [
279
+ alias(), // 1. パス解決を最初に
280
+ nodeResolve(), // 2. 依存関係解決
281
+ commonjs(), // 3. モジュール変換
282
+ json(), // 4. JSON サポート
283
+ typescript(), // 5. TypeScript コンパイル(最後)
284
+ ]
285
+ ```
286
+
287
+ **理由**: プラグインは順番に処理されるため、依存関係解決 → 変換 → コンパイルの順序が重要
288
+
289
+ ### external 設定の指針
290
+
291
+ ```javascript
292
+ // CLI ツールの場合
293
+ external: [
294
+ 'yaml', // 大きなライブラリ
295
+ 'glob', // ランタイム依存
296
+ // Node.js 組み込みモジュールは自動的に外部化されるため不要
297
+ ]
298
+
299
+ // ライブラリの場合
300
+ external: [
301
+ 'react', // ピア依存関係
302
+ 'lodash', // ユーザーが既に持っている可能性が高い
303
+ /^@babel/, // 特定のスコープパッケージ全体
304
+ ]
305
+ ```
306
+
307
+ ## TypeScript 設定
308
+
309
+ ### tsconfig.build.json の基本設定
310
+
311
+ ```json
312
+ {
313
+ "extends": "./tsconfig.json",
314
+ "compilerOptions": {
315
+ "outDir": "dist",
316
+ "rootDir": "src",
317
+ "declaration": true,
318
+ "declarationMap": true,
319
+ "sourceMap": false,
320
+ "removeComments": false,
321
+ "types": ["node"],
322
+ "module": "ESNext",
323
+ "target": "ES2020",
324
+ "moduleResolution": "Bundler",
325
+ "importHelpers": true,
326
+ "noEmitHelpers": false
327
+ },
328
+ "include": ["src/**/*"],
329
+ "exclude": [
330
+ "test/**/*",
331
+ "src/**/*.test.ts",
332
+ "src/**/*.spec.ts",
333
+ "**/*.test.ts",
334
+ "**/*.spec.ts"
335
+ ]
336
+ }
337
+ ```
338
+
339
+ ### 重要な設定項目
340
+
341
+ | オプション | 値 | 理由 |
342
+ |-----------|-----|------|
343
+ | `importHelpers` | `true` | tslib からヘルパー関数をインポート |
344
+ | `noEmitHelpers` | `false` | ヘルパー関数の出力を許可 |
345
+ | `moduleResolution` | `"Bundler"` | Rollup 用の解決戦略 |
346
+ | `module` | `"ESNext"` | ES モジュール形式 |
347
+ | `target` | `"ES2020"` | モダンな JavaScript ターゲット |
348
+ | `sourceMap` | `false` | 本番ビルドでは無効化 |
349
+
350
+ ### モノレポでの tsconfig.json
351
+
352
+ #### ルートプロジェクト
353
+
354
+ ```json
355
+ {
356
+ "compilerOptions": {
357
+ "composite": true,
358
+ "declaration": true,
359
+ "importHelpers": true,
360
+ "noEmitHelpers": false
361
+ },
362
+ "references": [] // workspace パッケージは参照しない
363
+ }
364
+ ```
365
+
366
+ #### workspace パッケージ
367
+
368
+ ```json
369
+ {
370
+ "compilerOptions": {
371
+ "composite": true,
372
+ "declaration": true,
373
+ "declarationMap": true,
374
+ "importHelpers": true,
375
+ "noEmitHelpers": false
376
+ },
377
+ "references": [
378
+ { "path": "../common" } // 依存するパッケージを参照
379
+ ]
380
+ }
381
+ ```
382
+
383
+ ## package.json 設定
384
+
385
+ ### 基本設定
386
+
387
+ ```json
388
+ {
389
+ "name": "@scope/package-name",
390
+ "version": "1.0.0",
391
+ "type": "module",
392
+ "main": "dist/index.js",
393
+ "types": "dist/index.d.ts",
394
+ "files": [
395
+ "dist/",
396
+ "README.md",
397
+ "LICENSE"
398
+ ],
399
+ "publishConfig": {
400
+ "access": "public",
401
+ "registry": "https://registry.npmjs.org/"
402
+ }
403
+ }
404
+ ```
405
+
406
+ ### CLI ツールの場合
407
+
408
+ ```json
409
+ {
410
+ "name": "@scope/cli-tool",
411
+ "version": "1.0.0",
412
+ "type": "module",
413
+ "bin": {
414
+ "my-cli": "dist/cli.js"
415
+ },
416
+ "files": [
417
+ "dist/",
418
+ "README.md"
419
+ ],
420
+ "publishConfig": {
421
+ "access": "public"
422
+ }
423
+ }
424
+ ```
425
+
426
+ **重要**: CLI のエントリーファイル(`dist/cli.js`)には shebang が必要
427
+
428
+ ```typescript
429
+ #!/usr/bin/env node
430
+ // CLI コード
431
+ ```
432
+
433
+ ### ビルドスクリプト
434
+
435
+ ```json
436
+ {
437
+ "scripts": {
438
+ "build": "rollup -c",
439
+ "build:clean": "rm -rf dist && rollup -c",
440
+ "build:watch": "rollup -c -w",
441
+ "prepublishOnly": "npm run build:clean"
442
+ }
443
+ }
444
+ ```
445
+
446
+ ### モノレポの場合
447
+
448
+ ```json
449
+ {
450
+ "workspaces": [
451
+ "packages/core",
452
+ "packages/common"
453
+ ],
454
+ "scripts": {
455
+ "build": "npm run --ws build && tsc -p tsconfig.build.json",
456
+ "root:build": "npm run build && if [ \"$NODE_ENV\" != \"test\" ]; then rollup -c && chmod +x dist/cli.js; fi",
457
+ "build:pkg:clean": "npm run --ws clean",
458
+ "clean": "npm run build:pkg:clean && rm -rf dist",
459
+ "prepublishOnly": "npm run root:build"
460
+ }
461
+ }
462
+ ```
463
+
464
+ **ビルド順序**:
465
+ 1. `npm run --ws build`: 各パッケージのビルド
466
+ 2. `tsc -p tsconfig.build.json`: ルートプロジェクトのコンパイル
467
+ 3. `rollup -c`: バンドル生成
468
+ 4. `chmod +x dist/cli.js`: 実行権限付与(CLI の場合)
469
+
470
+ ## ビルドプロセス
471
+
472
+ ### 標準的なビルドフロー
473
+
474
+ ```mermaid
475
+ graph TD
476
+ A[workspace ビルド] --> B[ルート TypeScript コンパイル]
477
+ B --> C[Rollup バンドル]
478
+ C --> D[実行権限付与]
479
+ D --> E[配布物完成]
480
+ ```
481
+
482
+ ### 実行コマンド
483
+
484
+ ```bash
485
+ # 開発時のビルド
486
+ npm run build
487
+
488
+ # クリーンビルド
489
+ npm run build:clean
490
+
491
+ # watch モード(開発時)
492
+ npm run build:watch
493
+
494
+ # 本番ビルド(公開前)
495
+ npm run prepublishOnly
496
+ ```
497
+
498
+ ### CI/CD での実行
499
+
500
+ ```yaml
501
+ # GitHub Actions の例
502
+ - name: Install dependencies
503
+ run: npm ci
504
+
505
+ - name: Build project
506
+ run: npm run build:clean
507
+
508
+ - name: Verify CLI executable
509
+ run: |
510
+ chmod +x dist/cli.js
511
+ ./dist/cli.js --help
512
+ ```
513
+
514
+ ### 環境別の考慮事項
515
+
516
+ #### 開発環境(`NODE_ENV=development`)
517
+
518
+ ```javascript
519
+ const isDev = process.env.NODE_ENV === 'development';
520
+
521
+ export default defineConfig({
522
+ output: {
523
+ sourcemap: isDev, // 開発時のみソースマップ
524
+ },
525
+ plugins: [
526
+ typescript({
527
+ sourceMap: isDev,
528
+ inlineSources: isDev,
529
+ }),
530
+ ],
531
+ });
532
+ ```
533
+
534
+ #### テスト環境(`NODE_ENV=test`)
535
+
536
+ ```json
537
+ {
538
+ "scripts": {
539
+ "root:build": "npm run build && if [ \"$NODE_ENV\" != \"test\" ]; then rollup -c; fi"
540
+ }
541
+ }
542
+ ```
543
+
544
+ **理由**: テスト時は Rollup バンドルをスキップして高速化
545
+
546
+ ## 配布物の品質確認
547
+
548
+ ### 必須チェック項目
549
+
550
+ #### 1. パッケージ内容の確認
551
+
552
+ ```bash
553
+ # ドライラン実行(ファイル一覧表示)
554
+ npm pack --dry-run
555
+
556
+ # 実際にパッケージ化
557
+ npm pack
558
+
559
+ # 内容確認
560
+ tar -tzf package-name-1.0.0.tgz
561
+ ```
562
+
563
+ #### 2. .npmignore の設定
564
+
565
+ ```
566
+ # 開発用ファイル
567
+ src/
568
+ test/
569
+ *.test.ts
570
+ *.spec.ts
571
+
572
+ # 設定ファイル
573
+ tsconfig*.json
574
+ rollup.config.mjs
575
+ .eslintrc*
576
+ .prettierrc*
577
+
578
+ # ドキュメント(README.md は含める)
579
+ docs/
580
+ .github/
581
+
582
+ # ビルドキャッシュ
583
+ *.tsbuildinfo
584
+ .cache/
585
+ ```
586
+
587
+ #### 3. files フィールドの確認
588
+
589
+ ```json
590
+ {
591
+ "files": [
592
+ "dist/",
593
+ "README.md",
594
+ "LICENSE"
595
+ ]
596
+ }
597
+ ```
598
+
599
+ **ベストプラクティス**: `.npmignore` より `files` フィールドを推奨(明示的で安全)
600
+
601
+ ### 確認タイミング
602
+
603
+ - ✅ 機能追加・リファクタリング後
604
+ - ✅ リリース前(必須)
605
+ - ✅ コミット前(推奨)
606
+
607
+ ### バンドルサイズの確認
608
+
609
+ ```bash
610
+ # ファイルサイズ確認
611
+ ls -lh dist/cli.js
612
+
613
+ # 依存関係の可視化(オプション)
614
+ npx rollup-plugin-visualizer
615
+ ```
616
+
617
+ ## トラブルシューティング
618
+
619
+ ### よくある問題と解決方法
620
+
621
+ #### 1. tslib エラー
622
+
623
+ **症状**:
624
+ ```text
625
+ RollupError: [plugin typescript] @rollup/plugin-typescript: Could not find module 'tslib'
626
+ ```
627
+
628
+ **原因**: tslib パッケージ未インストール
629
+
630
+ **解決**:
631
+ ```bash
632
+ npm install --save-dev tslib
633
+ ```
634
+
635
+ #### 2. モジュール解決エラー
636
+
637
+ **症状**:
638
+ ```text
639
+ Error: Could not resolve '@internal/core' from src/cli.ts
640
+ ```
641
+
642
+ **原因**: alias 設定または build 順序の問題
643
+
644
+ **解決**:
645
+ ```bash
646
+ # 1. workspace パッケージが先にビルドされているか確認
647
+ npm run --ws build
648
+
649
+ # 2. alias の path 設定を確認
650
+ # rollup.config.mjs で path.resolve() を使用
651
+ ```
652
+
653
+ #### 3. shebang 重複エラー
654
+
655
+ **症状**:
656
+ ```javascript
657
+ #!/usr/bin/env node
658
+ #!/usr/bin/env node
659
+ // コード
660
+ ```
661
+
662
+ **原因**: rollup.config.mjs の `banner` とソースファイルの shebang が重複
663
+
664
+ **解決**:
665
+ ```javascript
666
+ // rollup.config.mjs から banner を削除
667
+ export default defineConfig({
668
+ output: {
669
+ // banner: '#!/usr/bin/env node', // ← 削除
670
+ }
671
+ });
672
+
673
+ // ソースファイル(src/cli.ts)側で定義
674
+ #!/usr/bin/env node
675
+ ```
676
+
677
+ #### 4. 実行権限エラー
678
+
679
+ **症状**:
680
+ ```text
681
+ Permission denied: ./dist/cli.js
682
+ ```
683
+
684
+ **原因**: 生成されたファイルに実行権限がない
685
+
686
+ **解決**:
687
+ ```bash
688
+ chmod +x dist/cli.js
689
+ ```
690
+
691
+ または package.json で自動化:
692
+ ```json
693
+ {
694
+ "scripts": {
695
+ "root:build": "... && chmod +x dist/cli.js"
696
+ }
697
+ }
698
+ ```
699
+
700
+ #### 5. CommonJS と ES モジュールの混在問題
701
+
702
+ **症状**:
703
+ ```text
704
+ [!] (plugin commonjs) TypeError: Cannot read property 'xxx' of undefined
705
+ ```
706
+
707
+ **原因**: CommonJS モジュールの変換が適切に行われていない
708
+
709
+ **解決**:
710
+ ```javascript
711
+ commonjs({
712
+ transformMixedEsModules: true, // 混在モジュールの変換を有効化
713
+ })
714
+ ```
715
+
716
+ #### 6. 外部依存関係の処理エラー
717
+
718
+ **症状**:
719
+ ```text
720
+ Module 'yaml' not found in bundle
721
+ ```
722
+
723
+ **解決**:
724
+ ```javascript
725
+ // オプション 1: 外部化
726
+ external: ['yaml'],
727
+
728
+ // オプション 2: バンドルに含める(external から削除)
729
+ external: [], // yaml はバンドルされる
730
+ ```
731
+
732
+ ### デバッグ手順
733
+
734
+ #### 段階的ビルド確認
735
+
736
+ ```bash
737
+ # 1. パッケージビルド確認
738
+ npm run --ws build
739
+
740
+ # 2. ルートコンパイル確認
741
+ tsc -p tsconfig.build.json
742
+
743
+ # 3. Rollup バンドル確認
744
+ rollup -c
745
+
746
+ # 4. 成果物確認
747
+ ls -la dist/
748
+ ```
749
+
750
+ #### 依存関係確認
751
+
752
+ ```bash
753
+ # tslib インストール確認
754
+ npm list tslib
755
+
756
+ # workspace 依存関係確認
757
+ npm list --workspaces
758
+
759
+ # 全体の依存関係ツリー
760
+ npm list
761
+ ```
762
+
763
+ ## ベストプラクティス
764
+
765
+ ### 1. プラグインの順序を守る
766
+
767
+ ```javascript
768
+ plugins: [
769
+ alias(), // パス解決を最初に
770
+ nodeResolve(), // 依存関係解決
771
+ commonjs(), // モジュール変換
772
+ json(), // JSON サポート
773
+ typescript(), // TypeScript コンパイル(最後)
774
+ ]
775
+ ```
776
+
777
+ ### 2. 外部依存関係の適切な管理
778
+
779
+ ```javascript
780
+ // CLI ツール: ランタイム依存のみ外部化
781
+ external: ['yaml', 'glob'],
782
+
783
+ // ライブラリ: ピア依存関係を外部化
784
+ external: ['react', 'lodash'],
785
+ ```
786
+
787
+ ### 3. 出力フォーマットの選択
788
+
789
+ ```javascript
790
+ // CLI ツール: 単一フォーマット
791
+ output: {
792
+ file: 'dist/cli.js',
793
+ format: 'esm',
794
+ }
795
+
796
+ // ライブラリ: 複数フォーマット
797
+ output: [
798
+ { file: 'dist/index.esm.js', format: 'esm' },
799
+ { file: 'dist/index.cjs.js', format: 'cjs' },
800
+ ]
801
+ ```
802
+
803
+ ### 4. Tree-shaking の最大化
804
+
805
+ ```typescript
806
+ // ❌ 全体インポート
807
+ import * as utils from './utils';
808
+
809
+ // ✅ 必要な部分のみインポート
810
+ import { specificFunction } from './utils';
811
+ ```
812
+
813
+ ### 5. ビルドキャッシュの活用
814
+
815
+ ```json
816
+ {
817
+ "compilerOptions": {
818
+ "incremental": true,
819
+ "tsBuildInfoFile": ".tsbuildinfo"
820
+ }
821
+ }
822
+ ```
823
+
824
+ ### 6. 環境変数による制御
825
+
826
+ ```javascript
827
+ const isDev = process.env.NODE_ENV === 'development';
828
+
829
+ export default defineConfig({
830
+ output: {
831
+ sourcemap: isDev,
832
+ },
833
+ treeshake: isDev ? false : {
834
+ moduleSideEffects: false,
835
+ },
836
+ });
837
+ ```
838
+
839
+ ### 7. モノレポでのビルド順序管理
840
+
841
+ ```json
842
+ {
843
+ "scripts": {
844
+ "build": "npm run --ws build && tsc && rollup -c",
845
+ "build:watch": "npm run --ws build:watch & rollup -c -w"
846
+ }
847
+ }
848
+ ```
849
+
850
+ ### 8. 配布物の最終確認
851
+
852
+ ```bash
853
+ # リリース前チェックリスト
854
+ npm run build:clean # クリーンビルド
855
+ npm test # テスト実行
856
+ npm pack --dry-run # パッケージ内容確認
857
+ npm run prepublishOnly # 公開前処理
858
+ ```
859
+
860
+ ## まとめ
861
+
862
+ ### Rollup を使うべき場面
863
+
864
+ 1. **CLI ツール開発**: 単一ファイルの実行可能ファイル生成
865
+ 2. **ライブラリ開発**: 小さなバンドルサイズと複数フォーマット対応
866
+ 3. **モノレポ**: 複数パッケージの統合バンドル
867
+ 4. **TypeScript プロジェクト**: tslib との連携による最適化
868
+
869
+ ### 重要なチェックポイント
870
+
871
+ - ✅ tslib のインストール(ルートと各パッケージ)
872
+ - ✅ プラグインの順序(alias → nodeResolve → commonjs → json → typescript)
873
+ - ✅ external 設定(大きなライブラリは外部化)
874
+ - ✅ TypeScript 設定(`importHelpers: true`, `noEmitHelpers: false`)
875
+ - ✅ package.json の `files` フィールド
876
+ - ✅ `npm pack --dry-run` での配布物確認
877
+ - ✅ CLI の場合は shebang と実行権限
878
+
879
+ ### 参考リンク
880
+
881
+ - [Rollup 公式ドキュメント](https://rollupjs.org/)
882
+ - [@rollup/plugin-typescript](https://github.com/rollup/plugins/tree/master/packages/typescript)
883
+ - [tslib](https://github.com/Microsoft/tslib)