TypeScriptプロジェクト
TypeScriptプロジェクトジェネレータは、ECMAScript Modules (ESM)、TypeScriptのプロジェクト参照、テスト実行用のVitest、静的解析用のESLintなど、ベストプラクティスで構成されたモダンなTypeScriptライブラリまたはアプリケーションを作成するために使用できます。
TypeScriptプロジェクトの生成
Section titled “TypeScriptプロジェクトの生成”新しいTypeScriptプロジェクトを2つの方法で生成できます:
- インストール Nx Console VSCode Plugin まだインストールしていない場合
- VSCodeでNxコンソールを開く
- クリック
Generate (UI)
"Common Nx Commands"セクションで - 検索
@aws/nx-plugin - ts#project
- 必須パラメータを入力
- クリック
Generate
pnpm nx g @aws/nx-plugin:ts#project
yarn nx g @aws/nx-plugin:ts#project
npx nx g @aws/nx-plugin:ts#project
bunx nx g @aws/nx-plugin:ts#project
パラメータ | 型 | デフォルト | 説明 |
---|---|---|---|
name 必須 | string | - | TypeScript project name |
directory | string | packages | Parent directory where the library is placed. |
subDirectory | string | - | The sub directory the lib is placed in. By default this is the library name. |
ジェネレータの出力
Section titled “ジェネレータの出力”ジェネレータは<directory>/<name>
ディレクトリに以下のプロジェクト構造を作成します:
Directorysrc TypeScriptソースコード
- index.ts
- project.json プロジェクト設定とビルドターゲット
- tsconfig.json このプロジェクトの基本TypeScript設定(ワークスペースルートのtsconfig.base.jsonを継承)
- tsconfig.lib.json ライブラリ用TypeScript設定(ランタイムまたはパッケージ化ソース用)
- tsconfig.spec.json テスト用TypeScript設定
- vite.config.ts Vitestの設定
- eslint.config.mjs ESLintの設定
ワークスペースルートの以下のファイルにも変更が加えられます:
- nx.json Nx設定が更新され、@nx/js/typescriptプラグインがプロジェクト用に構成されます
- tsconfig.base.json ワークスペース内の他のプロジェクトからインポート可能にするためのTypeScriptエイリアスが設定されます
- tsconfig.json プロジェクト参照が追加されます
TypeScriptソースコードの記述
Section titled “TypeScriptソースコードの記述”TypeScriptコードはsrc
ディレクトリに追加します。
ESMインポート構文
Section titled “ESMインポート構文”TypeScriptプロジェクトはESモジュールであるため、インポート文ではファイル拡張子を明示的に指定する必要があります:
import { sayHello } from './hello.js';
他のTypeScriptプロジェクト向けのエクスポート
Section titled “他のTypeScriptプロジェクト向けのエクスポート”TypeScriptプロジェクトのエントリポイントはsrc/index.ts
です。他のプロジェクトからインポート可能にする要素はここでエクスポートできます:
export { sayHello } from './hello.js';export * from './algorithms/index.js';
他のプロジェクトでのライブラリコードのインポート
Section titled “他のプロジェクトでのライブラリコードのインポート”ワークスペースのtsconfig.base.json
で設定されたTypeScriptエイリアスを使用して、他のTypeScriptプロジェクトからプロジェクトを参照できます:
import { sayHello } from ':my-scope/my-library';
ワークスペース内の新しいプロジェクトを初めてインポートする場合、以下のようなエラーがIDEに表示される可能性があります:
インポートエラー
File '/path/to/my/workspace/packages/my-library/src/index.ts' is not under 'rootDir' '/path/to/my/workspace/packages/my-consumer'. 'rootDir' is expected to contain all source files. File is ECMAScript module because '/path/to/my/workspace/package.json' has field "type" with value "module" ts(6059)File '/path/to/my/workspace/packages/my-library/src/index.ts' is not listed within the file list of project '/path/to/my/workspace/packages/my-consumer/tsconfig.lib.json'. Projects must list all files or use an 'include' pattern. File is ECMAScript module because '/path/to/my/workspace/package.json' has field "type" with value "module" ts(6307)
これはプロジェクト参照が設定されていないためです。
TypeScriptプロジェクトはNx TypeScript Syncジェネレータで事前設定されているため、手動での設定は不要です。以下のコマンドを実行するとNxが必要な設定を追加します:
pnpm nx sync
yarn nx sync
npx nx sync
bunx nx sync
これによりIDEのエラーが解消され、ライブラリを使用できるようになります。
TypeScriptプロジェクトにpackage.json
ファイルが存在しないことに気付くかもしれません。これは従来のTypeScriptモノレポに慣れている場合、予期しないことでしょう。
モノレポ内のTypeScriptパッケージの依存関係を追加するには、ワークスペースルートのpackage.json
に依存関係を追加します。パッケージマネージャのコマンドラインから追加できます:
pnpm add -w some-npm-package
yarn add some-npm-package
npm install --legacy-peer-deps some-npm-package
bun install some-npm-package
これにより、ワークスペース内のすべてのTypeScriptプロジェクトで依存関係を使用できるようになります。
ランタイムコード
Section titled “ランタイムコード”TypeScriptプロジェクトをランタイムコード(例:AWS Lambda関数のハンドラ)として使用する場合、Rolldownなどのツールを使用してプロジェクトをバンドルすることを推奨します。これにより、実際に参照されている依存関係のみが含まれるようツリーシェイキングされます。
project.json
ファイルに以下のようなターゲットを追加することで実現できます:
{ ... "targets": { ... "bundle": { "cache": true, "executor": "nx:run-commands", "outputs": ["{workspaceRoot}/dist/packages/my-library/bundle"], "options": { "command": "rolldown -c rolldown.config.ts" } }, },}
rolldown.config.ts
ファイルは以下のように追加します:
import { defineConfig } from 'rolldown';
export default defineConfig([ { input: 'src/index.ts', output: { file: '../../dist/packages/my-library/bundle/index.js', format: 'cjs', inlineDynamicImports: true, }, },]);
NPMへの公開
Section titled “NPMへの公開”TypeScriptプロジェクトをNPMに公開する場合は、package.json
ファイルを作成する必要があります。
このファイルにはプロジェクトが参照する依存関係を宣言する必要があります。ビルド時にはワークスペースルートのpackage.json
経由でインストールされた依存関係が解決されるため、Nx Dependency Checks ESLint Pluginを設定して、公開用のpackage.json
にプロジェクトで使用するすべての依存関係が含まれるようにすることを推奨します。
TypeScriptプロジェクトにはbuild
ターゲット(project.json
で定義)が設定されており、以下のコマンドで実行できます:
pnpm nx run <project-name>:build
yarn nx run <project-name>:build
npx nx run <project-name>:build
bunx nx run <project-name>:build
<project-name>
はプロジェクトの完全修飾名です。
build
ターゲットはプロジェクトのコンパイル、リンティング、テストを実行します。
ビルド出力はワークスペースルートのdist
フォルダ内のパッケージディレクトリ(例: dist/packages/<my-library>/tsc
)に生成されます。
Vitestがプロジェクトのテスト用に設定されています。
テストの記述
Section titled “テストの記述”テストは.spec.ts
または.test.ts
ファイルに記述し、プロジェクトのsrc
フォルダに配置します。
例:
Directorysrc
- hello.ts ライブラリソースコード
- hello.spec.ts hello.tsのテスト
Vitestはdescribe
、it
、test
、expect
などのJest風の構文を提供します。
import { sayHello } from './hello.js';
describe('sayHello', () => {
it('should greet the caller', () => { expect(sayHello('Darth Vader')).toBe('Hello, Darth Vader!'); });
});
テストの記述方法や依存関係のモック作成などの詳細については、Vitestドキュメントを参照してください。
テストの実行
Section titled “テストの実行”テストはプロジェクトのbuild
ターゲットの一部として実行されますが、test
ターゲットを個別に実行することもできます:
pnpm nx run <project-name>:test
yarn nx run <project-name>:test
npx nx run <project-name>:test
bunx nx run <project-name>:test
-t
フラグを使用して特定のテストを実行できます:
pnpm nx run <project-name>:test -t 'sayHello'
yarn nx run <project-name>:test -t 'sayHello'
npx nx run <project-name>:test -t 'sayHello'
bunx nx run <project-name>:test -t 'sayHello'
リンティング
Section titled “リンティング”TypeScriptプロジェクトでは、リンティングにESLint、フォーマットにPrettierを使用します。
ワークスペースルートのeslint.config.mjs
ファイルでESLintを構成することを推奨します。これにより、変更がワークスペース内のすべてのTypeScriptプロジェクトに適用され、一貫性が保たれます。
同様に、Prettierはルートの.prettierrc
ファイルで設定できます。
リンターの実行
Section titled “リンターの実行”プロジェクトのリンターをチェックするにはlint
ターゲットを実行します:
pnpm nx run <project-name>:lint
yarn nx run <project-name>:lint
npx nx run <project-name>:lint
bunx nx run <project-name>:lint
リント問題の修正
Section titled “リント問題の修正”ほとんどのリンティングやフォーマットの問題は自動修正可能です。--configuration=fix
引数を付けてESLintを実行することで修正できます:
pnpm nx run <project-name>:lint --configuration=fix
yarn nx run <project-name>:lint --configuration=fix
npx nx run <project-name>:lint --configuration=fix
bunx nx run <project-name>:lint --configuration=fix
ワークスペース内のすべてのパッケージのリント問題を修正するには:
pnpm nx run-many --target lint --all --configuration=fix
yarn nx run-many --target lint --all --configuration=fix
npx nx run-many --target lint --all --configuration=fix
bunx nx run-many --target lint --all --configuration=fix